Show error message when adding a product of price 0.

To restrict products having price zero from shopping we need to create a observer on
checkout_cart_product_add_after event such that on every add to cart it will check the
product price and then add to cart.

Create a module and in etc/config.xml define the observer under event node.

Here Our module name is zeroprice under Letsknowit namespace.

File path : app/code/local/Letsknowit/zeroprice/etc/config.xml

 

       

           

               

                    singleton

                    zeroprice/observer

                    checkProductPrice

               

           

       

 

Now in Model folder we need to create observer with function name checkProductPrice.

File path : app/code/local/Letsknowit/zeroprice/Model/Observer.php

 

Class Letsknowit_Zeroprice_Model_Observer {

   

      public function checkProductPrice(Varien_Event_Observer $observer) {

        

        if ($observer->getProduct()->getFinalPrice() == 0) {

            Mage::getSingleton('core/session')->addError('Sorry, you cannot purchase "'.$observer->getProduct()->getName().'" product.Write to us if any query!'));

            $url = Mage::getModel('core/url')->getUrl('checkout/cart/index');

            Mage::app()->getResponse()->setRedirect($url);

            Mage::app()->getResponse()->sendResponse();

            exit();

        }

    }

 

}

?>

0   0
Sikha Goel
profile Shailesh 25th February 2017

Thanks for this post.

Sikha
22-04-2017 00:00

thank you Sir!!

Write a comment ...
Post comment
Cancel

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site