Magento Custom Module for add-to-cart Not Working - Observer TriggeredObserver not firingAdd To Cart Not WorkingHow to change product name in quote item dynamicallyrewrite block not workingMage_Log visitor observer not triggeredSimple Observer not firing on eventMagento Add to Cart button not workingAdd to cart not working in mozilla in custom themeAdd to cart Not Working HomepageCustom price in cart not working in Magento 2.3.1 observerHow to add condition in add-to-cart button function? Magento 1.9

Does a Globe of Invulnerability spell block outsiders from teleporting inside with a spell?

Is this car delivery via Ebay Motors on Craigslist a scam?

Is this standard Japanese employment negotiations, or am I missing something?

How to get the speed of my spaceship?

Is conquering your neighbors to fight a greater enemy a valid strategy?

Will Jimmy fall off his platform?

Why do people prefer metropolitan areas, considering monsters and villains?

How to reclaim personal item I've lent to the office without burning bridges?

Groups where no elements commute except for the trivial cases

How did Einstein know the speed of light was constant?

How complicated can a finite double complex over a field be?

Is it acceptable that I plot a time-series figure with years increasing from right to left?

Alternative to Willpower in Fighting Cravings

Shipped package arrived - didn't order, possible scam?

Why do Klingons use cloaking devices?

How serious is plagiarism in a master’s thesis?

Why no parachutes in the Orion AA2 abort test?

What is the fundamental difference between catching whales and hunting other animals?

Array or vector? Two dimensional array or matrix?

How to play a D major chord lower than the open E major chord on guitar?

Any way to meet code with 40.7% or 40.44% conduit fill?

What do I need to see before Spider-Man: Far From Home?

Why would "dead languages" be the only languages that spells could be written in?

Attach a visible light telescope to the outside of the ISS



Magento Custom Module for add-to-cart Not Working - Observer Triggered


Observer not firingAdd To Cart Not WorkingHow to change product name in quote item dynamicallyrewrite block not workingMage_Log visitor observer not triggeredSimple Observer not firing on eventMagento Add to Cart button not workingAdd to cart not working in mozilla in custom themeAdd to cart Not Working HomepageCustom price in cart not working in Magento 2.3.1 observerHow to add condition in add-to-cart button function? Magento 1.9






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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?










share|improve this question

















This question has an open bounty worth +50
reputation from zus ending ending at 2019-07-05 04:35:22Z">in 50 minutes.


Looking for an answer drawing from credible and/or official sources.
















  • 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











  • accept answer if it helpful to you

    – Ketan Borada
    21 hours ago

















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?










share|improve this question

















This question has an open bounty worth +50
reputation from zus ending ending at 2019-07-05 04:35:22Z">in 50 minutes.


Looking for an answer drawing from credible and/or official sources.
















  • 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











  • accept answer if it helpful to you

    – Ketan Borada
    21 hours ago













7












7








7


2






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?










share|improve this question
















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?







magento-1.9 addtocart restriction






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 at 6:58







zus

















asked Jun 25 at 9:43









zuszus

501 silver badge20 bronze badges




501 silver badge20 bronze badges






This question has an open bounty worth +50
reputation from zus ending ending at 2019-07-05 04:35:22Z">in 50 minutes.


Looking for an answer drawing from credible and/or official sources.








This question has an open bounty worth +50
reputation from zus ending ending at 2019-07-05 04:35:22Z">in 50 minutes.


Looking for an answer drawing from credible and/or official sources.














  • 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











  • accept answer if it helpful to you

    – Ketan Borada
    21 hours ago

















  • 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











  • accept answer if it helpful to you

    – Ketan Borada
    21 hours ago
















did you get anything in the log file.?

– nishu
Jun 26 at 9:50





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





No, there is no any error in log file?

– zus
Jun 26 at 10:56













accept answer if it helpful to you

– Ketan Borada
21 hours ago





accept answer if it helpful to you

– Ketan Borada
21 hours ago










2 Answers
2






active

oldest

votes


















3














appetcmodulesBrst_Test.xml



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


appcodecommunityBrstTestetcconfig.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>


appcodecommunityBrstTestModelObserver.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






share|improve this answer

























  • 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


















3















It should Work according to code.




Try this if your not works




  • Ovverride appcodecoreMageCheckoutModelCart.php to



     appcodelocalMageCheckoutModelCart.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






share|improve this answer

























  • 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













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279514%2fmagento-custom-module-for-add-to-cart-not-working-observer-triggered%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














appetcmodulesBrst_Test.xml



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


appcodecommunityBrstTestetcconfig.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>


appcodecommunityBrstTestModelObserver.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






share|improve this answer

























  • 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















3














appetcmodulesBrst_Test.xml



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


appcodecommunityBrstTestetcconfig.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>


appcodecommunityBrstTestModelObserver.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






share|improve this answer

























  • 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













3












3








3







appetcmodulesBrst_Test.xml



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


appcodecommunityBrstTestetcconfig.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>


appcodecommunityBrstTestModelObserver.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






share|improve this answer















appetcmodulesBrst_Test.xml



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


appcodecommunityBrstTestetcconfig.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>


appcodecommunityBrstTestModelObserver.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







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 28 at 10:34

























answered Jun 27 at 12:36









Vijay KhiradeVijay Khirade

3901 silver badge7 bronze badges




3901 silver badge7 bronze badges












  • 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

















  • 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
















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






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





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





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





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





Still adding other products.

– zus
Jun 28 at 9:04













3















It should Work according to code.




Try this if your not works




  • Ovverride appcodecoreMageCheckoutModelCart.php to



     appcodelocalMageCheckoutModelCart.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






share|improve this answer

























  • 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















3















It should Work according to code.




Try this if your not works




  • Ovverride appcodecoreMageCheckoutModelCart.php to



     appcodelocalMageCheckoutModelCart.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






share|improve this answer

























  • 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













3












3








3








It should Work according to code.




Try this if your not works




  • Ovverride appcodecoreMageCheckoutModelCart.php to



     appcodelocalMageCheckoutModelCart.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






share|improve this answer
















It should Work according to code.




Try this if your not works




  • Ovverride appcodecoreMageCheckoutModelCart.php to



     appcodelocalMageCheckoutModelCart.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







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 1 at 10:08

























answered Jun 28 at 6:42









Ketan BoradaKetan Borada

84111 silver badges40 bronze badges




84111 silver badges40 bronze badges












  • 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

















  • 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
















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





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





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





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





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





working good for me try to debugging loom.com/share/d7501c1428a14e96ae887ff514db6a76

– Ketan Borada
Jul 1 at 6:55

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279514%2fmagento-custom-module-for-add-to-cart-not-working-observer-triggered%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?