Observer & logic working fine but error message not shown 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");
}
}
}
}
}
?>
Observer & my logic working fine, but observer not throw exception message instead of keep loading add-to-cart. How to show exception if condition match?
I have tried your code in my module, and it works fine!
I post the modified code there with all the die(); for check the correct paths:
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");
}
}
}
}
}
You may check your product's settings. I post my configs too, maybe you just missed the right handler. It would be nice to see all your module.
<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>