0

I wrote the following code for displaying the products (like widget.)

    use Magento\Catalog\Block\Product\AbstractProduct;
    use Magento\Catalog\Block\Product\Context;
    use Magento\Catalog\Model\Product\Visibility;
    use Magento\Framework\DataObject\IdentityInterface;
    use Magento\Framework\Module\Manager as ModuleManager;
    use Magento\Framework\Registry;
    use Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory;

    /**
       * Product Most Ordered Block
      */
       class ProductList extends AbstractProduct implements  IdentityInterface
     { 
        /**
          * Core Registry
          *
          * @var \Magento\Framework\Registry
         */
        protected $_coreRegistry;

/**
 * Product Collection
 * 
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
protected $_itemCollection;

/**
 * Catalog Product Visibility
 *
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $_catalogProductVisibility;

/**
 * Module Manager
 * 
 * @var \Magento\Framework\Module\Manager
 */
protected $moduleManager;

/**
 * Reports Product Collection Factory
 * 
 * @var \Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productsFactory;    

/**
 * Initialize Block
 *
 * @param Context $context
 * @param Visibility $catalogProductVisibility
 * @param ModuleManager $moduleManager
 * @param Registry $registry
 * @param CollectionFactory $productsFactory
 * @param array $data
 */
public function __construct(
    Context $context,
    Visibility $catalogProductVisibility,
    ModuleManager $moduleManager,
    Registry $registry,
    CollectionFactory $productsFactory,
    array $data = []
) {
    $this->_catalogProductVisibility = $catalogProductVisibility;
    $this->moduleManager = $moduleManager;
    $this->_coreRegistry = $registry;
    $this->_productsFactory = $productsFactory;

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

When I do bin/magento setup:di:compile it is showing error like

Incorrect dependency in class Faonni\ProductMostOrdered\Block\ProductList in /var/www/html/New/magento-dev/app/code/Faonni/ProductMostOrdered/Block/ProductList.php \Magento\Framework\Registry already exists in the context object

1

Using the context registry should fix it :

$this->_coreRegistry = $context->getRegistry();

Meaning instead of injecting both your registry and context's registry you can use the context registry directly

so your code should be like :

<?php
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\DataObject\IdentityInterface; 
use Magento\Framework\Module\Manager as ModuleManager;
use Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory;
/**
  * Product Most Ordered Block
*/
class ProductList extends AbstractProduct implements IdentityInterface {

 /**
      * Core Registry
      *
      * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;    

/**
 * Product Collection
 * 
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
protected $_itemCollection;

/**
 * Catalog Product Visibility
 *
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $_catalogProductVisibility;

/**
 * Module Manager
 * 
 * @var \Magento\Framework\Module\Manager
 */
protected $moduleManager;

/**
 * Reports Product Collection Factory
 * 
 * @var \Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productsFactory;

/**
 * Initialize Block
 *
 * @param Context $context
 * @param Visibility $catalogProductVisibility
 * @param ModuleManager $moduleManager
 * @param Registry $registry
 * @param CollectionFactory $productsFactory
 * @param array $data
 */
public function __construct(
        Context $context, 
        Visibility $catalogProductVisibility, 
        ModuleManager $moduleManager, 
        CollectionFactory $productsFactory, 
        array $data = []
) {
    $this->_catalogProductVisibility = $catalogProductVisibility;
    $this->moduleManager             = $moduleManager;
    $this->_coreRegistry             = $context->getRegistry();
    $this->_productsFactory          = $productsFactory;

    parent::__construct(
            $context, $data
    );
}
//...
}
1

Remove Registry $registry from argument and

Replace below line

$this->_coreRegistry = $registry;

to

$this->_coreRegistry = $context->getRegistry();

1

There is no need to include Magento\Framework\Registry again in your class as you are extending Magento\Catalog\Block\Product\AbstractProduct and it already includes Magento\Framework\Registry.
Removing Magento\Framework\Registry from your class will solve your issue.

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.