I want to get selected associated product id after an checkout_cart_add_after triggered. If any helps would be appreciable.
1 Answer
I will be speaking for Magento 2.2.6
There is an event dispatch like this
$this->_eventManager->dispatch(
'checkout_cart_product_add_after',
['quote_item' => $result, 'product' => $product]
;
inside the addProduct
method of the Magento\Checkout\Model\Cart
class.
The $result
variable that is dispatched, and will be available to you if you were to listen to the checkout_cart_product_add_after
event, is actually of type \Magento\Quote\Model\Quote\Item
, i.e. this is the item that is added to the quote.
In this screenshot you will see all relevant data that you need. For example, in the _data
array there is the product_type
which in my example is configurable
, but in the sku
, this is the sku of the actual simple product that I have chosen.
And don't worry, here it will have the appropriate data if you need to extract data from a grouped or a bundle product. It is simply there, and this is what you should utilize.
So, the event again is checkout_cart_product_add_after
, and you should be able to collect the data from the Observer $observer
in your execute
method.
I hope this helps.
-
what method should I need inject there to get the details of associated products in M2.2x Oct 15, 2018 at 6:06