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.
2 Answers 2
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);
-
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?TheMask– TheMask2018年01月12日 10:20:12 +00:00Commented Jan 12, 2018 at 10:20
-
How to add
__constructin ResourceModel.Dhaduk Mitesh– Dhaduk Mitesh2019年12月11日 13:18:08 +00:00Commented Dec 11, 2019 at 13:18
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.