观察者 Mage::throwException 消息未显示给客户

Observer Mage::throwException message Not show to customer

观察者和逻辑工作正常,但错误消息未显示给客户。

<?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()->getItemsQty();

        if(in_array(681, $categoryIds))
        {
            error_log("line 31-".$categoryIds[0]);
            $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");
                  }
            }
        }elseif(!in_array(681, $categoryIds)){
            error_log("line 31-".$categoryIds[0]);
            $quote = Mage::getModel('checkout/cart')->getQuote();
            foreach ($quote->getAllItems() as $item) {
                $_product1 = $item->getProduct();

                $categoryIds1 = $_product1->getCategoryIds();
                error_log("line 34-".$item->getProductId()."-".$categoryIds1[0]);
                if(in_array(681, $categoryIds1)) {
                    Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
                 }


            }

        }
    }   
}
?>

观察者和我的逻辑工作正常,但观察者不会抛出异常消息,而是继续加载添加到购物车。如果条件匹配,如何显示异常?

我已经在我的模块中尝试了你的代码,它工作正常!

我 post 修改后的代码有所有的 die();检查正确的路径:

    public function test(Varien_Event_Observer $observer) {

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

        // get category id
        $categoryIds = $_product->getCategoryIds();
        echo print_r($categoryIds); // category: #3
//        die(" - 2 - ");
        if(in_array(3, $categoryIds))
        {
//          die(" - 3 - ");
            error_log("line 31-".$categoryIds[0]);
            $quote = Mage::getModel('checkout/cart')->getQuote();

            foreach ($quote->getAllItems() as $item) {

                $_product1 = $item->getProduct();
                $categoryIds1 = $_product1->getCategoryIds();

                if(!in_array(5, $categoryIds1)) {
                    Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
//                  die(" - 4 - ");
                }

            }

        }elseif(!in_array(3, $categoryIds)){
//          die(" - 5 - ");
            error_log("line 31-".$categoryIds[0]);
            $quote = Mage::getModel('checkout/cart')->getQuote();
            foreach ($quote->getAllItems() as $item) {
                $_product1 = $item->getProduct();
//              die(" - 6 - ");
                $categoryIds1 = $_product1->getCategoryIds();
                error_log("line 34-".$item->getProductId()."-".$categoryIds1[0]);
                if(in_array(6, $categoryIds1)) {
                    Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
                }


            }

        }
    }
}

此代码有效。

您可以检查产品的设置。 我也 post 我的配置,也许你只是错过了正确的处理程序。很高兴看到您的所有模块。

<global>
    <models>
        <test_eventslist>
            <class>Test_Eventslist_Model</class>
        </test_eventslist>
    </models>

<events>
    <checkout_cart_product_add_after>
        <observers>
            <Test_Eventslist>
                <type>singleton</type>
                <class>test_eventslist/observer</class>
                <method>test</method>
            </Test_Eventslist>
        </observers>
    </checkout_cart_product_add_after>
</events>
</global>