I want to add product to cart. So when i click on the add to cart button i will be checking some condition like some variable is set or not. If it is set, product will be added to cart. If not set, the product should not be added to cart.

for this i was using observer called controller_action_predispatch_checkout_cart_add

I can wrote a condition to check variable is set or not.if it is not set i want to come out of the observer. I mean product should not added to cart.

Please Help Me.

Thanks In Advance

2 Answers 2


The closest events I could find that would work that are lower than the dispatch event are


You can have an event listener on one of those events that does your logic check and throws an exception if it fails.

That being said, Magento itself actually uses the following event


Then, in the listener is uses code like this

$quoteItem = $observer->getEvent()->getQuoteItem();
$product = $observer->getEvent()->getProduct();
// Your checks here, return if you want to leave things alone
Mage::throwException('This is the message to the customer');

This is something that will help you: (as seen on Inchoo's blog article)


<?xml version="1.0"?>


class Brst_Test_Model_Observer
    //Put any event as per your requirement
    public function logCartAdd() {
        $product = Mage::getModel('catalog/product')
                        ->load(Mage::app()->getRequest()->getParam('product', 0));
        if (!$product->getId()) {
        $categories = $product->getCategoryIds();
            new Varien_Object(array(
                'id' => $product->getId(),
                'qty' => Mage::app()->getRequest()->getParam('qty', 1),
                'name' => $product->getName(),
                'price' => $product->getPrice(),
                'category_name' => Mage::getModel('catalog/category')->load($categories[0])->getName(),


<?xml version="1.0"?>
<layout version="0.1.0">
        <reference name="before_body_end">
            <block type="brst_test/event_checkout_cart_index" name="brst_test_event_checkout_cart_index" template="brst/test/event/checkout/cart/index.phtml" />

app/design/frontend/default/default/ template/brst/test/event/checkout/cart/index.phtml

<?php $_product = Mage::getModel('core/session')->getProductToShoppingCart() ?>
<?php if ($_product && $_product->getId()): ?>
    <script type="text/javascript">
        //Some 3rd party JS tracking code
    <?php Mage::getModel('core/session')->unsProductToShoppingCart(); ?>
<?php endif; ?>

Your Answer

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

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