How To

How to Solve the Issue of Product with Salable Qty 0 is not considered Out of Stock in Magento 2?

Hello Magento Friends,

Today I am going to solve the issue of product salable quantity 0 being shown in stock in Magento 2.

Preserving customer faith in your store is extremely important. Managing inventory and displaying the correct quantity status to customers is significant to provide a better user experience. Admin can immediately get the list of products that are out of stock and accordingly add more quantity by integrating Out of Stock Products List Extension for Magento 2.

At times Magento 2 store owners face issue of quantity availability showing in stock even if the salable quantity is 0. This has a negative experience on your store customers. So, when the salable quantity becomes 0 it should show out of stock in the frontend to customers in Magento 2.

Let’s see how you can solve the issue of a Product with a Salable Qty 0 not considered Out of Stock in Magento 2.

Steps to Solve the Issue of Product with Salable Qty 0 is not considered Out of Stock in Magento 2:

Step 1: Create default.phtml in the below path

app\design\frontend\Themes\Yourtheme\Magento_Catalog\templates\product\view\type\

Now add the below code

<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php
$stockstate = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
$sale_qty = $stockstate->execute($_product->getSku(), 1);
?>

<?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable() && $sale_qty > 0): ?>
        <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; ?>

Results:

Once you apply the above code, the stock availability status in the frontend shows properly based on the scalable quantity in the backend.

Admin Catalog Product Grid

Frontend Product View Page

Conclusion:

As simple as that! Try out the above solution to figure out the issue of Product with Scalable Qty 0 is showing in stock in Magento 2. For any help, you can freely reach out to us via the comment box.

If you found the solution useful, share it with your friends and colleagues. Stay in touch with us for more.

Happy Coding!

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • This would only work for the product page, not anywhere else it is shown - category grids, widgets, etc.

    • Yes this code is for product view page only. If you want to show on other pages like category, search etc. then just do same code into respective files.

  • Hello,

    I tried your solution and it seems to work only in the product page, in category view the products still are listed as "in stock". Is there any way to apply also in the product grid?

    Thanks in advance,
    Jon

  • If the backorder is enabled then you need to modify the code according to your requirements what you want to show.

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago