Overriding Magento\Catalog\Model\Layer class not working in Magento 2.

I'm trying to create a custom module for showing custom product collection with layered navigation.

here i added di.xml file to overwrite model using preference.

But its not working, i tried to create log also, no log generated.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Catalog\Model\Layer" type="Mage\Search\Model\Layer" />


namespace Mage\Search\Model;

class Layer extends \Magento\Catalog\Model\Layer

   public function getProductCollection()
        /*added log here*/
        var_dump('test'); // nothing showed.
        die(); // this also not helped
        if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
            $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
        } else {
            $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
            $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;

        return $collection;


Please suggest any solution for this.

Thank you!

  • hello. have you added di under frontend folder? – Sunil Patel May 25 '17 at 6:06
  • no, added under etc \app\code\Mage\Search\etc\di.xml – dhanabal May 25 '17 at 6:07
  • add into \app\code\Mage\Search\etc\frontend\di.xml file – Sunil Patel May 25 '17 at 10:11
  • @SunilPatel let check and let you know – dhanabal May 25 '17 at 10:19
  • make sure you flush cache, generation and di – Sunil Patel May 25 '17 at 10:22

use this

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Catalog\Model\Layer\ContextInterface;

class Layer  extends \Magento\Catalog\Model\Layer

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.