0

I want to display Product Salable Quantity, on Category view page.

On \app\design\frontend\Smartwave\porto\Magento_Catalog\templates\product\list.phtml

added the follow code:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku'); $qty = $StockState->execute($_product->getSku()); echo ($qty[0]['qty']); ?> 

form this post And it is works fine, but not for all categories pages. If in category view there are "Configurable Products" i get this error:

1 exception(s): Exception #0 (Exception): Notice: Undefined offset: 0 in /var/www/vhosts/glowfashion.gr/httpdocs/app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/list.phtml on line 239 

line 239: echo ($qty[0]['qty']);

i want to display the lower quantity of "Configurable Sub Products". Any idea?

1 Answer 1

0

You can not directly get configurable product's salable Quantity.

What you can do is either you can fetch any first simple product or you can sumup all the salable qty of the simple products associated with the configurable product.

Here is the sample code for the configurable product :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface'); $total_stock = 0; if($_product->getTypeID() == 'configurable'){ $productTypeInstance = $_product->getTypeInstance(); $usedProducts = $productTypeInstance->getUsedProducts($_product); foreach ($usedProducts as $simple) { $total_stock += $StockState->getStockQty($simple->getId(), $simple->getStore()->getWebsiteId()); } } echo $total_stock; 
2
  • Thnx it is works! But i want to display and Salable Quantity for products which is NON Configurable Products. So i added an else{ $StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku'); $qty = $StockState->execute($_product->getSku()); echo ($qty[0]['qty']); } Commented May 21, 2024 at 9:59
  • Congratulations! Happy to hear it worked. :) Commented May 21, 2024 at 10:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.