Magento Forum

   
Hidden Filter? 
 
mirage
Member
 
Avatar
Total Posts:  40
Joined:  2008-06-09
Milwaukee, WI
 

I have a need to always filter the results of layered navigation or even the catalog indexes by a given attribute.

But the filter shall not show in the filter list, nor the state.

The situation is such that for a group of products I only want to show the ‘hero’ product on the index page. So I created a yes/not attribute called hero. Now when the catalog indexes run, I want to add a filter in the code - probably module extension - that automatically reduces results to ‘hero’ products only.

The question is - where is the right place do this so that it happens before the category view fetches the product list.

Or should I just conditionally check the hero attribute as I loop through the product list? I’m not sure this would work properly with pagination…

 
Magento Community Magento Community
Magento Community
Magento Community
 
mirage
Member
 
Avatar
Total Posts:  40
Joined:  2008-06-09
Milwaukee, WI
 

Ok,

I’ve come a bit further on this. I wrote a Model extension for the Catalog_Layer_Model, adding my attribute that needs to be filtered there.

class Sitesquad_Catalog_Model_Layer extends Mage_Catalog_Model_Layer
{
    
public function prepareProductCollection($collection)
    
{

        $collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->
addMinimalPrice()
            ->
addFinalPrice()
            ->
addTaxPercents()
            ->
addAttributeToFilter('hero','1'// <------ forcing display of 'hero' products only
            
;
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        
$collection->addUrlRewrite($this->getCurrentCategory()->getId());

        return 
$this;
    
}
}

I found I needed to add it in the model, because otherwise the counts in the layered navigation would not update.

The problem now is that this filter is always applied, which is something I do not want. I need to make it so that it is only apply under certain conditions, e.g. for specific categories and only if not filtered by color.

Now because I’m doing this early in the Model code, any other filters have not yet been added by the Layer Block. So I can’t test in the model what else might be filtered by, or which category I’m currently in.

On the other hand, I cannot add the filter late in the code, because the product collection will have already been fetched and the filter has no effect.

I’m looking for a better place to intervene in the filtering - as late as possible so that I can check on conditions.

Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top