Skip to content

Commit d3b9c5a

Browse files
committed
Add checkout_cart_product_add_before event #17830
1 parent 89622ea commit d3b9c5a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-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

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

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,20 @@ public function testAddProduct($productInfo, $requestInfo)
318318
$this->productRepository->expects($this->any())
319319
->method('getById')
320320
->will($this->returnValue($product));
321+
322+
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
323+
'checkout_cart_product_add_before',
324+
['info' => $requestInfo, 'product' => $product]
325+
);
326+
321327
$this->quoteMock->expects($this->once())
322328
->method('addProduct')
323329
->will($this->returnValue(1));
324330
$this->checkoutSessionMock->expects($this->once())
325331
->method('getQuote')
326332
->will($this->returnValue($this->quoteMock));
327333

328-
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
334+
$this->eventManagerMock->expects($this->at(1))->method('dispatch')->with(
329335
'checkout_cart_product_add_after',
330336
['quote_item' => 1, 'product' => $product]
331337
);
@@ -363,6 +369,10 @@ public function testAddProductException()
363369
$this->productRepository->expects($this->any())
364370
->method('getById')
365371
->will($this->returnValue($product));
372+
$this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
373+
'checkout_cart_product_add_before',
374+
['info' => 4, 'product' => $product]
375+
);
366376
$this->quoteMock->expects($this->once())
367377
->method('addProduct')
368378
->will($this->returnValue('error'));
@@ -399,6 +409,11 @@ public function testAddProductExceptionBadParams()
399409
->method('getById')
400410
->will($this->returnValue($product));
401411

412+
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
413+
'checkout_cart_product_add_before',
414+
['info' => 'bad', 'product' => $product]
415+
);
416+
402417
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
403418
'checkout_cart_product_add_after',
404419
['quote_item' => 1, 'product' => $product]

0 commit comments

Comments
 (0)