4

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

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);
  • error shows in data.php – Shafeel Sha Jun 27 at 10:34
  • 1
    can you please mention here? – Ravi Soni Jun 27 at 10:35
  • Thanks for your Support ,its Done – Shafeel Sha Jun 27 at 10:50
  • Welcome. :) and +1 for accepting as right answer. – Ravi Soni Jun 27 at 10:53
  • your flag copied by me for profile – Jugal Kishor Jun 28 at 11:52

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.