2

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

5

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

  • Let's say I have a widget with a custom product colleciton -- how can I use the above function? – easymoden00b Feb 26 '15 at 15:23
  • 1
    Hi TBI Infotech, im facing exactly the same issue in magento 2.0... i have overridden LayeredNavigation (Magento\LayeredNavigation\Block\Navigation) class, but as above problem, filters come in URL, but they are not getting applied....I have also overridden Magentos Catalog's Block Product ListProduct's _getProductCollection(). – Sushivam Sep 28 '16 at 11:41
  • I have updated the issue with code in Magento 2 here magento.stackexchange.com/questions/138474/… – Sushivam Sep 28 '16 at 13:44
  • What is the equivalent class in Magento2 for app/code/core/Mage/CatalogSearch/Model/Layer...? – Sushivam Sep 29 '16 at 10:17
0

You are using wrong method

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->addAttributeToFilter('entity_id', array('in' => $result));
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    $this->_productCollection = $collection;
    }
    return $this->_productCollection;
}

So try the above code let me know if any comments.

  • $collection->addAttributeToFilter('entity_id', array('in' => $result)); made no difference, same result – Ahmed Sep 26 '14 at 4:46
  • Collection is filtering just fine, but the selected filter of layered navigation is not getting applied on him – Ahmed Sep 26 '14 at 4:48
  • then you have to check filter file i think layer.php – Keyul Shah Sep 26 '14 at 4:55
  • please correct me if I am wrong, but I think Layer.php is not responsible for filtering the product collection but it creates a layered navigation based on the product collection which it gets from the current category – Ahmed Sep 26 '14 at 5:15
  • public function getProductCollection() { if(isset($this->_productCollections[$this->getCurrentCategory()->getId()])) { $collection = $this->_productCollections[$this->getCurrentCategory()->getId()]; } else { $collection = $this->getCurrentCategory()->getProductCollection(); $this->prepareProductCollection($collection); $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection; } return $collection; } – Ahmed Sep 26 '14 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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