Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

Magento set limit in Compare product

Create module.

Directories :
app/code/local/Company/this can be any name
app/code/local/Company/Catalog
app/code/local/Company/Catalog/Helper
app/code/local/Company/Catalog/etc
Module config

Create a file: app/code/local/Company/Catalog/etc/config.xml
File contents:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Catalog>
            <version>0.1</version>
        </Company_Catalog>
    </modules>
    <frontend>
        <events>
            <catalog_product_compare_add_product>
                <observers>
                    <company_catalog>
                        <type>singleton</type>
                        <class>Company_Catalog_Helper_Observer</class>
                        <method>limitProductCompare</method>
                    </company_catalog>
                </observers>
            </catalog_product_compare_add_product>
        </events>
    </frontend>
</config>

Helper
Create a file: app/code/local/Company/Catalog/Helper/Observer.php

File contents:

class Pneumatig_Catalog_Helper_Observer extends Mage_Catalog_Model_Product_Compare_List {

    /* Set your limit Ex. : 4 */
    const COMPARE_LIMIT = 4;

    function limitProductCompare($event) {
        if (Mage::helper('catalog/product_compare')->getItemCount()<self::COMPARE_LIMIT) return;

        $session = Mage::getSingleton('catalog/session');
        Mage::getSingleton('catalog/product_compare_list')->removeProduct($event->getProduct());

        $session->getMessages()->clear();
        Mage::getSingleton('core/session')->addError('You have reached the limit of products to compare. Remove one and try again.');
    }
}

Enable module
Create file: app/etc/modules/Company_Catalog.xml

File contents:

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


Profit!
Everything should work fine now. After adding, 5th product gets removed, and nice notice is displayed. Its not the perfect solution (since it removes a product after it was added), but it does the job well.

1 comment

  1. Even the code is so simple, it is not working in Magento CE 1.9.0.1. You also have a type in class declaration. It should be Compnay_Catalog_Helper_Observer instead of Pneumatig_Catalog_Helper_Observer.

    Could you please check again and provide a solution? Thank you.

    ReplyDelete