What I'm trying to achieve is a little bit funky.

We have an attribute on certain items which is called Primary Item and set to true. What we're trying to achieve is the following:

1) On category page, with no filtering in layered navigation show only Primary Items. On layered navigation block, show counts of ALL items.

2) On category page with filtering of some sort, show all items that matches the filter property, regardless if its a primary item. On layered navigation block, show counts of ALL items.

3) On search, show all items that matches the search query, regardless if they are primary or not. On layered navigation block, show counts of ALL items.

We tried adjusting Catalog\Block\Product\List.php and added the following:

 $this->_productCollection = $layer->getProductCollection();

        $_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
        if (isset($_filters) && !empty($_filters)){
            //DO NOTHING


            $this->_productCollection = $this->_productCollection->addAttributeToFilter('primary_item', array('eq' => 1));;


This indeed works in category listing but it also changes the layered navigation count values as we're changing the collection too early.

We also tried manipulating the actual block:

$_filters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->getActiveFilters(); if (isset($_filters) && !empty($_filters)){ //DO NOTHING
}elseif(empty($_filters)){ $_productCollection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('*') ->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id)) ->addAttributeToFilter('primary_item', array('eq' => 1)) ->load(); } ?>

Although it worked for 1+2, the search does not work at all and showing ALL primary items no matter what you search for. Moreover, the sorting in the category page stopped working (by price, by position, etc).

1 Answer 1


For search filter model is different, you can replace this code by

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters(); 

below code

$_filters = Mage::getSingleton('catalogsearch/layer')->getState()->getFilters();

You can find better in catalog search module.

  • Question is with which method to go with. Trying to change the collection in List.php or the template files directly.
    – Nick
    Sep 6, 2016 at 7:49
  • You can change collection in List.php Sep 6, 2016 at 8:20
  • Changing the collection in List.Php changes the filter counts also to show only the Primary Items.
    – Nick
    Sep 6, 2016 at 10:27
  • Filter are displaying from different files not from List.php. this will effect only for listing page products Sep 6, 2016 at 13:38
  • So that's the thing, when you change the List.php and basically change the collection: $this->_productCollection = $this->_productCollection->addAttributeToFilter('primary_item', array('eq' => 1));; The counters of the items shown in the layered navaigation matches the items you have in the adjusted productCollection which is NOT what we want.
    – Nick
    Sep 6, 2016 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.