How to Get Product by ID, SKU in Magento 2

Getting product id and sku in Magento 2 brings you the exact Product ID number and SKU which are corresponding to the item you want to find. All you have to do is using the following commands in the admin console of your Magento 2 store. Mageplaza makes a great effort to write this topic, and we hope it will help you be clear about how to get the product ID and SKU in Magento 2.

How to getting product by ID or SKU in Magento 2

Step 1: Declare the command to get product ID or SKU

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\Catalog\Model\ProductRepository class in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{	
	protected $_productRepository;
		
	public function __construct(
		\Magento\Backend\Block\Template\Context $context,		
		\Magento\Catalog\Model\ProductRepository $productRepository,
		array $data = []
	)
	{
		$this->_productRepository = $productRepository;
		parent::__construct($context, $data);
	}
	
	public function getProductById($id)
	{
		return $this->_productRepository->getById($id);
	}
	
	public function getProductBySku($sku)
	{
		return $this->_productRepository->get($sku);
	}
}
?>

Step 2: Get product by ID or SKU in template file

Next, please use the below script to get product by id and sku in the template file.

$id = YOUR_PRODUCT_ID;
$sku = 'YOUR_PRODUCT_SKU';
$_product = $block->getProductById($id);
$_product = $block->getProductBySku($sku);
echo $_product->getEntityId();
echo '<br />';
echo $_product->getName();

Here is one of the best practices to get product information by product ID or SKU in Magento. Hope that you can do this easily to develop an SKU or Id-based feature or serve a client’s demand. If you have any queries about the article or any questions in general, use the comment section below!

Related post

Learn more: How to Quick Order by SKU in Magento 2

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 get product by id
  • magento 2 load product by id
  • magento 2 get product by sku
  • get product by id magento 2
  • magento 2 load product by sku
  • magento 2 get product id by sku
  • load product by id magento 2
  • get product by sku magento 2
  • load product by sku magento 2
  • product repository magento 2
  • magento 2 get product sku by id
  • magento 2 get sku by product id
  • magento get product by sku
  • magento 2 get product sku
  • get product id by sku magento 2
  • magento get product by id
  • get product by sku
  • get product sku magento 2
  • magento 2 product repository
  • get product magento 2
  • how to load product by id in magento 2
  • magento2 get product by sku
  • magento 2 get product
  • magento2 get product by id
  • magento get product
  • magento 2 rest api get product by sku
  • magento 2 get product id
  • magento 2 load product
  • magento 2 get product details by id
  • get product id magento 2
  • 2.3.x, 2.4.x
x