cancel
Showing results for 
Search instead for 
Did you mean: 

Observer Mage::throwException error - status 503 error

Observer Mage::throwException error - status 503 error

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 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;
            }
        }
    }   
}
?>

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";}
1 REPLY 1

Re: Observer Mage::throwException error - status 503 error

Hi @Aveeva,

 

Is your store in developer mode? (I know you're in Magento 1)

What if you change the break; for return; after the exception?

Also, maybe you can add a session message and just log the exception. I'm not sure if the best idea is to throw the execption.

And, finally, shouldn't this happend before the product is added?