0

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 other products 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;
            }
        }
    }  

If try to add cart in website, gettin 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";}
2
  • the obsever is call and error is through from this file Commented Aug 7, 2019 at 10:54
  • could you pls explain in brief, i am new in observer, any help to solve the error.
    – zus
    Commented Aug 7, 2019 at 10:58

1 Answer 1

0

Please use the below code

 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::getSingleton('core/session')->addError("You can not add This special Product, empty cart before add it");
                $url = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer() : Mage::getUrl();
                Mage::app()->getFrontController()->getResponse()->setRedirect($url);
                Mage::app()->getResponse()->sendResponse();
                return Mage::app()->getFrontController()->getResponse()->getRedirect($url);;


        }

    // check if other products add to cart
        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $prdId = $item->getProductId();
            if ($prdId != $prodID) {
                $_product1 = $item->getProduct();


                $categoryIds1 = $_product1->getCategoryIds();
                if (in_array(681, $categoryIds1)) {

                    Mage::getSingleton('core/session')->addError("If you want Kolu Padi, Purchase alone not mixed with other Products");
                    $url = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer() : Mage::getUrl();
                    Mage::app()->getFrontController()->getResponse()->setRedirect($url);
                    Mage::app()->getResponse()->sendResponse();
                    return Mage::app()->getFrontController()->getResponse()->getRedirect($url);;

                }
            }

        }
    }
9
  • Working partially, one condition working, like if cart has restricted category of products observer not allow add other category of products to cart, but another condition if cart has other category of products don't allow restricted category of products not working.
    – zus
    Commented Aug 7, 2019 at 11:16
  • you need to set the logic for that in your own way, ill give you the solution for the error occur. Commented Aug 7, 2019 at 11:17
  • i add one new condition in your logic if ($prdId != $prodID) { you can remove that if or comment that and check Commented Aug 7, 2019 at 11:19
  • if you get the solution than please accept and up vote my answer Commented Aug 7, 2019 at 11:31
  • Still getting error, 503 Service Unavailable
    – zus
    Commented Aug 7, 2019 at 11:35

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.