I want to set a max amount of products that can be added to the compare list. I want to set the max at 3 products that can be compare.

Tried this code:

Step1:Create config.xml at app>code>local>Stackexchange>Magento38261>etc


<?xml version="1.0" ?>


Step2: create Observer.php at app>code>local>Stackexchange>Magento38261>Helper


class Stackexchange_Magento38261_Model_Observer{
    public function preventadd($observer){

          $controller = $observer->getControllerAction();

             if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            }else {
            /* send error */
            $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
           // $controller->getResponse()->setRedirect(Mage::getUrl());

            /* start to redirec to lst page */
            $refererUrl = Mage::app()->getRequest()->getServer('HTTP_REFERER');
            if ($url = Mage::app()->getRequest()->getParam('referer_url')) {
                $refererUrl = $url;
            if ($url =Mage::app()->getRequest()->getParam('r64')) {
                $refererUrl = Mage::helper('core')->urlDecode($url);
            if ($url =Mage::app()->getRequest()->getParam('uenc')) {
                $refererUrl = Mage::helper('core')->urlDecode($url);

            if (empty($refererUrl)) {
                $refererUrl =Mage::getBaseUrl();


            return $this;


Step3:module file Stackexchange_Magento38261.xml at app\etc\modules


<?xml version="1.0" ?>

But this code does not work, because I still can add more than 3 products to the compare list.

  • First: What do you want to achieve. Second: What do you mean by "did not work"? Jan 30, 2015 at 8:46
  • I updated my question, was to soon with placing the question
    – JGeer
    Jan 30, 2015 at 8:49

1 Answer 1


The controller_action_predispatch_catalog_product_compare_add event is triggered before the action is executed.

So to check the item count at that point you have to use count($items) + $itemCountToBeAdded, where $itemCountToBeAdded is based on the request (how many products is the user trying to add)

Also it is not enough to set a redirection, you also have to tell Magento not to execute the action:

$controller->setFlag('', Mage_Core_Controller_Front_Action::FLAG_NO_DISPATCH, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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