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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.