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?

asked Jun 25, 2019 at 9:43
2
  • did you get anything in the log file.? Commented Jun 26, 2019 at 9:50
  • No, there is no any error in log file? Commented Jun 26, 2019 at 10:56

2 Answers 2

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

answered Jun 28, 2019 at 6:42
8
  • 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 Commented Jul 1, 2019 at 5:14
  • i have update my answer, added line Mage::getModel('checkout/cart')->getQuote()->setHasError(true); Commented Jul 1, 2019 at 6:20
  • still same error. Product not add but error message not shown. Commented Jul 1, 2019 at 6:41
  • code : pastiebin.com/5d19ac92c8eb5 Commented Jul 1, 2019 at 6:48
  • working good for me try to debugging loom.com/share/d7501c1428a14e96ae887ff514db6a76 Commented Jul 1, 2019 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.