Magento 2 Magento\Catalog\Model\layer getProductCollection() not effecting filters

1.2k Views Asked by At

I'm trying to update my catalog and catalogsearch listing collection in Magento\Catalog\Model\Layer::getProductCollection(). I need to show only grouped product in my list.

First of all, when I tried to overide Magento\Catalog\Model\Layer in my custom module, it is not working.

So, I wrote my code directly in vendor/magento/module-catalog/Model/Layer.php. The collection is only working in list.phtml but does not work in layer navigation filter.

Can anybody please let me know how to overide catalog and catalogsearch listing collection in Magento 2?

public function getProductCollection()
{
    if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
        $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
    } else {
        $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
        $collection->addAttributeToFilter('type_id', array('eq' => 'grouped'));
        $this->prepareProductCollection($collection);
        $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
    }
    return $collection;
}
0

There are 0 best solutions below