In Magento 2, Most of the case we need to retrive product using sku or product id. We use ProductRepositoryInterface to retrive product.
Load product by sku and id using ProductRepositoryinterface
Just only write code for load product using id and sku. In your case you need to add that constructor argument to your class and add that functions to retrive product by using sku and product id.
<?php
/* Your namespace and class declaration */
protected $productRepository;
/* add Magento\Catalog\Api\ProductRepositoryInterface to you constructor */
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
/* function for get product by id*/
public function getProductById($productId)
{
return $this->productRepository->getById($productId);
}
/* function for get product by sku */
public function getProductBySku($sku)
{
return $this->productRepository->get($sku);
}
Load product by sku and id using ObjectManager
As per the Magento standard we not need to use ObjectManager anyware in our custom module. We can use object manager for some short of time in phtml file when we developing anything. Once it’s done we need to move that logic in Block or ViewModel.
Also we cna use ObjectManager in our custom script.
<?php
$id = 5;
$sku = 'PRD001';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->get($sku); // get product by sku
$product2 = $productRepository->getById($id); // get product by product Id
Load product using Factory class
I am not recommend to use this factory class to load any data in Magento 2. Factory class always create the new object while creating the instance of the class. When we need every time new object at that time we can use the factory class.
use Magento\Catalog\Model\ProductFactory;
/** @var ProductFactory*/
protected $productFactory;
/**
* @param ProductFactory$productFactory
*/
public function __construct(
ProductFactory $productFactory
) {
$this->productFactory = $productFactory
}
public function getProductById($productId)
{
return $this->productFactory->create()->load($productId);
}
public function getProductBySku($sku)
{
return $this->productFactory->create()->loadByAttribute('sku', $sku);
}
Load product by sku and id using ObjectManager
As per the Magento standard we not need to use ObjectManager anyware in our custom module. We can use object manager for some short of time in phtml file when we developing anything. Once it’s done we need to move that logic in Block or ViewModel.
Also we cna use ObjectManager in our custom script.
<?php
$id = 5;
$sku = 'PRD001';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productFactory = $objectManager->create(\Magento\Catalog\Model\ProductFactory::class);
$product = $productFactory->create()->loadByAttribute('sku', $sku); // get product by sku
$product2 = $productRepository->load($id); // get product by product Id