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 :
<?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 :
$product_id=22; $product=$this->getLoadProduct(22); //echo $product->getData(); echo $product->getName();
- Load Product By Id Using ObjectManager :
$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!!!
Leave a Reply