2

I am trying to extend Magento 2's core ProductsList widget, but getting following error:

Fatal error: Uncaught TypeError: Argument 1 passed to VendorName\ModuleName\Block\ProductsList\Interceptor::__construct() must be an instance of Magento\Catalog\Block\Product\Context, instance of Magento\Catalog\Model\ResourceModel\Category\CollectionFactory given, called in D:\xampp\htdocs\magento226\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 111 and defined in D:\xampp\htdocs\magento226\generated\code\VendorName\ModuleName\Block\ProductsList\Interceptor.php:11 Stack trace: #0 D:\xampp\htdocs\magento226\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(111): VendorName\ModuleName\Block\ProductsList\Interceptor->__construct(Object(Magento\Catalog\Model\ResourceModel\Category\CollectionFactory)) #1 D:\xampp\htdocs\magento226\vendor\magento\framework\ObjectManager\Factory\Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('VendorName\\ModuleName\\B...', Array) #2 D:\xampp\htdocs\magento226\vendor\magento\framework\ObjectManager\ObjectManager.php(56): Mage in D:\xampp\htdocs\magento226\generated\code\VendorName\ModuleName\Block\ProductsList\Interceptor.php on line 11

Below is my custom ProductsList class code

<?php
namespace VendorName\ModuleName\Block;

use \Magento\CatalogWidget\Block\Product\ProductsList as MagentoProductsList;
use \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;

class ProductsList extends MagentoProductsList {

    protected $_categoryCollectionFactory;

    public function __construct(
           CategoryCollectionFactory $categoryCollectionFactory)
    {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
    }


    public function test() {
        //other code here
    }

Please tell me how I can use categoryCollectionFactory like I did in my other custom modules, or am I missing something basic.

1

Try following way:


namespace VendorName\ModuleName\Block;

use \Magento\CatalogWidget\Block\Product\ProductsList as MagentoProductsList;
use \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\Serialize\Serializer\Json;

class ProductsList extends MagentoProductsList
{
    protected $_categoryCollectionFactory;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
        \Magento\CatalogWidget\Model\Rule $rule,
        \Magento\Widget\Helper\Conditions $conditionsHelper,
        CategoryCollectionFactory $categoryCollectionFactory,
        array $data = [],
        Json $json = null
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        parent::__construct(
            $context,
            $productCollectionFactory,
            $catalogProductVisibility,
            $httpContext,
            $sqlBuilder,
            $rule,
            $conditionsHelper,
            $data,
            $json
        );
    }
}

Remove generated/*

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.