2

I need to limit the number of products added to compare in Magento. Lets say 3 products.

2
  • K,,............. – Amit Bera Oct 10 '14 at 15:20
  • 1
    please accept the answer if it was helpful to you so it can be helpful for future readers – Dexter Oct 11 '14 at 7:44
3

hi you can get current customer/visitor by below code:

Step1: get Compare product collection using resource model

$items=Mage::getResourceModel('catalog/product_compare_item_collection')
                ->useProductItem(true)
                ->setStoreId(Mage::app()->getStore()->getId());

Step2: Then filter this collection by current visitor/ customer which will give your current compare list

   if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
    }else {
    $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
    }

Finally this code $items current exiting product list

Step3: Magento is add compare product using Mage_Catalog_Product_CompareController at function addAction and then using controller_action_predispatch_catalog_product_compare_add

check current no of compare products and redirect to your list page;

xml code for observer:

<global>
   <events>
    <controller_action_predispatch_catalog_product_compare_add>
     <observers>
            <controller_action_before>
                <class>youmodeltype/yourfunction</class>
                <method>myFunction</method>
            </controller_action_before>
        </observers>
     </controller_action_predispatch_catalog_product_compare_add>
   </events>
</global>

See: Overriding a core controller's preDispatch method

and Observer code is

<php
class YourmoduleNameSapce_YourmoduldeName_Model_Observer
{
    public function myFunction($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('customer/session')->addError(Mage::helper('captcha')->__('Error.'));
            $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
            $controller->getResponse()->setRedirect($url);
            
            }
            return $this;


    }

}

Edit:

[FULL MOdule] According to your request i have put extension please check

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

code is

<?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 and where you have prevent to add compare list:

and code is:

<?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 is:

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

This code have checked and work perfectly on my machine

1
  • 1
    Perfect amit.. Jst went through code and it was amazing.. – Pavan Kumar Oct 11 '14 at 6:41

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