0

I try to load a product by it's ID into my block template. Currently I am using this code which works fine:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());

But I heard that this is not clean. How can I improve this?

I tried it like this:

Block:

<?php

namespace Anshu\Bestsellerwidget\Block\Product\Widget;

use \Magento\Catalog\Model\ProductRepository;

class BestsellerWidget extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    protected $_productRepository;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        ...
        ProductRepository $productRepository,
        array $data = []
    ) {
        ...
        $this->_productRepository = $productRepository;
        parent::__construct($context, $data);
    }

    public function getProductById($id)
    {
        return $this->_productRepository->getById($id);
    }

    ...
}

Template:

$_product = $block->getProductById($item->getProductId());

But I get Type Error occurred when creating object: Anshu\Bestsellerwidget\Block\Product\Widget\BestsellerWidget

UPDATE

I found this in Magento/var/log/system.log:

Type Error occurred when creating object: Anshu\Bestsellerwidget\Block\Product\Widget\BestsellerWidget, Argument 5 passed to Anshu\Bestsellerwidget\Block\Product\Widget\BestsellerWidget::__construct() must be an instance of Magento\Catalog\Model\ProductRepository, array given, called in /srv/www/vhosts/hausfux_m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 116 [] []

NOTE: Argument 5 is the line ProductRepository $productRepository, in the constructor

  • Did you check the types for Magento\Framework\View\Element\Template class __construct() method arguments? It may be due to argument type mismatch with parent class constructor. – Jaimin Sutariya Jan 9 at 16:58
  • I updated the question – Black Jan 10 at 9:19
  • please add an actual class without removing any code – Hiren Jan 10 at 9:28
1

I had to delete the folder generated/code, then I executed sudo -u projectuser php bin/magento setup:upgrade. Now it works.

It can be enough to just execute the command without having to delete the folder first.

There was nothing wrong with the code, only with magento.

0

The issue is because the arguments you are passing with the parent::__construct methods are wrong.

The parent function requires \Magento\Framework\View\Element\Template\Context while you are passing \Magento\Catalog\Block\Product\Context as your context object.

Change the class with \Magento\Framework\View\Element\Template\Context and it should resolve your issue.

  • No, it does still throw the same error. The block class already worked before perfectly fine it just crashes after adding the productRepository code lines. – Black Jan 10 at 8:30
  • Glad you were able to solve it by yourself. – Jaimin Sutariya Jan 10 at 12:56

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.