3

I need to retrieve product quantity, I found that, the interfaces we used before to get product quantity are deprecated, these are:

\Magento\CatalogInventory\Api\StockStateInterface $stockItem

And,

\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry

Below link says StockStateInterface is replaced with Multi Source Inventory but I cannot see a function returning product quantity in it.

https://www.magentoextensions.org/documentation/interface_magento_1_1_catalog_inventory_1_1_api_1_1_stock_state_interface.html

Is there some other way to get product quantity? I don't want to use deprecated code.

1 Answer 1

2

Here is the sample code:

    <?php


namespace SR\MagentoCommunity\Model;

use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
use Magento\InventorySales\Model\GetProductSalableQty;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;

class Stock
{
    /**
     * @var GetProductSalableQty
     */
    private $getProductSalableQty;

    /**
     * @var GetStockIdForCurrentWebsite
     */
    private $getStockIdForCurrentWebsite;

    /**
     * @var GetStockItemDataInterface
     */
    private $getStockItemData;

    /**
     * Stock constructor.
     *
     * @param GetProductSalableQty $getProductSalableQty
     * @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
     * @param GetStockItemDataInterface $getStockItemData
     */
    public function __construct(
        GetProductSalableQty $getProductSalableQty,
        GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
        GetStockItemDataInterface $getStockItemData
    ) {
        $this->getProductSalableQty = $getProductSalableQty;
        $this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
        $this->getStockItemData = $getStockItemData;
    }

    public function getStock($sku)
    {
        $stockId = $this->getStockIdForCurrentWebsite->execute();

        // Actual qty - Reserve Qty - OOS Threshold
        $productQtyInStock = $this->getProductSalableQty->execute($sku, $stockId);
        echo $productQtyInStock . PHP_EOL;

        // Return only qty
        $stockItemData = $this->getStockItemData->execute($sku, $stockId);
        $qty = $stockItemData[GetStockItemDataInterface::QUANTITY];
        echo $qty . PHP_EOL;
    }
}

You can get salable qty or total Qty using the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.