2

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.

asked Jan 12, 2018 at 9:57

2 Answers 2

3

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);
answered Jan 12, 2018 at 10:05
2
  • One more question if i want to get specific category product list then what i need to do? i mean i don't want to display product of subcategory then? Commented Jan 12, 2018 at 10:20
  • How to add __construct in ResourceModel. Commented Dec 11, 2019 at 13:18
1

You must use this code:

<?php
namespace Narola\Magentocatalog\Block\Category;
class View extends \Magento\Catalog\Block\Category\View
{
 protected $_productCollectionFactory;
 protected $_categoryFactory;
 /**
 * 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;
 /**
 * @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\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
 \Magento\Catalog\Model\CategoryFactory $categoryFactory,
 array $data = []
 ) {
 $this->_categoryFactory = $categoryFactory;
 $this->_productCollectionFactory = $productCollectionFactory;
 $this->_categoryHelper = $categoryHelper;
 $this->_catalogLayer = $layerResolver->get();
 $this->_coreRegistry = $registry;
 parent::__construct($context, $layerResolver, $registry, $categoryHelper, $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;
 } 
}

The parameters, that you are passing in the constructor doesn't make it compatible with the parent's constructor as you are not passing all of them.

answered Jan 12, 2018 at 10:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.