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’t work as the product only does not exist in the first place!!
The easy escape to such a situation is to check if product with SKU exists in Magento 2! Prior to using the SKU anywhere, you need to check if that product exists in Magento 2 store or not.
If the answer is yes, then only you can use the product SKU for further conditions.
And the programmatic method to do so is as below:
Method to Check If Product With SKU Exists in Magento 2:
class Data extends AbstractHelper
public function __construct(
$this->product = $product;
$this->productRepository = $productRepository;
public function productExistBySku($sku)
public function productExistById($productId)
Than simply crate Helper class object and call above function like :
public function __construct([Vendor]\[Module]\Helper\Data $data)
$this->helper = $data;
Check if product with SKU exists in Magento 2 with the above code easily.
Mention them in the Comments section below.
I’d be happy to help.
Don’t forget to share this trick with fellow developers via social media to save them from going through unnecessary development and work smart!