0

I want to get selected associated product id after an checkout_cart_add_after triggered. If any helps would be appreciable.

1 Answer 1

2

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.

enter image description here

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.

1
  • what method should I need inject there to get the details of associated products in M2.2x Commented Oct 15, 2018 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.