General

How to Load Products by Product ID and SKU in Magento 2?

Hello, Magento Folks,

Last time we learned how you can Set Image Gallery Fotorama thumbnail as Vertical in Magento 2. Today, we are going to learn how you can load your Magento 2 Products by Product ID and SKU.

Loading products is an important thing to get product information, and also it is the most sought after thing by many merchants. This serves the purpose of getting the exact product information to make changes or do other things with the help of product ID or SKU assigned to the particular product.

We will be looking into how you can load products from product ID and SKU both with the help of some simple codes that are also recommended by Magento.

Let’s first look at how you can load products from product ID.

Method – 1: This short method is not recommended by the Magento to use so we don’t either.

$productid = 32;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productid);

Method – 2: This method is highly recommended by Magento to use when you need the product information.

<?php
namespace Vendor\Extension\Block;

class Product extends \Magento\Framework\View\Element\Template
{
   protected $productrepository;  

   public function __construct(\Magento\Catalog\Api\ProductRepositoryInterface $productrepository) {
        $this->productrepository = $productrepository;
   }

   public function getProductDataUsingId($productid) {
       return $this->productrepository->getById($productid);
   }
}

PHTML file code,
$product = $block->getProductDataUsingId(32);
echo $product->getName();

Now, let’s look at how to load products by product SKU.

Method – 1: This short method is not recommended by the Magento to use so we don’t either.

$productsku = "productsku";
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->get($productsku);

Method – 2: This method is highly recommended by Magento to use when you need product information.

<?php 
namespace Vendor\Extension\Block; 

class Product extends \Magento\Framework\View\Element\Template
{
  protected $productrepository; 

  public function __construct(\Magento\Catalog\Api\ProductRepositoryInterface $productrepository) { 
         $this->productrepository = $productrepository;
  }

  public function getProductDataUsingSku($productsku) {
        return $this->productrepository->get($productsku);
  }
}


PHTML file code,
$product = $block->getProductDataUsingSku(“productsku”);
echo $product->getName();

So, this was it for now. Using these codes, you will be able to load product information by ID or SKU, whichever you prefer. Comment below to let us know that you like the article. You can also share this with your Magento Friends.

 

Lastly, if any problem arises when applying codes, then you can get in touch with us at our support portal.

Happy Coding!?

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago