1

I need to create a dynamic shopping cart rule depending on the count of items in the cart.

So I need to listen to an event which changes the item count in the cart (add product, remove product, change quantity).

Does such an event exist or do I have to listen for multiple events?

4 Answers 4

6

you can use this observer, it will be triggered whenever the cart is updated.

checkout_cart_save_after

magento_root/app/etc/modules/Namespace_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_Module>
    </modules>
</config>

magento_root/app/code/local/Namespace/Module/etc/config.xml

<?xml version="1.0"?> 
<config>
    <modules>
        <Namespace_Module>
            <version>0.1</version>
        </Namespace_Module>
    </modules>
    <global>
        <models>
            <namespace_module>
                <class>Namespace_Module_Model</class>
            </namespace_module>
        </models>   
        <events>
            <checkout_cart_save_after>
                <observers>
                    <cart_update>
                        <class>Namespace_Module_Model_UpdateObserver</class>
                        <method>updateCartAfter</method>
                    </cart_update>
                </observers>
            </checkout_cart_save_after>    
        </events>
    </global>
</config>

magento_root/app/code/local/Namespace/Module/Model/UpdateObserver.php

<?php

class Namespace_Module_Model_UpdateObserver {

    public function updateCartAfter($observer) {
        die('product add after');   
    }

}
4
  • In my case the event doesn't get triggered when a product is added to cart Nov 8, 2016 at 6:59
  • @DominikBarann I've updated my post please test this. It should work. Nov 8, 2016 at 7:12
  • Damn, I found my mistake: I changed the name of my custom observer name in the xml node and not the name of the event. So it doesn't change anything. Now I added the checkout_cart_save_after at the correct position and everything works. Thank you! Nov 8, 2016 at 8:29
  • I'm glad it works for you! Nov 8, 2016 at 8:36
2

there are many cart event you can observe its completely depending on your requirement

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

These are the list to cart events you can use as per your purpose.

1
  • I want to check the cart at stage of order placed, what event i need to use?
    – zus
    Sep 6, 2019 at 11:29
1

In your module config.xml file

<events>
        <checkout_cart_save_after>
            <observers>
                <uniquename>
                    <type>singleton</type>
                    <class>Package_Modulename_Model_Observer</class>
                    <method>removeItem</method>
                </uniquename>
            </observers>
        </checkout_cart_save_after>
    </events>

In your observer File:

public function removeItem($observer)
{

$cart = $observer->getCart();
$total = $cart->getItemsCount();

if($total > 3){
   //your code here
}
2
  • In my case the event doesn't get triggered when a product is added to cart Nov 8, 2016 at 7:00
  • Can you please add your config.xml code and also observer code here?
    – user46386
    Nov 8, 2016 at 7:20
0

There is no specific observer, as they all are different actions, but you can have an action on checkout_cart_save_before. This is executed every time there is a change to the current basket.

1
  • In my case the event doesn't get triggered when a product is added to cart Nov 8, 2016 at 7:00

Your Answer

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

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