Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Get Product by ID, SKU in Magento 2

Vinh Jacker | 03-17-2025

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 get product by ID or SKU in Magento 2

Method 1: Use Construct

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();

Method 2: Use ObjectManager

The second method that you should consider is using ObjectManager. You can use it like this:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    $productsku = "product_sku"; // add Your SKU here
    $product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->get($productsku);
   echo $product->getId();

Which method to choose?

Both can meet the need to get product by ID and SKU just fine. What’s important is which one you feel more confident in. However, if you’re still confused, we would like to suggest you use the second option because:

  • Magento 2 standards: Avoid direct use of ObjectManager.
  • Better performance: Dependency Injection is optimized.
  • Easier testing: You can mock ProductRepository for unit tests.
  • Clean and maintainable code: Clear and extensible.

Conclusion

To create an efficient Magento 2 order management system, store owners should be able to perform basic tasks like managing products, restricting shipping and payment methods, and getting customer collections. These small details work together to create a successful and smooth-running store.

And in today’s blog, we have discovered 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!

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

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services