Inventory – Salable Quantity of 0 but Still Showing in Stock

inventoryout-of-stocksalable

I have a big issue with Magento 2.3.2. When products reach a salable quantity of 0 they still appear in stock. These products still have a physical quantity of more than zero as they haven't shipped yet, but they can still be selected on the frontend, although they won't add to basket, leading customers to think that the website is not functioning correctly. (Which I suppose it isn't). Why would my products still be selectable on the frontend even though salable stock is zero? Can anyone please shed some light?

I do have the 'inventory_stock_1' database view, but products with zero salable quantity are still listed as 'is_salable=1' in this view.

Product doesn't actually go 'Out of Stock' until 'Quantity' reaches zero (all are shipped). Something is not right.

Thank you.

Best Answer

I was facing the same issue in one of my projects recently. In my case the client wanted to display stock availability label (i.e. Instock/Outofstock) on category pages as well. That is not available in default magento.

When saleable qty becomes 0 and quantity is 1 in that case PDP page was showing outofstock where the category page was still showing in stock having the same code written over both places. However, the fact was that the customer could not able to add that product into cart at the end. Hence, it was creating chaos on customer's mind.

What I did was to modify magento default code a little bit as below:

Old Code :

<?php if ($block->displayProductStockStatus()) :?>
    <?php if ($_product->isAvailable()) :?>
        <div class="stock available" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
            <span><?= $block->escapeHtml(__('In stock')) ?></span>
        </div>
    <?php else :?>
        <div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
            <span><?= $block->escapeHtml(__('Out of stock')) ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>

New Code :

<?php 
    $stockState = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
    $saleQty = $stockState->execute($_product->getSku(), 1);
?>

<?php if ($block->displayProductStockStatus()) :?>
    <?php if ($_product->isAvailable() && $saleQty > 0) :?>
        <div class="stock available" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
            <span><?= $block->escapeHtml(__('In stock')) ?></span>
        </div>
    <?php else :?>
        <div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
            <span><?= $block->escapeHtml(__('Out of stock')) ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>

Hope this help you.

Thanks,

Related Topic