Question

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.

Was it helpful?

Solution

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,

OTHER TIPS

Have you checked the value in Inventory if it is Out of Stock in Backend?

I tried the solution provided by Pratik Navapara. In Magento 2.4.2 the file default.phtml has been changed slightly:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>

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

I inluded the part

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

and the condition

&& $saleQty > 0

but in case the saleable quantity = 0 and stock quantity = 1 the product page does not show anything about the stock status. I guess the given solution works only on certain Magento versions.

I hope Pratik Navapara can reflect on this.

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top