In this blog, we will explain how to get product data using ID in Magento 2.
Factory method and object manager are the two different ways to lode Product data.
- Load Product By Id Using Factory Method :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace VenderName\Modulename\Block; class Product extends \Magento\Framework\View\Element\Template { protected $_productloader; public function __construct( \Magento\Catalog\Model\ProductFactory $_productloader ) { $this->_productloader = $_productloader; } public function getLoadProduct($id) { return $this->_productloader->create()->load($id); } } |
- Call phtml file :
1 2 3 4 |
$product_id=22; $product=$this->getLoadProduct(22); //echo $product->getData(); echo $product->getName(); |
- Load Product By Id Using ObjectManager :
1 2 3 |
$productid = 22; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get(‘Magento\Catalog\Api\ProductRepositoryInterface’)->getById($productid); |
We hope the above blog helps you to clearly understand How to Load Products by ID in Magento 2 Programmatically.
If you have any questions let me know in the comment section
Thank You!!!