3

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!

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

2 Answers 2

1

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;
    }
}
1
  • It's not working :( I want to override getProductCollection() from Layer.php Mar 2, 2022 at 17:53
0

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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