7

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

My Module :

app/etc/modules/Brst_Test.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Brst_Test>
            <active>true</active>
            <codePool>community</codePool>
        </Brst_Test>
    </modules>
</config>

This is my observer file

app/code/community/Brst/Test/Model/Observer.php

<?php
ini_set('display_errors', '1');

// Mage::log('fine dude', null, 'logfile.log');
class Brst_Test_Model_Observer
{
    //Put any event as per your requirement
    public function logCartAdd($observer) {
        // Mage::log('good dude', null, 'logfile.log');


        $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::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $productId = $item->getProductId();
            if($productId==31588){
                Mage::throwException("Cart has Special Product you can not add another");
            }
        }
    }
}
?>

app/code/community/Brst/Test/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Brst_Test>
            <version>1.0.0</version>
        </Brst_Test>
    </modules>
    <global>
        <models>
             <brst_test>
                <class>Brst_Test_Model</class>
             </brst_test>
        </models>
    </global>
    <frontend>
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <Brst_Test_Model_Observer>
                            <type>singleton</type>
                            <class>Brst_Test_Model_Observer</class>
                            <method>logCartAdd</method>
                        </Brst_Test_Model_Observer>
                    </observers>
                </checkout_cart_product_add_after>
            </events>
    </frontend>
</config>

Not working, how to solve the error?

  • did you get anything in the log file.? – nishu Jun 26 at 9:50
  • No, there is no any error in log file? – zus Jun 26 at 10:56
3
+25

It should Work according to code.

Try this if your not works

  • Ovverride \app\code\core\Mage\Checkout\Model\Cart.php to

       \app\code\local\Mage\Checkout\Model\Cart.php
    
  • Find function addProduct($productInfo, $requestInfo=null) in your ovveride file, product add logic written in there

  • add code after line `$request = $this->_getProductRequest($requestInfo);

`

public function addProduct($productInfo, $requestInfo=null)
    {

        $product = $this->_getProduct($productInfo);
        $request = $this->_getProductRequest($requestInfo);

        /* ===========Restricted Product Coding Start==========        */
        $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
        $restrictedIds = array(1,2,3); //add restricted product ids here
        if (in_array($product->getId(), $restrictedIds) && $cart_qty > 0) {
            Mage::getSingleton('core/session')->addError('You can not add This special Product, empty cart before add it');
           Mage::getModel('checkout/cart')->getQuote()->setHasError(true);
            return false;
        }

        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $productId = $item->getProductId();
            if(in_array($productId, $restrictedIds)){
                Mage::getSingleton('core/session')->addError('Cart has Special Product you can not add another');
           Mage::getModel('checkout/cart')->getQuote()->setHasError(true);
                return false;
            }
        }

        /* ===========Restricted Product Coding End ==========        */

        /** @var Mage_Catalog_Helper_Product $helper */
        $helper  = Mage::helper('catalog/product');
       .
       .
       .
       .
       .
}

Note : You can call observer overthere and put logic in your observer if you dont want to code in that function

  • Working good, but error error message not shown, eg: if i try add restricted product consider cart already have product, if click restricted product add-to-cart button message like product added to cart successfully. FYI -> product not added to cart, iis looking good, my only issue error message not shown properly. @Ketan Borada – zus Jul 1 at 5:14
  • i have update my answer, added line Mage::getModel('checkout/cart')->getQuote()->setHasError(true); – Ketan Borada Jul 1 at 6:20
  • still same error. Product not add but error message not shown. – zus Jul 1 at 6:41
  • code : pastiebin.com/5d19ac92c8eb5 – zus Jul 1 at 6:48
  • working good for me try to debugging loom.com/share/d7501c1428a14e96ae887ff514db6a76 – Ketan Borada Jul 1 at 6:55
2

app\etc\modules\Brst_Test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Brst_Test>
                <active>true</active>
                <codePool>community</codePool>
            </Brst_Test>
        </modules>
    </config>

app\code\community\Brst\Test\etc\config.xml

        <?xml version="1.0"?>
        <config>
            <modules>
                <Brst_Test>
                    <version>1.0.0</version>
                </Brst_Test>
            </modules>
            <global>
                <models>
                     <brsttest>
                        <class>Brst_Test_Model</class>
                     </brsttest>
                </models>
            </global>
            <frontend>
                    <events>
                            <controller_action_predispatch_checkout_cart_add>
                                <observers>
                                    <brsttest>
                                        <type>singleton</type>
                                        <class>brsttest/observer</class>
                                        <method>logCartAdd</method>
                                    </brsttest>
                                </observers>
                            </controller_action_predispatch_checkout_cart_add>
                        </events>
            </frontend>
        </config>

app\code\community\Brst\Test\Model\Observer.php

    <?php
    class Brst_Test_Model_Observer extends Mage_Core_Model_Abstract
    {

        public function logCartAdd($observer){

            //I am change this
            $id = Mage::app()->getFrontController()->getRequest()->getParam('product');

            $product = Mage::getModel('catalog/product')->load($id);

            $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

            // I am change this
            if ($product->getId() == '31588' && $cart_qty > 0) {
                Mage::getSingleton('core/session')->addError('You can not add This special Product, empty cart before add it');
                $url = Mage::getModel('core/url')->getUrl("checkout/cart");
                Mage::app()->getResponse()->setRedirect($url);
                Mage::app()->getResponse()->sendResponse();
                exit;
            }

           $quote = Mage::getModel('checkout/cart')->getQuote();

            foreach ($quote->getAllItems() as $item) {
                $productId = $item->getProductId();
                if($productId ==  '31588'){
                    Mage::getSingleton('core/session')->addError('Cart has Special Product you can not add another');
                    $url = Mage::getModel('core/url')->getUrl("checkout/cart");
                    Mage::app()->getResponse()->setRedirect($url);
                    Mage::app()->getResponse()->sendResponse();
                    exit;

                }
            }


        }
    }

Try this code

  • Thanks for your reply, not working still other products added to cart if restricted products available in cart. code -> pastiebin.com/5d1597f887f17 @Vijay-CyberLocker – zus Jun 28 at 4:29
  • In your code also same, observer triggered, but condition not working @Vijay-CyberLocker – zus Jun 28 at 7:10
  • Ok brother give me few minutes i will update my answer. – Vijay Khirade Jun 28 at 7:35
  • i just want to know if my class right in observer class : <class>brst_test/observer</class> in config.xml – zus Jun 28 at 7:39
  • Still adding other products. – zus Jun 28 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.