Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 7b728b1

Browse files
committed
Add checkout_cart_product_add_before event #17830
1 parent 3797563 commit 7b728b1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

app/code/Magento/Checkout/Model/Cart.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ public function addProduct($productInfo, $requestInfo = null)
370370

371371
if ($productId) {
372372
try {
373+
$this->_eventManager->dispatch(
374+
'checkout_cart_product_add_before',
375+
['info' => $requestInfo, 'product' => $product]
376+
);
373377
$result = $this->getQuote()->addProduct($product, $request);
374378
} catch (\Magento\Framework\Exception\LocalizedException $e) {
375379
$this->_checkoutSession->setUseNotice(false);

app/code/Magento/Checkout/Test/Unit/Model/CartTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,20 @@ public function testAddProduct($productInfo, $requestInfo)
315315
$this->productRepository->expects($this->any())
316316
->method('getById')
317317
->will($this->returnValue($product));
318+
319+
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
320+
'checkout_cart_product_add_before',
321+
['info' => $requestInfo, 'product' => $product]
322+
);
323+
318324
$this->quoteMock->expects($this->once())
319325
->method('addProduct')
320326
->will($this->returnValue(1));
321327
$this->checkoutSessionMock->expects($this->once())
322328
->method('getQuote')
323329
->will($this->returnValue($this->quoteMock));
324330

325-
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
331+
$this->eventManagerMock->expects($this->at(1))->method('dispatch')->with(
326332
'checkout_cart_product_add_after',
327333
['quote_item' => 1, 'product' => $product]
328334
);
@@ -360,6 +366,12 @@ public function testAddProductException()
360366
$this->productRepository->expects($this->any())
361367
->method('getById')
362368
->will($this->returnValue($product));
369+
370+
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
371+
'checkout_cart_product_add_before',
372+
['info' => 4, 'product' => $product]
373+
);
374+
363375
$this->quoteMock->expects($this->once())
364376
->method('addProduct')
365377
->will($this->returnValue('error'));
@@ -396,6 +408,11 @@ public function testAddProductExceptionBadParams()
396408
->method('getById')
397409
->will($this->returnValue($product));
398410

411+
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
412+
'checkout_cart_product_add_before',
413+
['info' => 'bad', 'product' => $product]
414+
);
415+
399416
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
400417
'checkout_cart_product_add_after',
401418
['quote_item' => 1, 'product' => $product]

0 commit comments

Comments
 (0)