Inventory Management takes an integral part in Ecommerce Management. In some cases, we need to get stock information of products for many different reasons. For example, you need to check if a product is in stock to show it on the category page, or to prevent customers from adding an out-of-stock product to the shopping cart and many other cases. 

In this article, we will guide you step by step to get product stock Magento 2.

Get product stock information from Admin Pages

As an administrator, you can get stock information of products in category product grid:

get product stock magento 2: product grid
Get product stock Magento 2: Quantity per source & Saleable quantity

Or in product creation/edition form, you could edit quantity (per source) or viewing the saleable quantity of the product.

get product stock magento 2: edit product
Get product stock Magento 2: Edit in product creation form

Get product stock information using API

Magento 2 has already created a collection of API as an easy way to get stock information without knowing so much about the internal system. Please view this link get more information.

For developers

As a developer, you also need to know product stock information such as backorders status, stock status, remaining quantity, max quantity, min quantity,… and much other information. Magento 2 supports some ways for developers to get stock information of products.

Get stock information of a particular product with StockItemRepository

...
$productId = 620;
/** @var \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItem */

$stockItem = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\Stock\StockItemRepository::class)

// Retrieve backorders status
echo $stockItem->getBackorders();

// Get quantity of item remaining in stock
echo "qty: ".$stockItem->getQty();

// Get stock status
echo $stockItem->getIsInStock();

// Retrieve Maximum Qty Allowed in Shopping Cart data wrapper
echo "max sale qty: ".$stockItem->getMaxSaleQty();
...

Retrieve product multi-source quantities with GetSourceItemsDataBySku

...
// To retrieve product multi-source quantities, you can use GetSourceItemsDataBySku

/** @var \Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku $sourceDataBySku */
//use ObjectManager to get \Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku instance

$sku = "24-MB01"; //sku of product

$sourceDataBySku = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku::class);

//load quantities of assigned source
$data = $sourceDataBySku->execute($sku);

// the returned data should be an array including source name, source code, qty of the product in source, source status
// data = array(
//  array(
//      'source_code' => "example",
//      'quantity' => 100,
//      'status' => 1
//      'name' => "Example"
//      'source_status' => true
//)

...

Product repository is used for loading all information on a particular product. Therefore, it also used to get stock information. 

...
   //use product repository to get stock data
  $sku = "883985891715";
/** @var Magento\Catalog\Model\ProductRepository $productRepository */
   $productRepository = \Magento\Framework\App\ObjectManager::getInstance()->get(Magento\Catalog\Model\ProductRepository::class);
   $product = $productRepository->get($sku);
   if ($product){
       echo $product->isSalable(); // Check is product available for sale
       echo $product->isAvailable(); // Check whether the product type or stock allows to purchase the product
       echo $product->getQty(); get quantity of product

   }
...

Product collection is also useful to get stock information on a list of products. You can also use stock attributes to filter or get data from a collection.

...
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);

//use  salable option to filter product in collection
$data =  $productCollection
->addAttributeToFilter("is_saleable", 1)
->setPageSize(5)
->toArray();

...

Note: All example code in this article is uploaded into GitHub. Please download it here.