Magento Tutorials

How To Check If Product With SKU Exists In Magento 2

Hello, Magento Buddies!

Are you facing the same challenge as many developers today? Developing an entire functionality of a product using its ID only to find at the end that the product has gone to meet the maker? The product is no more in your store?

But, as the saying goes, every problem comes with a solution too! And so, here I am today with a solution for all of you facing the aforementioned problem. It is imperative to recheck once if the product exists or not in your Magento 2 store before using the SKU for any purpose elsewhere. 

Programmatic Way To Check If Product With SKU Exists In Magento 2

Step 1: Create a file named Data.php in the below-given path.

app\code\Vendor\Extension\Helper\

Now, add the below code in the file.

<?php

namespace Vendor\Extension\Helper;

use Magento\Framework\App\Helper\Context;

use Magento\Catalog\Model\Product;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper

{

    protected $product;

    public function __construct(Context $context,Product $product)

    {

        $this->product = $product;

        parent::__construct($context);

    }

    public function Checkproductexist($product_sku)

    {

        if ($this->product->getIdBySku($product_sku))

      $this->_logger->info('Product is Exist');

        else

  $this->_logger->info('Product is not Exist');

    }

}

Step 2: Next, add the below-given code in module phtml file:

<?php

$helper = $this->helper('Vendor\Extension\Helper\Data');

$product_sku = "product_sku"; // add your sku here

$helper->Checkproductexist($product_sku);

?>

After you have added the above code, check whether the product exists or not in the system.log (var/log/system.log) file.

Summing It Up!

That was one quick solution. Add this code properly and you will surely get the desired results. Also, it is quite important as checking whether the product exists or not is crucial before using its SKU anywhere.

To get your Magento store custom-developed, Hire a Dedicated Magento Developer and we will create your store just as you require it to be! Also, share this solution with your Magento friends. Catch you later. Till then, have a G’day! 

Happy Coding!

Click to rate this post!
[Total: 8 Average: 4.9]
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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

6 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago