Add to cart after event in magento2

The question:

I have created add to cart before the event it’s working fine. But I want to the observer after add to cart please tell me how to do it.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="cart_eventbefore_observer_cartbefore" instance="CartEventbeforeObserverCartbefore" />
    </event>
</config>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You can use the checkout_cart_add_product_complete event, which fires after the product added to cart successfully.

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_cart_add_product_complete"> 
      <observer name="after_add_to_cart" instance="YourObserverFile" /> 
   </event>
</config>

Method 2

checkout_cart_product_add_after worked for me.

Here are the events fired for checkout cart:

checkout_cart_add_product_complete
checkout_cart_update_item_complete
checkout_cart_product_add_after
checkout_cart_update_items_before
checkout_cart_update_items_after
checkout_cart_save_before
checkout_cart_save_after
checkout_cart_product_update_after

https://cyrillschumacher.com/magento-2.2-list-of-all-dispatched-events/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment