Skip to content

Commit 8879751

Browse files
ENGCOM-2987: Add checkout_cart_product_add_before event #17830 #18080
- Merge Pull Request #18080 from leandro-rosa/magento2:17830 - Merged commits: 1. 7b728b1 2. f9389ad 3. fef2205 4. 0de795a
2 parents f710f9b + 0de795a commit 8879751

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

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

382382
try {
383+
$this->_eventManager->dispatch(
384+
'checkout_cart_product_add_before',
385+
['info' => $requestInfo, 'product' => $product]
386+
);
383387
$result = $this->getQuote()->addProduct($product, $request);
384388
} catch (\Magento\Framework\Exception\LocalizedException $e) {
385389
$this->_checkoutSession->setUseNotice(false);
@@ -615,6 +619,8 @@ public function truncate()
615619
}
616620

617621
/**
622+
* Get product ids.
623+
*
618624
* @return int[]
619625
*/
620626
public function getProductIds()

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

Lines changed: 18 additions & 5 deletions
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,17 +369,19 @@ public function testAddProductException()
363369
$this->productRepository->expects($this->any())
364370
->method('getById')
365371
->will($this->returnValue($product));
372+
373+
$this->eventManagerMock->expects($this->once())->method('dispatch')->with(
374+
'checkout_cart_product_add_before',
375+
['info' => 4, 'product' => $product]
376+
);
377+
366378
$this->quoteMock->expects($this->once())
367379
->method('addProduct')
368380
->will($this->returnValue('error'));
369381
$this->checkoutSessionMock->expects($this->once())
370382
->method('getQuote')
371383
->will($this->returnValue($this->quoteMock));
372384

373-
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
374-
'checkout_cart_product_add_after',
375-
['quote_item' => 1, 'product' => $product]
376-
);
377385
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
378386
$this->cart->addProduct(4, 4);
379387
}
@@ -399,6 +407,11 @@ public function testAddProductExceptionBadParams()
399407
->method('getById')
400408
->will($this->returnValue($product));
401409

410+
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
411+
'checkout_cart_product_add_before',
412+
['info' => 'bad', 'product' => $product]
413+
);
414+
402415
$this->eventManagerMock->expects($this->never())->method('dispatch')->with(
403416
'checkout_cart_product_add_after',
404417
['quote_item' => 1, 'product' => $product]

0 commit comments

Comments
 (0)