Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom messages in magento when a user adds an item to a cart?

Tags:

magento

First I wanted to create a new attribute. Let's call it "Price Factor." Integer values can be set from the product page on the admin control panel.

On the front end, whenever a user adds an item to the shopping cart, a message pops up in the shopping bag and says "Your old price was X and your new price is Y" (where X was the original price and Y is the adjusted price.)

How would I go about create a custom message when someone adds something to their shopping cart?

like image 973
user784637 Avatar asked Aug 12 '11 19:08

user784637


1 Answers

This first thing you need to do is listen to the event that's fired when an item is added to the cart. This is called checkout_cart_add_product_complete and originates from Mage/Checkout/controllers/CartController.php.

The signature of the event that is dispatches is:

Mage::dispatchEvent('checkout_cart_add_product_complete',
    array(
        'product'  => $product,
        'request'  => $this->getRequest(),
        'response' => $this->getResponse()
    )
);

We can access the product that has been added to the cart through the product variable. That means we can assess whether we need to show our new message or not, based on your attribute.


The next step is to add a flash message to the page. This is controlled through sessions. There are three types of messages that can be used: success, error and notice. Adding a message is simple:

Mage::getSingleton('core/session')->addSuccess($message);
Mage::getSingleton('core/session')->addError($message);
Mage::getSingleton('core/session')->addNotice($message);
like image 97
Nick Avatar answered Nov 07 '22 20:11

Nick