Override constructor of vender/magento/module_caltalog/block/ Block in magento 2

by TheMask   Last Updated January 12, 2018 10:09 AM

I am trying to get product list from specific category id. i am using magento 2.2.1 version.

below are my code of Block File : view.php i want to override this file from vender.

Here is my custom module :

app\code\Narola\Magentocatalog\Block\Category\View.php

namespace Narola\Magentocatalog\Block\Category;
class View extends \Magento\Catalog\Block\Category\View
{
  protected $_productCollectionFactory;
  protected $_categoryFactory;

  public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    array $data = []
  ) {
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context, $data);
}

public function getSubcategory($catId)  
{
    $objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $collection = $categoryFactory->create()
    ->addFieldToFilter('parent_id', $catId) 
    ->addAttributeToSelect('*');
    return $collection;
}

public function  getSubcategory_product($sub_catid){
    $category = $this->_categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    return $collection;
} 
}

==============================

Here when i add constructor i am getting below error.


Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Catalog\Block\Category\View::__construct() must be an instance of Magento\Catalog\Model\Layer\Resolver, array given, called in C:\wamp64\www\magento_221\app\code\Narola\Magentocatalog\Block\Category\View.php on line 18 and defined in C:\wamp64\www\magento_221\vendor\magento\module-catalog\Block\Category\View.php on line 45

If any one has solution regarding this then please provide me.

Tags : magento2 blocks


Answers 1


Your constructor has to be like

\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Helper\Category $categoryHelper,
array $data = []

and call the parent function like

parent::__construct($context, $layerResolver, $layerResolver, $categoryHelper, $data);
Nikolas
Nikolas
January 12, 2018 10:05 AM

Related Questions


Move Header Search to New Destination

Updated February 23, 2016 04:04 AM



Blocks Not Updating - Magento 2

Updated March 13, 2017 12:09 PM

Magento2 rewrite block not work

Updated June 12, 2016 08:03 AM