4

I tried to override Magento core model which located in vendor/magento/module-catalog/Model/Layer.php, but it doesn't work at all, here's my code:

app/code/Vendor/Module/etc/di.xml

<preference for="Magento\Catalog\Model\Layer" type="Vendor\Module\Model\Catalog\Layer" />

app/code/Vendor/Module/Model/Catalog/Layer.php

namespace Vendor\Module\Model\Catalog;

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

class Layer extends \Magento\Catalog\Model\Layer
{

    public function __construct(
        \Magento\Catalog\Model\Layer\ContextInterface $context,
        \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
        AttributeCollectionFactory $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 getProductCollection()
    {
        die('asdas'); 
    }


}
  • which error you are getting ? – Navin Bhudiya Nov 17 '17 at 6:14
  • @NavinBhudiya there's no error at all, but it still call the core model function not my model function – Shell Suite Nov 17 '17 at 6:17
  • is your module installed properly ? – Navin Bhudiya Nov 17 '17 at 6:23
  • @NavinBhudiya yes i've override magento block core before, and it's working fine – Shell Suite Nov 17 '17 at 6:23
0

Move your /etc/di.xml to /etc/frontend/di.xml

app/code/Vendor/Module/etc/frontend/di.xml

Please remove __construct and add following in your class, and don't forget to clear cache before anything.

class Layer extends \Magento\Catalog\Model\Layer
{
    public function getProductCollection()
    {
        // Do your stuff here
        return parent::getProductCollection();
    }

}
  • Still not working – Shell Suite Nov 17 '17 at 6:47
  • have you tried check after remove /var/generation ? – Aasim Goriya Nov 17 '17 at 8:19
  • yes i've tried that, setup:upgrade, complie data and remove static – Shell Suite Nov 17 '17 at 8:23
  • which magento version you are using ? – Aasim Goriya Nov 17 '17 at 8:33
  • @ShellSuite Check updated ans – Aasim Goriya Nov 17 '17 at 11:09

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.