5

Actually I need to Create a Helper Class in Magento2 then Call ProductRepository in

app/code/{vendor}/{module}/Helper/Data.php

to load Product details by sku and return Product Details in cart page and display

app/code/{vendor}/{module/view/frontend/templates/cart/item/default.phtml

1 Answer 1

5

First, you have to create a Helper file

Custom\Module\Helper\Data.php

<?php
namespace Custom\Module\Helper;

/**
 * Custom Helper for Load Products 
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $productRepository;

    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    public function loadMyProduct($sku)
    {
        return $this->productRepository->get($sku);
    }
}
?>

Then you can call this helper in your phtml file like below:

$customHelper = $this->helper('Custom\Module\Helper\Data');
$product = $customHelper->loadMyProduct($sku);
6
  • error shows in data.php Jun 27, 2019 at 10:34
  • 1
    can you please mention here?
    – Ravi Soni
    Jun 27, 2019 at 10:35
  • Thanks for your Support ,its Done Jun 27, 2019 at 10:50
  • Welcome. :) and +1 for accepting as right answer.
    – Ravi Soni
    Jun 27, 2019 at 10:53
  • your flag copied by me for profile Jun 28, 2019 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.