We are currently showing our available qty in stock on the frontend of each product using the following code:
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface'); $qty = $StockState->getStockQty($_product->getId()); ?>
But when we enable the new MSI feature in Magento 2 the above code always outputs 0. How are we able to show the total salable qty in the frontend of all inventory sources?
I found out that when I enable the Default source and assign it to a product and give it for example 10pcs. The above code outputs 10, so the above code doesnt take all the inventory sources into count. It should show the total salable qty like its showing in the backend.
Explanation with images.
With the above settings the above code outputs 10. It should output 25.
When Default Source is unassigned the above code outputs 0. It should output 15.