Magento 2 Get Product by ID and SKU

In Magento 2, there are multiple methods for loading products. However, the recommended approach is to use the Repository. In this guide, we will outline all the available methods for loading products in Magento 2, and help you determine which one is best suited for your specific needs.

Method 1:

Load product using Repository in Magento 2

If you need to load all the data for a product, then you should use this method. However, if you only need limited data, such as the name and price, then it’s better to use Collection instead of Repository. The Repository may provide too much data, which could affect performance.

<?php
namespace Vish\HelloWorld\Block;

use Magento\Catalog\Api\ProductRepositoryInterface;

class Test extends \Magento\Framework\View\Element\Template
{

	/** @var ProductRepositoryInterface */
	protected $_productRepository;
	
	/**
	 * @param ProductRepositoryInterface $productRepository
	 */
	public function __construct(
	    ProductRepositoryInterface $productRepository
	) {
	    $this->_productRepository= $productRepository;
	}

	public function loadProductBySKu($sku)
	{
	    return $this->_productRepository->get($sku);
	}

	public function loadProductById($id)
	{
	    return $this->_productRepository->getById($id);
	}
}

Method 2:

Load Product using Factory in Magento 2

You can load products using the Model, Catalog Product Model also loads all the data of the product so instead of using this method we recommend using Product Repository.

Important Note: Do not use Model directly, always use Factory. It creates a new instance for each request.

<?php
namespace Vish\HelloWorld\Block;

use Magento\Catalog\Model\ProductFactory;

class Test extends \Magento\Framework\View\Element\Template
{

	/** @var ProductFactory*/
	protected $_productFactory;
	
	/**
	 * @param ProductFactory$productFactory 
	 */
	public function __construct(
	    ProductFactory $productFactory 
	) {
		$this->_productFactory = $productFactory 
	}

	public function getProduct($id)
	{
		return $this->_productFactory->create()->load($id);
	}
}

Method 3:

Get Product Data using Product Collection in Magento 2

If you do not want to get all the data of the product and only want to get some fields only then use Collection.

<?php
namespace Vish\HelloWorld\Block;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class Test extends \Magento\Framework\View\Element\Template
{

	/** @var CollectionFactory */
	protected $_productCollectionFactory;
	
	/**
	 * @param CollectionFactory $productCollectionFactory
	 */
	public function __construct(
		CollectionFactory $productCollectionFactory
	) {
		$this->_productCollectionFactory= $productCollectionFactory
	}

	public function getProductCollection($sku)
	{
		$collection = $this->_productCollectionFactory->create();
		$collection->addAttributeToSelect('name');
		$collection->addAttributeToFilter('sku', array('in' => $sku))
		//$collection->addAttributeToFilter('sku', array('in' => array('sku1', 'sku2', 'sku3' ))); // Use this line to get data of multiple SKUs.
		return $collection;
	}
}

Method 4:

Load Product Using Object Manager in Magento 2

This is strongly not recommended way, Magento also mentioned not to use this method in the Adobe Commerce Developer Guide. Direct use of Object manager hides real dependencies of a class, so ignore direct use of object manager and use Factory, Resource Model, Collection or Repository instead.

$id = 7;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($id)

Conclusion: If you want to load all the data of the product then use Product Repository otherwise use Collection.