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

layered-navigationmagento-2.1magento2overrides

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.

\app\code\Mage\Search\etc\di.xml

<?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" />
</config>

Layer.php

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->prepareProductCollection($collection);
            $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
        }

        return $collection;
    }

}

Please suggest any solution for this.

Thank you!

Best Answer

You should extend \Magento\Catalog\Model\Layer\Category or \Magento\Catalog\Model\Layer\Search class, like this:

In your etc/di.xml add:

...
<preference for="Magento\Catalog\Model\Layer\Category" type="[Vendor]\[Your_Extension]\Model\Override\Layer\Category" />
<type name="[Vendor]\[Your_Extension]\Model\Override\Layer\Category">
    <arguments>
        <argument name="context" xsi:type="object">Magento\Catalog\Model\Layer\Category\Context</argument>
    </arguments>
</type>
...

Create your model with your new or overrided functions in [Vendor][Your_Extension]\Model\Override\Layer\Category:

<?php

namespace [Vendor]\[Your_Extension]\Model\Override\Layer;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\ResourceModel;

class Category extends \Magento\Catalog\Model\Layer
{
    /**
     * @param $context
     * @param StateFactory $layerStateFactory
     * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory
     * @param \Magento\Catalog\Model\ResourceModel\Product $catalogProduct
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Registry $registry
     * @param CategoryRepositoryInterface $categoryRepository
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Model\Layer\ContextInterface $context,
        \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
        \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory,
        \Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Registry $registry,
        CategoryRepositoryInterface $categoryRepository,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $layerStateFactory,
            $attributeCollectionFactory,
            $catalogProduct,
            $storeManager,
            $registry,
            $categoryRepository,
            $data
        );
    }

    public function newFunction($param1){
        //Your code here
        return $this;
    }
}
Related Topic