1

I have namespace\module\Block\Display.php:

 <?php

 namespace Magento\History\Block;

 class Display extends \Magento\Framework\View\Element\Template
 {    

    private $productRepository;

    public function __construct(

          \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
        ) 
        {

            $this->productRepository = $productRepository;

         }
    public function getProductBySku($sku)
    {
        $order = $this->productRepository->get($sku);
        foreach ($order->getAllItems() as $item) 
        {  
            echo $item->getName();
        }
    }
}

And in this file .phtml, I have :

<?php
 $order = $block->getLayout()->createBlock('Magento\History\Block\Display');
 echo $order->getProductBySku();
?>

When I run, I have this error: enter image description here

Can anyone help? Thanks

  • echo $order->getProductBySku($sku); you have to pass sku in function as argument – Ketan Borada Jul 3 '19 at 10:59
  • what is your task or what you want using this code ??? – Rk Rathod Jul 3 '19 at 11:13
1
public function getProductBySku($sku)

As we can see, your function needs an argument.
I also have a note that your block class is missing the parent constructor from \Magento\Framework\View\Element\Template.

| improve this answer | |
  • @all. Thanks for your help. How can I fix this error??? – Michael Tao Jul 4 '19 at 1:31

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.