In Magento, there is option to show the out of stock products. So that time we need to check whether the product is saleable or not for that, we need to check it for configurable and simple products
if ($_product->getTypeId() != ‘configurable’) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get(‘\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku’);
$qty = $StockState->execute($_product->getSku());
$isSaleable = $qty[0][‘qty’];
} else {
$isSaleable = $_product->isSaleable();
}
Here first check the product is configurable, if yes then check the saleable quantity of that product using it.
If simple product it checks for simple products on the same.
Thank you.