:::: MENU ::::

Magento Tutorial | Magento Blog | Learn Magento 2

Cookies Consent Popup

Magento 2 introduced MSI (Multi‑Source Inventory) starting from version 2.3. This feature allows merchants to manage inventory across multiple sources (warehouses, stores, etc.). However, developers often need to programmatically fetch product inventory data, such as stock quantity and salable status, especially when building custom modules or integrations.

Problem Scenario

  • After enabling MSI, traditional methods like StockRegistryInterface may not return correct stock data.
  • Developers need to fetch salable quantity and is_salable status for products.
  • Custom modules or API integrations require accurate inventory values.

Solution: Use Magento MSI APIs

Magento provides new APIs and services to fetch inventory data under MSI. Below are examples:

1. Get Salable Quantity


use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;

class InventoryHelper
{
    protected $getProductSalableQty;

    public function __construct(
        GetProductSalableQtyInterface $getProductSalableQty
    ) {
        $this->getProductSalableQty = $getProductSalableQty;
    }

    public function getSalableQty($sku, $stockId = 1)
    {
        return $this->getProductSalableQty->execute($sku, $stockId);
    }
}

2. Check If Product is Salable


use Magento\InventorySalesApi\Api\IsProductSalableInterface;

class InventoryHelper
{
    protected $isProductSalable;

    public function __construct(
        IsProductSalableInterface $isProductSalable
    ) {
        $this->isProductSalable = $isProductSalable;
    }

    public function checkSalable($sku, $stockId = 1)
    {
        return $this->isProductSalable->execute($sku, $stockId);
    }
}

3. SQL View for Inventory Stock

Magento MSI creates database views like inventory_stock_1 to store inventory data. You can query directly:


SELECT product_id, sku, quantity, is_salable
FROM inventory_stock_1
WHERE sku = 'test-sku';

Practical Example

Suppose you want to display stock information on a custom product page. Using GetProductSalableQtyInterface, you can fetch the exact salable quantity for the SKU. Using IsProductSalableInterface, you can check if the product is available for purchase.

Best Practices

  • Always use MSI APIs instead of legacy StockRegistryInterface when MSI is enabled.
  • Use dependency injection to access MSI services.
  • Query inventory_stock_1 only for debugging, not in production code.
  • Test with multiple sources to ensure accurate results.

SEO & UX Benefits

  • Accurate stock data improves customer trust and reduces cart errors.
  • Helps SEO by ensuring products marked “In Stock” are truly available.
  • Improves conversion rates with reliable inventory visibility.

Troubleshooting

  • If MSI APIs return zero quantity, check if sources are assigned to the product.
  • Verify inventory_stock_1 view exists in the database.
  • Run php bin/magento indexer:reindex inventory after changes.
  • Check logs in var/log for MSI errors.

Conclusion

Magento 2 MSI changes how inventory data is managed. By using GetProductSalableQtyInterface and IsProductSalableInterface, developers can reliably fetch product stock information. This ensures accurate inventory display, better customer experience, and smoother integrations with external systems.

0 comments:

Post a Comment