Magento 2 Get Product by ID and SKU

There are several ways to load products in Magento 2 but the best way to load products is using Repository. We are showing here all the way to load the product in the Magento 2, you can choose which one is best fit to your requirement. We also tell you which one you should use based on your requirements.

Method 1:

Load product using Repository in Magento 2

When you want to load all the data of the product at that time you should go with this method. When you want to load only limited data of the product, like only name and price then use Collection instead of Repository. The repository gives you all the data and it’s a matter of 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.