5

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

5

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

catalog_product_type_prepare_full_options
catalog_product_type_prepare_lite_options

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

checkout_cart_product_add_after

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
$quoteItem->getQuote()->removeItem($quoteItem->getId());
Mage::throwException('This is the message to the customer');
2

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

app/code/community/Brst/Test/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Brst_Test>
            <version>1.0.0.0</version>
        </Brst_Test>
    </modules>
    <global>
        <models>
            <brst_test>
                <class>Brst_Test_Model</class>
            </brst_test>
        </models>
    </global>
    <frontend>
        <layout>
            <updates>
                <brst_test>
                    <file>brst_test.xml</file>
                </brst_test>
            </updates>
        </layout>
        <events>
            <controller_action_predispatch_checkout_cart_add>
                <observers>
                    <brst_test_log_cart_add>
                        <class>brst_test/observer</class>
                        <method>logCartAdd</method>
                    </brst_test_log_cart_add>
                </observers>
            </controller_action_predispatch_checkout_cart_add>
        </events>
    </frontend>
</config>

app/code/community/Brst/Test/Model/Observer.php

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()) {
            return;
        }
        $categories = $product->getCategoryIds();
        Mage::getModel('core/session')->setProductToShoppingCart(
            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(),
            ))
        );
    }
}

app/design/frontend/default/default/layout/brst_test.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_cart_index>
        <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" />
        </reference>
    </checkout_cart_index>
</layout>

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
    </script>
    <?php Mage::getModel('core/session')->unsProductToShoppingCart(); ?>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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