Magento 2 How to load product by id

1)  Object method

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

2) Factory Method

<?php
namespace Hello\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);
    }
}

If like efforts, Please share, comment and subscribe for future posts and inspire more.

Comments

Popular posts from this blog

Hosting and Goodies

Magento 2 Import Scripts

How to add product attribute value in minicart magento 2