In this artical we learn how to get product data by product id.
we create a block file and add below code. To acheave it we use \Magento\Catalog\Model\ProductFactory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php protected $_productfact; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\ProductFactory $_productfact ) { $this->_productfact = $_productfact; parent::__construct($context); } public function getLoadProduct($id) { return $this->_productfact->create()->load($id); } } |
And then we call below code to associated .phtml file
1 2 |
$product=$this->getLoadProduct(20); echo $product->getName(); |
Welcome to our blog!