In Magento 2, a product can show available stock in the admin area yet appear as unavailable on the storefront. With MSI enabled, the entered quantity is not the whole story. Magento distinguishes the physical quantity held in a source…
Checking whether a product is in stock by product ID (Magento 2)
Use the following Magento 2 snippet to check stock availability by product ID:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function __construct( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, ... ) { $this->stockRegistry = $stockRegistry; } $stockItem = $this->stockRegistry->getStockItem($productId); if ($stockItem->getIsInStock()) { echo "Product is in stock"; } else { echo "Out of stock"; } |
Et voilà!