How to load product by id in magento2

Load product by id using Object Method in magento2


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


Load product by id using Factory Method in magento2

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

Comments