This Blog help to understand how we load Product by ID and SKU in Magento 2.
In the following post , we describe three different ways of getting products by ID in Magento 2:
1) Object Method
2) API Repository
3) Object Manager
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.
<?php |
Now create phtml file in templates for get the product name.Here we pass product id 13.
<?php ?> |
Using getName() we fetch the value of product name.
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).
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( ) $this->_productRepository = $productRepository; public function execute() |
In above example we fetch product name where product id is 13.
To get a product Name by ID in Magento 2 via the object manager method, use the following code:
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository'); $id = 13; |
Using above code we get the product name where id is 13.
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.
<?php |
Now create phtml file in templates for get the product name.Here we pass product sku 24-WB07.
<?php $sku = '24-WB07'; ?> |
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).
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( ) $this->_productRepository = $productRepository; public function execute() |
In above example we fetch product name where product sku is '24-WB07'.
To get a product Name by ID in Magento 2 via the object manager method, use the following code:
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository'); $sku = '24-WB07'; |
Using above code we get the product name where sku is '24-WB07'.
To load product by id using object manager method, we write \Magento\Framework\App\ObjectManager::getInstance() to get the object manager instance.
<?php class Test extends \Magento\Framework\App\Action\Action ) public function execute() |
In above example we load the product id 13 and then get product name using getName() with help of Object Manager.
To load product by store id ,first we pass instance of \Magento\Catalog\Model\ProductFactory and create its object $_productFactory.
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( ) public function execute() |
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.
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.
Sign In
Create New Account