Magento 1.8 Layered Navigation – Custom Product Collection Not Getting Filtered

layered-navigationmagento-1.8

I had Overridden the product List.php Class & here is the code

protected function _getProductCollection()
{   
 if (is_null($this->_productCollection)) {

$result = array_unique($productIds);        

$collection = Mage::getResourceModel('catalog/product_collection');
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$collection->addAttributeToSelect($attributes);
$collection->addIdFilter($result);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

$this->_productCollection = $collection;
}

return $this->_productCollection;
}

working fine, I also had added Layered Navigation as mentioned here and layered navigation appeared as expected.

The only problem is, when I click on any filter in layered navigation, navigation gets updated and filter also get added to url, but product list won't get filtered by the selected filter. Please guide me how can I apply the filters on product collection

Best Answer

For layered Navigation you have to:

Extend

app/code/core/Mage/CatalogSearch/Model/Layer.php

to your custom module.

And over ride prepare collection function:

public function prepareProductCollection($collection){
    if(Mage::helper('catalogsearch')->getQuery()->getQueryText())
        return parent::prepareProductCollection($collection);
    else{
        $collection
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
            $collection->setStore(Mage::app()->getStore())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addStoreFilter()
            ->addUrlRewrite();

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
    }

    return $this;
}

Check this Reference URL

Related Topic