0

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>";
?>
1

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>";
?>
2
  • Perfect thank you :) – Kasper Elund Feb 1 at 19:41
  • The above code still looks at the stock level for quantity and not for the saleable quantity. How do I change so it looks at the stock level for saleable quantity and not quantity? It dispalys the backorder meassage to late for example if my quantity is 4 and saleable quantity is 3 then the backorder notifaction must be displaed when I add 4 to the cart. – Kasper Elund Feb 9 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.