Magento Own Custom Module for add-to-cart Not Working

Created module like No other products add to cart if restricted product available in cart and vice versa.

My Module :


<?xml version="1.0"?>

This is my observer file


ini_set('display_errors', '1');

// Mage::log('Hy observer called', null, 'logfile.log');
class Brst_Test_Model_Observer
    //Put any event as per your requirement
    public function logCartAdd() {
        $product = Mage::getModel('catalog/product')
                        ->load(Mage::app()->getRequest()->getParam('product', 0));
        $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

        if ($product->getId()==31588 && cart_qty > 0) {
            Mage::throwException("You can not add This special Product, empty cart before add it");

        // $quote = Mage::getSingleton('checkout/session')->getQuote();
        // if ($quote->hasProductId(2)) 
        //  Mage::getSingleton("core/session")->addError("Cart has Special Product you can not add another");
        //  return;
        // }
        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $productId = $item->getProductId();
                Mage::throwException("Cart has Special Product you can not add another");



<?xml version="1.0"?>

Not working, how to solve the error?

What exactly isn’t working?

If i add restricted product to cart, there is no other product eligible to add cart, but here other products also able to add to cart, how can i restrict if my restricted product available in cart no other products add add-to-cart.

I am new to PHP, i try module like no other products eligible to add add-to-cart if restricted product available in the cart, so i got the script from stackoverflow,the script based on magento observer. I am not able to find the error, can i get help to find the error?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.