0

Condition: I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

Workout: Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like If you want this product, Purchase alone not mixed with other Products

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like Cart has Special Product you can not add another

code :

app/etc/modules/Gta_KolupadiRestrict.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <!-- Gta_KolupadiRestrict -->
        <Gta_KolupadiRestrict>
            <active>true</active>
            <codePool>local</codePool>
        </Gta_KolupadiRestrict>
    </modules>
</config>

app/code/local/Gta/KolupadiRestrict/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Gta_KolupadiRestrict>
            <version>1.0.0</version>
        </Gta_KolupadiRestrict>
    </modules>
    <global> 
        <models>
            <gta_kolupadirestrict>
                <class>Gta_KolupadiRestrict_Model</class>
            </gta_kolupadirestrict>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Gta_KolupadiRestrict_Model_Observer>   
                        <type>singleton</type>
                        <class>Gta_KolupadiRestrict_Model_Observer</class>
                        <method>cartevent</method>
                    </Gta_KolupadiRestrict_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>      
        </events>
    </global>   
</config>

app/code/local/Gta/KolupadiRestrict/Model/Observer.php

    <?php 
    // Mage::log('fine dude', null, 'logfile.log');
    //create class

    class Gta_KolupadiRestrict_Model_Observer
    {

        //create function
        public function cartevent(Varien_Event_Observer $observer)
        {
            // $category_id = array(680, 894, 895) ; //category ids

            // $category_products  = Mage::getModel('catalog/category')
            //                  ->addAttributeToFilter('category_id', array('in' => array('680','894','895')))
            //                  ->setWebsiteId(2);              // load website id  

            $product = $observer->getProduct();

            $category_id = $product->getCategoryIds();
            $productCatId  = Mage::getModel('catalog/category')
                                ->setWebsiteId(2)        // load website id
                                ->load($category_id);        // load category 

            // check cart qty status                            
            $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();  

            //logic
            if($productCatId->getId()=='680' && $cart_qty > 0  ) //680 category id
            {
                Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");    
            }

             //check if cart have products 
             $quote = Mage::getModel('checkout/cart')->getQuote();

            foreach($quote->getAllItems() as $item)
            {
                $productCategoryId  = $item->getCategoryIds();

                if($productCategoryId =='680')  //680 category id
                {
                    Mage::throwException("Cart has Special Product you can not add another");
                }
            }    
        }       
    }
?>

Observer not trigger. Does anyone help me?

  • can you change the event to controller_action_predispatch_checkout_cart_add and recheck? – Mohit Kumar Arora Aug 5 at 13:58
  • @MohitKumarArora Just i test observer working or not, observer not working, update with your code -> justpaste.it/4qf3f observer.php -> justpaste.it/5w87x log file not created – zus Aug 6 at 8:02
  • I also checked in the latest Magento. It is not working either. I also tried other events but no success. – Mohit Kumar Arora Aug 6 at 9:43
  • FYI finally found the error, Actually case sensitive codepool to codePool, now working. – zus Aug 6 at 10:00
  • I was also going to post my answer pointing the same error. – Mohit Kumar Arora Aug 6 at 10:01
1

In config.xml:

<class>Kolupadi_Restrict_Model_Observer</class>

should be

<class>Gta_KolupadiRestrict_Model_Observer</class>

Change your app/etc/modules/Gta_KolupadiRestrict.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Gta_KolupadiRestrict>
            <active>true</active>
            <codePool>local</codePool>
        </Gta_KolupadiRestrict>
    </modules>
</config>

Clear cache.

[update]

config.xml looks like:

<?xml version="1.0"?>
<config>
    <modules>
        <Gta_KolupadiRestrict>
            <version>1.0.0</version>
        </Gta_KolupadiRestrict>
    </modules>
    <global>
        <models>
            <gta_kolupadirestrict>
                <class>Gta_KolupadiRestrict_Model</class>
            </gta_kolupadirestrict>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <gta_kolupadirestrict_checkout_cart_product_add_after>
                        <class>gta_kolupadirestrict/observer</class>
                        <method>cartevent</method>
                    </gta_kolupadirestrict_checkout_cart_product_add_after>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </global>
</config>

app/code/local/Gta/KolupadiRestrict/Model/Observer.php

<?php

class Gta_KolupadiRestrict_Model_Observer
{
    public function cartevent($observer)
    {
        error_log('hey');
    }
}

Now you can see log for every add to cart press.

  • still getting same error – zus Aug 3 at 12:23
  • Mage::log('fine dude', null, 'logfile.log'); -> remove from top – Sohel Rana Aug 3 at 12:59
  • Check updated answer – Sohel Rana Aug 3 at 13:34
  • Still Not working. – zus Aug 5 at 6:45
  • Observer testing error : justpaste.it/7hsyp – zus Aug 6 at 5:38
0

Please check with below url :

Observer not firing

I hope its work for you..

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.