How to Load Product by ID and SKU in Magento 2
This Blog help to understand how we load Product by ID and SKU in Magento 2.
Product by Id
In the following post , we describe three different ways of getting products by ID in Magento 2: 1) Object Method 2) API Repository3) Object Manager
1) 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 $productRepository object.
<?phpnamespace 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 phtml file in templates for 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 product name.
2) API Repository:
In this method to get product name by using Id ,Here we have used \Magento\Catalog\Api\ProductRepositoryInterface instance and create it’s object($productRepository).
<?phpnamespace 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 above example we fetch product name where product id is 13.
3) Object Manager:
To get a product Name by ID in Magento 2 via the object manager method, use the following code:
<?phpnamespace 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 above code we get the product name where id is 13.
Product by SKU
1) 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 $productRepository object.
<?phpnamespace 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 phtml file in templates for 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(); ?> |
2) API Repository:
In this method to get product name by using sku ,Here we have used \Magento\Catalog\Api\ProductRepositoryInterface instance and create it’s object($productRepository).
<?phpnamespace 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'.
3) Object Manager:
To get a product Name by ID in Magento 2 via the object manager method, use the following code:
<?phpnamespace 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 above code we get the product name where sku is '24-WB07'.
Magento 2 load product by id object manager
To load product by id using object manager method, we write \Magento\Framework\App\ObjectManager::getInstance() to get the object manager instance.
<?phpnamespace 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 above example we load the product id 13 and then get product name using getName() with help of Object Manager.
Magento 2 load product by store id
To load product by store id ,first we pass instance of \Magento\Catalog\Model\ProductFactory and create its object $_productFactory.
<?phpnamespace 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 above code we use setStoreId() to set the store id value and load the product id.Here we use 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 blog by click Here.
If you want to understand about How to load customer by id and by email in Magento 2 then, you can read our blog by click Here.
Conclusion:
We hope above blog helps you to clearly understand How to Load Product by ID and SKU in Magento 2 Programmatically.
In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.