Magento set limit in Compare product

Create module.

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

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

<?xml version="1.0"?>

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('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"?>

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