0

I just want to restrict some of the category of products to purchase with other category products,

Workout :

If customer click add to cart using observer compare current add-to-cart product category id & already cart products category id, if the current product category id and cart products category id different trigger observer & display message like, You can not add This special Product, empty cart before add it,

and also vice versa, if cart has other category of products, if customer try to add restricted category products throw exception like If you want Kolu Padi, Purchase alone not mixed with other Products

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>

my observer :

    <?php
class Gta_KolupadiRestrict_Model_Observer {

    public function cartevent(Varien_Event_Observer $observer) {

        // Load product
        $product = $observer->getProduct();
        $prodID = $product->getId();
        $_product = Mage::getModel('catalog/product')->load($prodID);

        // get category id
        $categoryIds = $_product->getCategoryIds();

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

        // check conditiion cart has other products 
        if(in_array(681, $categoryIds) && $cart_qty > 0) {
            Mage::throwException("You can not add This special Product, empty cart before add it");
        }

        // check if restricted category of products try to  add to cart
        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $_product1 = $item->getProduct();

            $categoryIds1 = $_product1->getCategoryIds();
            if(in_array(681, $categoryIds1)) {
                Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
                break;
            }
        }
    }   
}
?>

Error :

a:5:{i:0;s:67:"If you want Kolu Padi, Purchase alone not mixed with other Products";i:1;s:1835:"#0 /home/abc/public_html/app/code/local/Gta/KolupadiRestrict/Model/Observer.php(21): Mage::throwException('If you want Kol...')
#1 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(1358): Gta_KolupadiRestrict_Model_Observer->cartevent(Object(Varien_Event_Observer))
#2 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Gta_KolupadiRestrict_Model_Observer), 'cartevent', Object(Varien_Event_Observer))
#3 /home/abc/public_html/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('checkout_cart_p...', Array)
#4 /home/abc/public_html/app/code/core/Mage/Checkout/Model/Cart.php(290): Mage::dispatchEvent('checkout_cart_p...', Array)
#5 /home/abc/public_html/app/code/local/Cmsmart/AjaxCart/controllers/IndexController.php(315): Mage_Checkout_Model_Cart->addProduct('5071', Array)
#6 /home/abc/public_html/app/code/local/Cmsmart/AjaxCart/controllers/IndexController.php(133): Cmsmart_AjaxCart_IndexController->tryaddAction(Object(Mage_Catalog_Model_Product), Array)
#7 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Cmsmart_AjaxCart_IndexController->indexAction()
#8 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#9 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#10 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#11 /home/abc/public_html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#12 /home/abc/public_html/australia/index.php(93): Mage::run('australia', 'website')
#13 {main}";s:3:"url";s:100:"/ajaxcart/index/index/?form_key=MtlJbvKkqPQKCLJR&product=5071&related_product=&qty=1&_=1565155804563";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:9:"australia";}
5
  • does your observer work fine? can you please try the below code in your config file. <events> <checkout_cart_product_add_after> <observers> <checkout_cart_product_add_after_handler> <type>singleton</type> <class>kolupadirestrict/observer</class> <method>cartevent</method> <args></args> </checkout_cart_product_add_after_handler> </observers> </checkout_cart_product_add_after> </events> Aug 7, 2019 at 12:28
  • If i update with your code, still both of the category of products adding to add to cart.
    – zus
    Aug 7, 2019 at 12:42
  • My code is not for logic its just for checking your observer is working or not, so now its working fine then I will check your observer code and let you know if anything. Aug 8, 2019 at 4:27
  • Yes, my observer working
    – zus
    Aug 8, 2019 at 5:02
  • @AasimGoriya Any help?
    – zus
    Aug 8, 2019 at 10:32

2 Answers 2

1

I don't know wether your conditions work, I leave that up to you. The problem with the error comes - I assume - because the addProduct() is not inside a try/catch block.

The default Magento Controller looks like this:

\Mage_Checkout_CartController::addAction
public function addAction()
{
    /* ... */
    try {
        /* ... */
        $cart->addProduct($product, $params);
        /* ... */

    } catch (Mage_Core_Exception $e) {
        /* Handle Magento Exception */
    } catch (Exception $e) {
        /* ... */        
    }
}

So I would check the extension, because it looks horrible and broken: https://github.com/roshu1980/add-computers/blob/446b3bea101c239b8e93a39131cb484221bf5437/app/code/local/Cmsmart/AjaxCart/controllers/IndexController.php#L151-L156

0

The line $categoryIds = $_product->getCategoryIds(); will give you category IDs in an array format.

So there is no need to use:

if($categoryIds->getCategoryIds()=='681' && $cart_qty > 0  )

instead, use:

if(in_array(681, $categoryIds) && $cart_qty > 0)

Update after checking the observer's code:

Please try this code:

<?php
class Gta_KolupadiRestrict_Model_Observer {

    public function cartevent(Varien_Event_Observer $observer) {
        $product = $observer->getProduct();
        $prodID = $product->getId();
        $_product = Mage::getModel('catalog/product')->load($prodID);
        $categoryIds = $_product->getCategoryIds();
        $cart_qty = (int)Mage::getModel('checkout/cart')->getQuote()->getItemQty();
        // foreach ($categoryIds as $category) {


        if(in_array(681, $categoryIds) && $cart_qty > 0) {
            Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
        }


        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $_product1 = $item->getProduct();
            // $productCategoryId = $item->getCategoryIds();

                // if ($productCategoryId == '681')  //680 category id
                // {
                //  Mage::throwException("Cart has Special Product you can not add another");
                // }
            $categoryIds1 = $_product1->getCategoryIds();
            if(in_array(681, $categoryIds1)) {
                Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
                break;
            }
        }
        // }
    }     
}
?>
12
  • yes,thank you for your help, but both of my condition not working. observer-> pastiebin.com/5d496e619d3f1 FYI no error.
    – zus
    Aug 6, 2019 at 12:11
  • Did the first one work for you? What is actually happening after changing the condition shown above? Aug 6, 2019 at 12:44
  • Yes, there no error and also logic not appplied
    – zus
    Aug 6, 2019 at 12:46
  • my exact observer -> paste.ofcode.org/KyEnmJCiuR6r5hmNcdAirR
    – zus
    Aug 6, 2019 at 12:47
  • @zus, please check my updated answer. This is not tested code and is intended to help you understand the basic idea. Aug 6, 2019 at 12:59

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.