Magento 2 Get Product Salable Quantity Programmatically

Written by Jigar Patel

Sep 27, 2021

Magento 2 Get Product Salable Quantity Programmatically

The Salable Quantity first appears in Magento 2.3 release together with the Multi-Source Inventory system. It allows you to manage all your warehouses in one place and avoid dead stocks and out-of-stock situations.

You can see the additional column at the product grid – salable quantity.

The product grid salable quantity.

Magento 2 Get Product Salable Quantity Programmatically

Salable Quantity is the sum of available resources, grouped in stocks.

In previous versions of Magento, the quantity of a product does not decrease when an order is placed. Instead, the quantity remains the same but the salable quantity gets reduced. The quantity of the product decrease only after shipping is completed.

How To Get Product Salable Quantity In Magento 2

Get Product Salable Quantity Programmatically through two methods.

Method 1: Using getSalableQuantityDataBySku method.

Method 2: Using The Object Manager

Using getSalableQuantityDataBySku method

<?php

use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku; 
private $getSalableQuantityDataBySku;

public function __construct(GetSalableQuantityDataBySku $getSalableQuantityDataBySku) 
{
    $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
}

public function getProductSalableQty($sku)
{
    $salable = $this->getSalableQuantityDataBySku->execute($sku);
    echo json_encode($salable);
}

Now, you can get value by call getSalabelQty() function in your module file. You need to pass parameter of product sku in the function. You can call function by this below line.

$block->getSalabelQty(’24-MB04′);

Here, 24-MB04 is product sku. You will get array of salable qty details.

This will give output:-

[dt_code][{"stock_name":"Default Stock","qty":100,"manage_stock":true}][/dt_code]
  • qty is salable quantity not the actual quantity of the product.We get actual quantity of the product by $product->getQty().

Using The Object Manager

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo($qty[0]['qty']);
?>
  • $objectManager: The object manager itself is an instance of the Magento\Framework\ObjectManager\ObjectManager
    class that implements the Magento\Framework\ObjectManagerInterface class.The object manager can instantiate a PHP class, which can
    be a model, helper, or block object.

If you have any queries regarding this post, use the comment section below!

Happy Coding!

Thank you for reading.

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out the form below, and one of our technical experts will reach out to you within 12 hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    ×

    Hello!

    Click one of our contacts below to chat on WhatsApp

    × How can I help you?
    TO TOP