
How to get product by ID and SKU in Magento 2
In Magento 2 there are various ways to load product by ID or SKU. We will try to show them to you so that you can use any of them, which fits best to your requirement.
Load Product using Model
Product can be loaded using the Magento\Catalog\Model\Product model. We will use the factory of this model to get the new instance of the product. For the demo we are injecting this dependency in the block file. You can use this somewhere else as per your need.
<?php
namespace WebbyTroops\Catalog\Block;
class Product extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $_productFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $productFactory,
array $data = []
) {
$this->_productFactory = $productFactory;
parent::__construct($context, $data);
}
/**
* Get Product by id
*
* @param int $id
* @return \Magento\Catalog\Model\Product
*/
public function getProductById($id)
{
return $this->_productFactory->create()->load($id);
}
/**
* Get Product by sku
*
* @param string $sku
* @return \Magento\Catalog\Model\Product
*/
public function getProductBySku($sku)
{
return $this->_productFactory->create()->loadByAttribute('sku', $sku);
}
}
Load Product using Repository
You can also load product using Magento\Catalog\Api\ProductRepositoryInterface repository. All you need to do is inject the dependency in the constructor and use its method get() and getById($id) to get products by SKU and ID.
<?php
namespace WebbyTroops\Catalog\Block;
class Product extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $_productRepository;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
array $data = []
) {
$this->_productRepository = $productRepository;
parent::__construct($context, $data);
}
/**
* Get Product by id
*
* @param int $id
* @return \Magento\Catalog\Model\Product
*/
public function getProductById($id)
{
return $this->_productRepository->getById($id);
}
/**
* Get Product by sku
*
* @param string $sku
* @return \Magento\Catalog\Model\Product
*/
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
}
Get Product in template file
Using any of the above approach you can load the product data and use it in the template file in the following way.
$product = $block->getProductById(5); // To load by SKU use $block->getProductBySku('sku-test');
$sku = $product->getSku();
$name = $product->getName();
Related Posts
Leave a Reply Cancel reply
Categories
- Digital Marketing (7)
- eCommerce (34)
- Laravel (1)
- Latest News (5)
- Magento 2 Extensions (5)
- Magento2 (38)
- Shopify (5)
- Shopware (7)
- Wordpress (2)
Recent Posts
Advanced custom links extension (1) auto cancel order (1) banners (1) cancel order (1) cart attributes (1) Customer (7) Custom order invoice number module (1) custom shipping method (1) E-Commerce (7) eCommerce (41) full page cache magento 2 (1) hcaptcha (1) hcaptcha vs recaptcha (1) How to add customer attribute in Magento 2 (1) Laravel 10 (1) Magento2 (34) magento 2 back in stock notification (1) magento 2 cancel order (1) Magento2 Extension (3) magento 2 extensions (1) magento 2 layered navigation (1) magento 2 recaptcha (2) magento 2 robots.txt (1) magento 2.4.6 (1) magento headless commerce (1) new features (1) online store (13) openmaze (1) order management (1) Product (4) product attributes (1) recaptcha (1) search engine optimization (6) seo (10) shipping rule (1) shopify (3) shopware (6) Shopware-plugin (3) Shopware 6 (5) shopware 6 released (1) store switcher (1) structured data (1) upcoming update (1) wordpress (1) wordpress vs shopify (1)