开发者

Magento Products Comparison limit

I need to limit the number 开发者_Go百科of products added to compare in Magento. Only wanted to have a maximum of 4 products to be compared.

I'm thinking of doing it in the .phtml (where item display looping is) but have no idea where I should edit to display the message "Compare list is full". Any idea?

Thanks!


I've hooked to the catalog_product_compare_add_product event.

Here is my solution:

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

  2. 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>
    
  3. Helper

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

    File contents:

    <?php
    
    class Company_Catalog_Helper_Observer extends Mage_Catalog_Helper_Data {
    
        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();
            $session->addNotice($this->__('You have reached the limit of products to compare. Remove one and try again.'));
        } 
    
    } 
    
  4. 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>
    
  5. 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.


compare items are added in Mage_Catalog_Product_CompareController and you can see that there are events dispatched that you can hook your observer to or you can add your limits by extending Mage_Catalog_Model_Product_Compare_List and overriding addProduct() or addProducts() methods or even make this in collection classes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜