How to load products by ID via Factory method ?

How to load products by ID via Factory method ?

Block file : -

namespace Test\Module\Block;

class Product extends \Magento\Framework\View\Element\Template

{

protected $_productloader;

public function __construct(

\Magento\Framework\View\Element\Template\Context $context,

\Magento\Catalog\Model\ProductFactory $_productloader

) {

$this->_productloader = $_productloader;

parent::__construct($context);

}

public function getLoadProduct($id)

{

return $this->_productloader->create()->load($id);

}

}

Phtml file :

$product=$this->getLoadProduct(20);

echo $product->getName();



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.