Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Magento - Category of Products with Observer condition Error?


10:21 am on Aug 3, 2019 (gmt 0)

New User

joined:July 19, 2019
posts: 5
votes: 0

Condition: I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

Workout: Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like If you want this product, Purchase alone not mixed with other Products

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like Cart has Special Product you can not add another

code :


<?xml version="1.0" encoding="UTF-8"?>


<?xml version="1.0"?>


Mage::log('fine dude', null, 'logfile.log');
//create class
class Gta_KolupadiRestrict_Model_Observer

//create function
public function cartevent(Varien_Event_Observer $observer)
$category_id = array(680, 894, 895) ; //category ids

$category_products = Mage::getModel('catalog/category')
->setWebsiteId(2); // load website id
->load($category_id); // load category

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

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

//check if cart have products
$quote = Mage::getModel('checkout/cart')->getQuote();
foreach($quote->getAllItems() as $item)
$productId = $item->getCategoryId();

if($productId == $category_id)
Mage::throwException("Cart has Special Product you can not add another");

Observer not trigger. Does anyone help me?
2:13 pm on Aug 6, 2019 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 3

Hi aveevan,

I noticed you posted your question in the Magento forums now too. That was going to be my advice. I'm not familiar with the underlying structure of that particular software application and I'm guessing you'll get a lot more feedback on Magento-specific questions there. For what it's worth, nothing is jumping out at me here or in your Magento forum code snippets either except for the thrown and not catched error:

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

Your $categoryIds variable is an array, not an object. You are attempting to use a method on an array.