1

I am developing a project with custom module of my own.There is a situation i want to filter all the products of the store with my custom logic having data provided by each user from the cookie.Other products will not be displayed in any part of the website to the same user.Whether it is related products block or from any slider etc.Which model should i overwrite in order to filter default product collection ?

  • what did you try ? – Rajeev K Tomy Nov 8 '14 at 7:37
  • Mage_Catalog_Model_Resource_Product_Collection – Ashish Nov 8 '14 at 7:38
  • what is that ?? – Rajeev K Tomy Nov 8 '14 at 7:39
  • I have tried to overwrite _applyProductLimitations() – Ashish Nov 8 '14 at 7:40
  • BUt i cant figure it out how to filter the products – Ashish Nov 8 '14 at 7:40
2

if you need to apply your own filtering in all product collection, then the best way would be listen to the event catalog_product_collection_load_before. This event will called in every product load operation.

In your module, first you need to setup this event. So add this content inside your module's config.xml

File : app/code/local/Namespace/Modulename/etc/config.xml

<config>
   <frontend>
        <events>
            <catalog_product_collection_load_before>
                <observers>
                    <namespace_modulename>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>applyCustomFilter</method>
                    </namespace_modulename>
                </observers>
            </catalog_product_collection_load_before>
        </events>
    </frontend>
    <global>
        <models>
            <namespace_modulename>
               <class>Namespace_Modulename_Model</class>
            </namespace_modulename>
        </models>
    </global>
</config>

Now define your observer class like this

File : app/code/local/Namespace/Modulename/Model/Oberver.php

<?php
class Namespace_Modulename_Model_Observer
{
     public function applyCustomFilter($observer)
     {
            $collection = $observer->getEvent()->getCollection();

            //you got collection above now perform filtering here.
            $collection->addAttributeToFilter(...);

            return $this;
     }
}

Any filtering that you apply inside applyCustomFilter() will then affect every product collections.

Hope that will help you.

[This answer is a part of #mageStackDay. It is an event conducted by Magento Community members as part of increasing the question-answer ratio. For more information http://www.magestackday.com/]

| improve this answer | |
  • 2
    Hi this helped me a lot but the handler should be catalog_product_collection_load_before – Ashish Nov 8 '14 at 9:20
  • If it helped, then feel free to accept it – Rajeev K Tomy Nov 8 '14 at 9:30
  • Hi @RajeevKTomy, It worked for me in this way, but the layered navigation filters doesn't match with the filtered collection, do you what else can I do? – Beto Castillo Aug 17 '17 at 7:35
  • @BetoCastillo Can you try this event catalog_block_product_list_collection instead of above-mentioned event? This probably works for you. – Rajeev K Tomy Aug 17 '17 at 9:25
  • With that event, the collection is not getting affected :( – Beto Castillo Aug 17 '17 at 16:40

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.