cancel
Showing results for 
Search instead for 
Did you mean: 

How to Observe 'add to cart' of a product

How to Observe 'add to cart' of a product

I was wondering if anyone could help me with setting up an observer for the add to cart, so when the event of a product being successfully added fires then the observer will pick up and see if it's product x. If so then javascript alert. Else assume normal add to cart functionality of going to shopping cart.

 

Sorry I am not the most adept as of yet at magento: I believe checkout_cart_product_add_after is the event that'd be observed. Would this be correct ?

Tags (2)
5 REPLIES

Re: How to Observe 'add to cart' of a product

Yes you can, use below event to track add to cart action...

controller_action_predispatch_checkout_cart_add

However you can get product ids as well so can check your conditions... 

 $product = Mage::getModel('catalog/product')
                  ->load(Mage::app()->getRequest()->getParam('product', 0));

Good luck Smiley Happy

-
Magento Programmer | Was my answer helpful? You can accept it as a solution.

Re: How to Observe 'add to cart' of a product

Thank you for taking time to reply, could you guide me to creating the observer. 

 

So far i have done this:

 

1- Created Vish_Popup.xml in /app/etc/modules

 

<?xml version="1.0"?>
<config>
    <modules>
        <Vish_Popup>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </Vish_Popup>
    </modules>
</config>

2- Created folder structure: /app/code/local/Vish/Popup/etc

 

 

3- in there created config.xml

<?xml version="1.0"?>
<config>
<modules>
<Vish_Popup>
<version>0.0.1</version>
</Vish_Popup>
</modules>
<global>
<models>
<vish_popup>
<class>Vish_Popup_Model</class>
</vish_popup>
</models>
<events>
<checkout_cart_add_product_complete>
<observers>
<vish_add_popup>
<type>singleton</type>
<class>vish_popup/observer</class>
<method>addMsg</method>
</vish_add_popup>
</observers>
</checkout_cart_add_product_complete>
</events>
</global>
</config>

Then in in /app/code/local/Vish/Popup/Models i created Observer.php

 

<?php
	
class Vish_Popup_Model_Observer

{

    public addMsg(Varien_Event_Observer $observer)

    {

        $ProductId = ‘1’;

        $product = $observer->getEvent()->getProduct();

        if ($product->getId() != $ProductId) {

            return true;

        }
		

    }

}
?>

I am not too sure I am doing this correct, but if I am now I need to code the pop-up ?

 

Re: How to Observe 'add to cart' of a product

okay

Re: How to Observe 'add to cart' of a product

Hi vpatel3

 Steps 1 to 3 is good and below things you can do in Observer.php file

 

<?php
    
class Vish_Popup_Model_Observer
{

    public function addMsg(Varien_Event_Observer $observer)

    {

        $ProductId = '1';

        $product = $observer->getEvent()->getProduct();

        if ($product->getId() == $ProductId) {

           Mage::getSingleton("checkout/session")->setshowPopMessage(1);

        }
        

    }

}


Here i have set a flag in session to know whether i want to display a popup or not.

 and in cart.phtml file you can take this session variable data and if you get its with value 1 then u can show a popup to customer and then unset value of that session variable


Here is a example code for cart.phtml

 

<?php
$showPopup=Mage::getSingleton("checkout/session")->getshowPopMessage();

if($showPopup==1)
{
    Mage::getSingleton("checkout/session")->unsshowPopMessage();
?>
<script type="text/javascript">
alert("Some Message you can show to customer");
</script>
<?php }?>


Hope it solve your issue.

 Regards,

Infobeans Team

 

 

 

Re: How to Observe 'add to cart' of a product

Hi Infobeans team,

 

is there a way to make the pop-up appear on the products page ?