Sometimes during development in Magento 2, you may have some cases where you need to get the product by id or SKU and it per your specific requirement. 

In this article, we will show you how to Get Product by ID and SKU in Magento 2

1. Using Model

Create your custom model Vendor\Module\Model\MyModel where you retrieve the produce by SKU or ID and use it further when needed in your code. 

<?php

namespace Vendor\Module\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;

class MyModel
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;
    
    /**
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * Retrieve product by SKU
     *
     * @param string $productSku
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getProductBySku($productSku)
    {        
        return $this->productRepository->get($productSku);
    }

    /**
     * Retrieve product by id
     *
     * @param int $productId
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getProductById($productId)
    {        
        return $this->productRepository->getById($productId);
    }
}

2. Using Block

You can inject your custom model Vendor\Module\Model\MyModel in your block to get product by ID and SKU.

Example:

<?php

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Vendor\Module\Model\MyModel;

class MyBlock extends Template
{
    /**
     * @var MyModel
     */
    protected $myModel;

    /**
     * @param Context $context
     * @param MyModel $myModel
     * @param array $data
     */
    public function __construct(
        Context $context,
        MyModel $myModel,
        array $data = []
    ) {
	$this->myModel = $myModel;
	parent::__construct($context, $data);
    }

    /**
     * Get product by SKU
     *
     * @param string $productSku
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getProductBySku($productSku)
    {        
        return $this->myModel->getProductBySku($productSku);
    }

    /**
     * Get product by id
     *
     * @param int $productId
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getProductById($productId)
    {        
        return $this->myModel->getProductById($productId);
    }
}

replace: Vendor\Module with your vendor and module name

3. Using PHTML

In the phtml template file, you can get product by Id or SKU from your custom block Vendor\Module1\Block\MyBlock

Example:

<?php 
$productId = 'YOUR PRODUCT ID'; // eg. 2 
$productSku = 'YOUR PRODUCT SKU'; // eg. UM-SKU-T
?>
<?= $block->escapeHtml($block->getProductById($productId)) ?>
<?= $block->escapeHtml($block->getProductBySku($productSku)) ?>

 

Conclusion

We hope you find this guide to "Get Product by ID and SKU in Magento 2" helpful. Feel free to share or leave a comment below. Your opinion is much appreciated!

If you're looking for experienced Magento 2 developers to customize your site, fix issues, develop custom solutions, support, maintain, or anything else, feel free to check on this collection of Quality Services For Magento 2, choose the service that best suits your requirements, or contact us

Tags: magento-2-backend-development