When you have set up a Multi-store inventory for your eCommerce projects in Magento 2 with multiple stocks is set up for sending an item shipment from different locations or warehouse.
You can verify Is product assigned to specific Stock or not using this article.
Sometimes when you are developing a feature for your projects and need to check specific product SKU is assigned to Stock or not, in this case, this article is useful.
If the site contains multiple Stocks for Different Sales Channel and you can verify your status of product to specific stocks.
MSI contains a single interface class to identify this feature,
Magento\InventoryApi\Model\IsProductAssignedToStockInterface
An interface contains execute() method to verify SKU in stock or not.
<?php namespace Jesadiya\IsProductAssignedToStock\Block; use Magento\InventoryApi\Model\IsProductAssignedToStockInterface; class CheckProductStock { /** * @var IsProductAssignedToStockInterface */ private $productAssignedToStock; /** * Demo constructor. * * @param IsProductAssignedToStockInterface $productAssignedToStock */ public function __construct( IsProductAssignedToStockInterface $productAssignedToStock ) { $this->productAssignedToStock = $productAssignedToStock; } /** * @return bool */ public function verifySkuAssignedToStock($sku, $stockId) { $isProductAssignedToStock = $this->productAssignedToStock->execute($sku, $stockId); return $isProductAssignedToStock; } }
Check from the Template or Other model class,
$sku = '24-MB05'; $stockId = 2; $this->verifySkuAssignedToStock($sku, $stockId);
I have another Stock with ID is 2 for my Canadian store, I just verify given SKU is assigned to stock or not by the verifySkuAssignedToStock() method.
The result will be boolean.
Based on the SKU assigned to Stock or not.