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

Your email address will not be published. Required fields are marked *

CynoInfotech was founded in 2019, in Ahmedabad, Gujrat. CynoInfotech is a Magento module development company that grow up over this year with outsourcing clients.