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 TomyNov 8, 2014 at 7:37
-
Mage_Catalog_Model_Resource_Product_Collection– AshishNov 8, 2014 at 7:38
-
what is that ??– Rajeev K TomyNov 8, 2014 at 7:39
-
I have tried to overwrite _applyProductLimitations()– AshishNov 8, 2014 at 7:40
-
BUt i cant figure it out how to filter the products– AshishNov 8, 2014 at 7:40
1 Answer
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/]
-
2Hi this helped me a lot but the handler should be catalog_product_collection_load_before– AshishNov 8, 2014 at 9:20
-
-
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? Aug 17, 2017 at 7:35
-
@BetoCastillo Can you try this event
catalog_block_product_list_collection
instead of above-mentioned event? This probably works for you. Aug 17, 2017 at 9:25 -