3

I am trying to override Magento\Catalog\Block\Category\View

added this code in di.xml

<preference for="Magento\Catalog\Block\Category\View" type="Naheed\AllProducts\Block\Category\View" />

And my new file is this Vendor\Module\Block\Category\View

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Block\Category;

/**
 * Class View
 * @api
 * @package Magento\Catalog\Block\Category
 * @since 100.0.2
 */
class View extends \Magento\Catalog\Block\Category\View
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * Catalog layer
     *
     * @var \Magento\Catalog\Model\Layer
     */
    protected $_catalogLayer;

    /**
     * @var \Magento\Catalog\Helper\Category
     */
    protected $_categoryHelper;

    protected $priceHelper;

    protected $_storeManager;

    protected $categoryRepository;

    protected $_request;

    protected $productFactory;

    protected $eavconfig;   
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory,
        \Magento\Eav\Model\Config $eavconfig,
        array $data = []
    ) {
        $this->_categoryHelper = $categoryHelper;
        $this->priceHelper = $priceHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_coreRegistry = $registry;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->categoryRepository = $categoryRepository;
        $this->productFactory = $productFactory;

        parent::__construct($context, $data);
    }

    ...
}

Now I am getting error Argument 2 passed in __constructor

  • why you need class rewrite? Please explain your purpose. – Amit Bera Jun 12 at 8:02
  • update error log – MSA Jun 12 at 8:03
  • @AmitBera I need to hide some filters for specific categories. Do my purpose linked with the error I have mentioned? I mean magento give me option to override any file and it does not require any purpose :D – Ahmed Zameer Jun 12 at 8:05
  • Actually my main concern is, I am unable to override Magento\Catalog\Block\Category\View file – Ahmed Zameer Jun 12 at 8:06
  • 1
    @AhmedZameer please delete generated folder,try again. – tokey Jun 12 at 8:08
1

You have to changes the

parent::__construct($context, $data);

to

parent::__construct($context, $layerResolver, $registry, $categoryHelper, $data);

The original method Magento\Catalog\Block\Category\View::__construct(

the definition has 5 parameter,

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Helper\Category $categoryHelper,
    array $data = []
) {

So you have your class method __construct( must provide this 5 parameter. This is basically the oops concept

  • Thanks Amit, I will apply this. Will get back to you :) – Ahmed Zameer Jun 12 at 8:11
  • Note that after this changes you must need di compile properly. – Amit Bera Jun 12 at 8:15
  • Perfect. Thanks :) – Ahmed Zameer Jun 12 at 9:12

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.