Question

Have used the following code to display the saleable quantity on my product page:

How do I add the following?

The $qty must be in bold?
Quantity below 0 like -5 must be displayed as 0 because of backorder? Quantity over 25 must be displayed as 10+

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo "<b>In stock&nbsp</b>"; echo ($qty[0]['qty']);echo "<b>&nbsppcs.</b>";
?>
Was it helpful?

Solution

It's not directly related to Magento, the rules you want to apply are done via simple PHP.

About your code:

  • It seens you are writting logic in your PHTML files... it should preferably be done in your block files.
  • In an perfect world you should not access ObjectManager directly (but ok, it works in this example)

Anyway, the code below may help you:

<?php
/* @var $block \Magento\Catalog\Block\Product\View\AbstractView */
$_product = $block->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $stockState->execute($_product->getSku());
$qty = $qty[0]['qty'] < 0 ? 0 : ($qty[0]['qty'] > 25 ? '10+' : $qty[0]['qty']);

echo "<b>In stock: {$qty}</b>";
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top