1

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.

2
  • Reindex your magento setup Commented Apr 13, 2020 at 8:31
  • as in 'bin//magneto indexer:reindex'? Thanks Hamendra, but I've done that so many times, the 'x' has rubbed off my keypad! :)
    – user355218
    Commented Apr 13, 2020 at 9:01

3 Answers 3

4

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 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$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,

2
  • Thank you for taking the time to answer Patrick. Which file did you have to amend?
    – user355218
    Commented Apr 13, 2020 at 19:00
  • Hello @user355218, I have installed the third party theme in my project so I have changed it in respective theme file. In default magento you can look for this file "vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml" and can put above code in this file by overriding it in your specific theme. For safer way, I would suggest you to check first about which file is responsible to display stock status on product page by enabling template path hints and then do the rest. If you find my answer useful then please upvote it. Commented Apr 14, 2020 at 8:15
0

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

5
  • They are not out of stock in the backend, as the stock is still on the system as it hasn't been shipped. I could manually change everything with zero 'salable quantity' to 'Out of Stock' but I shouldn't have to, should I. Shouldn't Magento be doing that automatically?
    – user355218
    Commented Apr 13, 2020 at 9:47
  • No, It doesn't do automatically when you have zero quantity in stores. Mark as out of stock in Inventory and Tell me the new result Commented Apr 13, 2020 at 9:56
  • Thank you for your answer, Ahmed. So when I have zero salable quantity, as in nothing left to sell, I have to adjust 'in stock/out of stock' manually?? Really? I may as well stick to using paper and a pencil. :)
    – user355218
    Commented Apr 13, 2020 at 10:09
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Jarnail S
    Commented Apr 13, 2020 at 10:10
  • It is @Jai. I mean he should check in Inventory not Quantity Commented Apr 13, 2020 at 10:16
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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