Magento 1 – How to Rewrite Catalog Product Collection

moduleoverridesproduct-collection

I'm trying to rewrite the Magento Catalog Product Collection – basically I have a specific filtering rule that must be applied every time a product collection is loaded, so I'm figuring that putting it into the collection directly is the best way of doing it.

However, I've never really played with this before, and I've not been able to get it working so far.

The relevant part of my config.xml is

  <global>
        <models>
            <catalog>
                <rewrite>
                    <resource_product_collection>Lw_Restrict_Model_Resource_Product_Collection</resource_product_collection>
                </rewrite>
            </catalog> 

And for testing, my app/code/local/Lw/Restrict/Model/Resource/Product/Collection.php just contains:

 <?php
 die("??");
 class Lw_Restrict_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection {

     protected function _beforeLoad() {
         echo "Load!!";
         parent::_beforeLoad();
     }

 }

So if I go to a category page for example, I'd actually expect the page to die, which it isn't!

Could someone point out whatever obvious thing I've obviously missed is please!!

Thanks!

Best Answer

After doing some extra reading, I figured out the XML should look like this:

            <catalog_resource>
                <rewrite>
                    <product_collection>Lw_Restrict_Model_Resource_Product_Collection</product_collection>
                </rewrite>
            </catalog_resource> 
Related Topic