{"id":671,"date":"2019-11-15T06:32:11","date_gmt":"2019-11-15T06:32:11","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/11\/15\/check-if-product-with-sku-exists-in-magento-2\/"},"modified":"2025-05-22T15:22:43","modified_gmt":"2025-05-22T09:52:43","slug":"check-if-product-with-sku-exists-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/check-if-product-with-sku-exists-in-magento-2\/","title":{"rendered":"How To Check If Product With SKU Exists in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">How embarrassing it would be for a Magento 2 developer to develop a complete functionality based on the product ID and try to implement it only to realize in the end that it won&#8217;t work as the product only does not exist in the first place!!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happens!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The easy escape to such a situation is to <strong>check if product with SKU exists in Magento 2<\/strong>! Prior to using the SKU anywhere, you need to check if that product exists in Magento 2 store or not.<i><\/i><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the answer is yes, then only you can use the product SKU for further conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And the programmatic method to do so is as below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Check If Product With SKU Exists in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Helper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\Catalog\\Model\\ProductRepository;\nclass Data extends AbstractHelper\n{\n    protected $product;\n    protected $productRepository;\n    public function __construct(Context $context, Product $product, ProductRepository $productRepository)\n    {\n        $this->product = $product;\n        $this->productRepository = $productRepository;\n        parent::__construct($context);\n    }\n    public function productExistBySku($sku)\n    {\n        if ($this->product->getIdBySku($sku)) \n        {\n            $this->_logger->info('Product Exist');\n        }\n    }\n    public function productExistById($productId)\n    {\n        if ($this->productRepository->getById($productId)) \n        {\n            $this->_logger->info('Product Exist');\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Than simply create Helper class object and call above function like :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">protected $helper;\npublic function __construct([Vendor]\\[Module]\\Helper\\Data $data)\n{\n    $this->helper = $data;\n}\n$this->helper->productExistBySku('AB1');\n$this->helper->productExistById(123);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check if product with SKU exists in Magento 2<em> <\/em>with the above code easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to share this trick with fellow developers via social media to save them from going through unnecessary development and work smart!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How embarrassing it would be for a Magento 2 developer to develop a complete functionality based on the product ID and try to implement it&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-671","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":15184,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/671\/revisions\/15184"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}