1

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

Code:

<?xml version="1.0" ?>
<config>
    <modules>
        <Stackexchange_Magento38261>
            <version>1.0.0</version>
        </Stackexchange_Magento38261>
    </modules>

    <global>
        <models>
            <magento38261>
                <class>Stackexchange_Magento38261_Model</class>
            </magento38261>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch_catalog_product_compare_add>
                <observers>
                    <prevent_add_to_compare>
                        <class>magento38261/observer</class>
                        <method>preventadd</method>
                    </prevent_add_to_compare>
                </observers>
            </controller_action_predispatch_catalog_product_compare_add>
        </events>
    </frontend>
</config>

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

Code:

<?php
class Stackexchange_Magento38261_Model_Observer{
    public function preventadd($observer){

          $controller = $observer->getControllerAction();

         $items=Mage::getResourceModel('catalog/product_compare_item_collection')
                ->useProductItem(true)
                ->setStoreId(Mage::app()->getStore()->getId());
             if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
            }else {
            $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
            }
            /* send error */
            if(count($items)>3){
             Mage::getSingleton('catalog/session')->addError(Mage::helper('catalog')->__('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();
            }
             $controller->getResponse()->setRedirect($refererUrl);

            }


            return $this;

    }
}

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

code:

<?xml version="1.0" ?>
<config>
    <modules>
        <Stackexchange_Magento38261>
            <codePool>local</codePool>
            <active>true</active>
        </Stackexchange_Magento38261>
    </modules>
</config>

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"? – Fabian Blechschmidt Jan 30 '15 at 8:46
  • I updated my question, was to soon with placing the question – Henk Z Jan 30 '15 at 8:49
0

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, privacy policy and cookie policy

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