To check whether the item is available or not for the purchase. In Magento, the quantity is always remain the same when we purchase an item from the website. But the saleable quantity count will be reduced based on the purchase of the product. So in the case of checking the available quantity of a product, we should check the saleable quantity.
For that, we can use this method.
$product is the product.
if ($product->getTypeId() != 'configurable') {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($associateproduct->getSku());
$stockqty = $qty[0]['qty'];
} else {
$stockqty = $_product->isSaleable();
}
In the $stockqty, we will get the stock quantity count.
Thank you.