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– nishu2019年06月26日 09:50:31 +00:00Commented Jun 26, 2019 at 9:50
-
No, there is no any error in log file?zus– zus2019年06月26日 10:56:01 +00:00Commented Jun 26, 2019 at 10:56
2 Answers 2
It should Work according to code.
Try this if your not works
Ovverride
\app\code\core\Mage\Checkout\Model\Cart.phpto\app\code\local\Mage\Checkout\Model\Cart.phpFind function
addProduct($productInfo, $requestInfo=null)in your ovveride file, product add logic written in thereadd 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 Boradazus– zus2019年07月01日 05:14:09 +00:00Commented Jul 1, 2019 at 5:14
-
i have update my answer, added line
Mage::getModel('checkout/cart')->getQuote()->setHasError(true);Ketan Borada– Ketan Borada2019年07月01日 06:20:24 +00:00Commented Jul 1, 2019 at 6:20 -
still same error. Product not add but error message not shown.zus– zus2019年07月01日 06:41:48 +00:00Commented Jul 1, 2019 at 6:41
-
code : pastiebin.com/5d19ac92c8eb5zus– zus2019年07月01日 06:48:23 +00:00Commented Jul 1, 2019 at 6:48
-
working good for me try to debugging loom.com/share/d7501c1428a14e96ae887ff514db6a76Ketan Borada– Ketan Borada2019年07月01日 06:55:05 +00:00Commented Jul 1, 2019 at 6:55
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-CyberLockerzus– zus2019年06月28日 04:29:39 +00:00Commented Jun 28, 2019 at 4:29
-
In your code also same, observer triggered, but condition not working @Vijay-CyberLockerzus– zus2019年06月28日 07:10:16 +00:00Commented Jun 28, 2019 at 7:10
-
Ok brother give me few minutes i will update my answer.user55776– user557762019年06月28日 07:35:47 +00:00Commented 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.xmlzus– zus2019年06月28日 07:39:10 +00:00Commented Jun 28, 2019 at 7:39
-