Magento 2: Ensuring Product Availability by SKU
When managing an online store with Magento 2, it's essential to verify whether a product exists in your inventory by its SKU (Stock Keeping Unit). This process helps maintain accurate inventory records, streamline operations, and improve customer satisfaction. Here's a step-by-step guide on how to check if a product exists by its SKU in Magento 2.
There are 3 ways to check, the product exists or not in Magento 2 by SKU.
1. Using Product ResourceModel:
When working with Magento 2, you might need to retrieve the Product ID based on its SKU. This can be particularly useful for various customizations and integrations. In this guide, we'll show you how to use Magento 2's Product ResourceModel to get the Product ID by SKU. If the product exists, the method will return the Product ID; otherwise, it will return false.
/** * @copyright Copyright (c) 2018-2024 Mavenbird Technologies Private Limited ( http://mavenbird.com ) * @author Mavenbird Team */ declare(strict_types=1); namespace Mavenbird\ProductAvailable\Helper; use Magento\Catalog\Model\ResourceModel\ProductFactory; /** * Class CheckProductBySKU * @package Mavenbird\ProductAvailable\Helper */ class CheckProductBySKU { /** * @var ProductFactory */ protected $_productResourceModel; /** * CheckProductBySKU constructor. * @param ProductFactory $productResourceModel */ public function __construct( ProductFactory $productResourceModel ) { $this->_productResourceModel = $productResourceModel; } /** * @param $sku * @return false|int */ public function isProductExist($sku) { return $this->_productResourceModel->create()->getIdBySku($sku); } }
Call CheckProductBySKU using constructor in any template file.
$sku = 'SKU-100'; if ($this->checkProductBySKU->isProductExist($sku)) { // Product is exist in Magento. } else { // No Product is exist in Magento. }
2. Using Product Model:
/** * @copyright Copyright (c) 2018-2024 Mavenbird Technologies Private Limited ( http://mavenbird.com ) * @author Mavenbird Team */ declare(strict_types=1); namespace Mavenbird\ProductAvailable\Helper; use Magento\Catalog\Model\ProductFactory; /** * Class CheckProductBySKU * @package Mavenbird\ProductAvailable\Helper */ class CheckProductBySKU { /** * @var ProductFactory */ protected $_productFactory; /** * CheckProductBySKU constructor. * @param ProductFactory $productFactory */ public function __construct( ProductFactory $productFactory ) { $this->_productFactory = $productFactory; } /** * @param $sku * @return int */ public function isProductExist($sku): int { return $this->_productFactory->create()->getIdBySku($sku); } }
Call CheckProductBySKU using constructor in any template file.
$sku = 'SKU-100'; if ($this->checkProductBySKU->isProductExist($sku)) { // Product is exist in Magento. } else { // No Product is exist in Magento. }
3. Using ProductRepositoryInterface:
/** * @copyright Copyright (c) 2018-2024 Mavenbird Technologies Private Limited ( http://mavenbird.com ) * @author Mavenbird Team */ declare(strict_types=1); namespace Mavenbird\ProductAvailable\Helper; use Exception; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; /** * Class CheckProductBySKU * @package Mavenbird\ProductAvailable\Helper */ class CheckProductBySKU { /** * @var ProductRepositoryInterface */ protected $_productRepository; /** * CheckProductBySKU constructor. * @param ProductRepositoryInterface $productRepository */ public function __construct( ProductRepositoryInterface $productRepository ) { $this->_productRepository = $productRepository; } /** * @param $sku * @return ProductInterface * @throws Exception */ public function isProductExist($sku): ProductInterface { try { return $this->_productRepository->get($sku); } catch (NoSuchEntityException $e) { throw new Exception($e->getMessage()); } } }
Call CheckProductBySKU using constructor in any template file.
$sku = "SKU-100"; try { $product = $this->checkProductBySKU->isProductExist($sku); } catch (\Exception $exception) { echo $exception->getMessage(); }