Add custom message in checkout cart page after adding product into cart when you are adding product to cart or in checkout page.
Add custom message in checkout cart page after adding product to cart

In this article I am going to explain how you can add custom message in checkout cart page after adding product into cart when you are adding product to cart or in checkout page. First some background. Magento handles item specific display via a product object variable/method called ‘customOptions’. You would already have used this in your code, or come across it, as it is the same place the ‘buyRequest’ data for a product (or cart item) is stored during checkout.

Thus, it is possible for us to inject our own item level message, using the ‘additional_options’ message code. Reading the code, we need to insert an array with keys ‘label’ and ‘value’ How to do it: In this example, I have created a product attribute called ‘custom_message’. This is a varchar field, and can be populated with any message via admin product edit section, or you could add an inputbox in the products area to allow the user to enter a message for the item. The first step is to create an observer for the ‘catalog_product_load_after’ event in your config.xml in the dir :

app/code/local/[NameSpace]/[ModuleName]/etc

and you can place below code here.



    .............
    .............
    
        
            
                
                    model
                    jr_setcustomoption/observer
                    setOption
                
            
        
    
    ...............
    ...............


Next create an Observer.php file under model folder in the dir :

app/code/local/[NameSpace]/[ModuleName]/model

and place below code.


< ?php

class JR_Setcustomoption_Model_Observer
{
    public function setOption(Varien_Event_Observer $observer)
    {
        $action = Mage::app()->getFrontController()->getAction();
        $params = Mage::app()->getRequest()->getParams();
        
        if ($action->getFullActionName() == 'checkout_cart_add')
        {
            $product = $observer->getProduct();
            $customMessage = $product->getCustomMessage();
            
            //this array will acces the text message from product edit section having attribute 'custom_message'.
            $additionalOptions[] = array(
                'label' => 'My custom message',
                'value' => $customMessage,
            );
            
            //or for static value use below code.
            /*
            $additionalOptions[] = array(
                'label' => 'My custom message',
                'value' => 'I have added a new message',
            );
            */
            $product->addCustomOption('additional_options', serialize($additionalOptions),$product);
        }
    }
}

?>
    

So don’t be selfish please give some comments whether it helps you or not ?

Add custom message in checkout cart page
Tagged on:                     

One thought on “Add custom message in checkout cart page

Leave a Reply

Your email address will not be published. Required fields are marked *