Magento2: How to load product by id

I got the product collection in my custom template using the following snippet:

<?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection
  $productCollection */
  $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
 /** Apply filters here */

 $productCollection->load();

 foreach ($productCollection as $p)
 {
  echo "<pre>";
  print_R($p->getData());
 }
Here i got array with product id like this

 Array
(
[entity_id] => 1
[attribute_set_id] => 4
[type_id] => virtual
[sku] => Double Corona Natural (box of 25)
[has_options] => 0
[required_options] => 0
[created_at] => 2015-12-23 09:20:38
[updated_at] => 2015-12-23 09:20:38
[is_salable] => 1
)

From this entity_id, how can I load products like we did in Magento 1.x?

$_product = Mage::getModel('catalog/product')->load($productID);  

Solutions

Object method

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

Factory Method

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

}

phtml file code

$product=$this->getLoadProduct(20);
echo $product->getName();

In Magento 2.1

<?php
    namespace Test\Module\Block;
    use Magento\Catalog\Api\ProductRepositoryInterface;
    class Product extends \Magento\Framework\View\Element\Template
     {
          protected $productRepository; 
          protected $_storeManager; 

          public function __construct(
            \Magento\Framework\App\Action\Context $context,
            ProductRepositoryInterface $productRepository
          ) {
              parent::__construct($context);

              $this->productRepository = $productRepository;
          }
          public function getProduct()
          {

              $productId=1;
              return $product = $this->productRepository->getById($productId);
          }
      }

Reference

Similar questions

Products are removed from mini cart after homepage load it doesn't clear if load another URL
When I added products to cart it's added succesfully but when i click on homepage the mini cart become empty. what can be issue??
Load CMS Block or PHTML file after page load
I have a PHTML file that loads products based on the data I send via the set data method. e.g ->setData('xxx', $xxx) I call this file in another PHTML file numerous times to show all products. This means it is taking a while for the page to load. Is there a way I can load the PHTML file or a CMS Block after page load or on click? I am using this...
Images does not load on first load
When I enter my Magento website, Most of the time, some images do not load (Sometimes they do load). However, when I refresh the page, all the images become visible. Please suggest how can I debug this Problem?
SOAP-ERROR: Parsing WSDL: Couldn't load from URL Failed to load External Entity
I'm trying to get all products in $productArrarray from magento 2 API via SOAP client. I tried the below function but it throws this error: Error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://shop.mysite.com/soap/default?wsdl&services=catalogProductRepositoryV1' : failed to load external entity "https://shop.mysite.com/soap/default?wsd...
Have a option to enable/disable the module. I need to load admin page css when my module is enabled. No need to load css while module is disable
I write css in path app/code/my/module/view/adminhtml/layout/default.xml Above code apply menuformat.css to all the admin pages whether my module is enabled or not I am replacing It returns error How can i write?. I need css only active when my module is enable otherwise no need . I am using magento 2.2 I have a option to enable and disable the mod...
Magento 2.3.3 -Load Custom Jquery after the Shipping Form load
I have added jquery on other click events they are working fine but when i am trying load jQuery after the Shipping page load. I want to hide my custom added fields in shipping page but they are not working this is my custom field. i have tried many methods like: 2. and much more but did not worked for me. Please help me to get out of it.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.