Magento – Apply an extra filter in view

attributescollection;products-view

I am trying to add additional filters to a collection after the collection seems to of been generated. In catalogue product list.phtml there is the following line of code:

$_productCollection = $this->getLoadedProductCollection();

When I var_dump this it does report back as a collection (Mage_Catalog_Model_Resource_Product_Collection). However if I try to add:

$_productCollection->addAttributeToFilter('attr_handle', array('eq' => 155));

directly after the first line, the resulting collection is still exactly the same. How can I modify what is in $_productCollection in list.phtml?

Any help is appreciated

—- UPDATE —-

I notice that in the List.php block the collection is loaded in the _beforeToHtml method:

$this->_getProductCollection()->load();

I am assuming therefore I have to "unload" (?) it and then apply my filters in the view and then re-load it (?)

Best Answer

You can call $collection->clear(), apply your filters, and then reload the collection. However IIRC the collection will be in a different state than the layer model expects.

This a combination of both Mage_Catalog_Model_Resource_Product_Collection::clear() and Varien_Data_Collection::clear().