This blog helps to understand how to load product by ID and how to load product by SKU in Magento 2.
How to Load Product by ID in Magento 2
In the following post, we describe three different ways of getting products by ID in Magento 2:
- Object Method
- API Repository
- Object Manager
Get Product by ID Using Object Method
To get product name by id using object method, you will use a block class of the module Mageants_Blog.
Then inject the object of \Magento\Catalog\Model\ProductRepository class in the constructor of the module’s block class.
Here we create a $productRepository object.
<?php
namespace Mageants\Blog\Block;
class Index 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);
}
}
|
Now create a phtml file in templates to get the product name. Here we pass product id 13.
<?php $id = 13; $_product = $block->getProductById($id); echo $_product->getName(); |
Using getName() we fetch the value of the product name.
Load Product by ID Using API Repository
In this method to get product name by ID using API Repository.
Here we have used \Magento\Catalog\Api\ProductRepositoryInterface instance and created its object ($productRepository).
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
protected $_productRepository;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->_productRepository = $productRepository;
return parent::__construct($context);
}
public function execute()
{
$productId=13;
$product = $this->_productRepository->getById($productId);
echo $product->getName();
}
}
|
In the above example we fetch product name where product id is 13.
Load Product by ID Using Object Manager
To get a product Name by ID in Magento 2 via the object manager method, use the following code:
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
return parent::__construct($context);
}
public function execute()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$id = 13;$product = $productRepository->getById($id);
echo $product->getName();
}
}
|
Using the above code we get the product name where id is 13.
Let MageAnts’ Magento experts optimize and maintain your Magento 2 store for lightning-fast load times and peak performance.
How to Load Product by SKU in Magento 2
Get Product by SKU Using Object Method
To get product name by SKU using object method, you will use a block class of the module Mageants_Blog.
Then inject the object of \Magento\Catalog\Model\ProductRepository class in the constructor of the module’s block class.
Here we create a $productRepository object.
<?php
namespace Mageants\Blog\Block;
class Index 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 getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
}
|
Now create a phtml file in templates to get the product name.
Here we pass product SKU 24-WB07.
<?php $sku = '24-WB07'; $_product = $block->getProductBySku($sku); echo $_product->getEntityId(); echo ' '; echo $_product->getName(); |
Load Product by SKU Using API Repository
In this method to get product name by SKU using API Repository.
Here we have used \Magento\Catalog\Api\ProductRepositoryInterface instance and create its object ($productRepository).
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
protected $_productRepository;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->_productRepository = $productRepository;
return parent::__construct($context);
}
public function execute(){
$sku='24-WB07';$product = $this->_productRepository->get($sku);
echo $product->getName();
}
}
|
In above example we fetch product name where product sku is '24-WB07'.
Load Product by SKU Using Object Manager
To get product name by sku in Magento 2 via the object manager method, use the following code:
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
return parent::__construct($context);
}
public function execute()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$sku = '24-WB07';
$product = $productRepository->get($sku);
echo $product->getName();
}
}
|
Using the above code we get the product name where sku is '24-WB07'.
Magento 2 Load Product by ID Using Object Manager
To load product by id using the object manager method, we write \Magento\Framework\App\ObjectManager::getInstance() to get the object manager instance.
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
return parent::__construct($context);
}
public function execute()
{
$product_id=13;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);echo $product->getName();
}
}
|
In the above example we load the product id 13 and then get the product name using getName() with help of Object Manager.
Transform your store with Magento 2 upgrade—improve security, speed, and usability in less than no time.
Magento 2 Load Product by Store ID
To load product by store id, first we pass an instance of \Magento\Catalog\Model\ProductFactory and create its object $_productFactory.
<?php
namespace Mageants\Blog\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
protected $_productFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Model\ProductFactory $_productFactory
) {
$this->_productFactory = $_productFactory;
return parent::__construct($context);
}
public function execute()
{
$storeid=1;$productid=13;
$product= $this->_productFactory->create()->setStoreId($storeid)->load($productid);
echo $product->getName();
}
}
|
In the above code we use setStoreId() to set the store id value and load the product id.
Here we use the value of store id=1 and product id=13.
If you want to understand about How to Load Order By ID in Magento 2 Programmatically then, you can read our detailed blog.
If you want to understand about How to Load Customer by ID and by Email in Magento 2 then, you can check out our comprehensive guide.
Conclusion
We hope the above blog helps you to clearly understand How to Load Product by ID and SKU in Magento 2 Programmatically.
Should you encounter any challenges implementing these snippets or have further questions, don’t hesitate to reach out to MageAnts’ experts for personalized assistance.