cancel
Showing results for 
Search instead for 
Did you mean: 

How to add In stock / out of stock label for products in category page

How to add In stock / out of stock label for products in category page

I'm trying to add a in stock / out of stock label for all products on the category page. I've seen it on a lot of other webshops but haven't been able to figure out how.

Example of what I'm trying to achieve:
https://imgur.com/a/WMtvFO8

2 REPLIES 2

Re: How to add In stock / out of stock label for products in category page

@xshadiumxg0889 

Find the list.phtml file which gets called in the category pages.

For example: app/design/frontend/rwd/default/template/catalog/product/list.phtml

 

Now, add the below code where you want to show label/status

<?php   
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
    $qty = $stock->getQty();
    if($qty <= 0)
    {
?>
       <span class="out-of-stock"><span>Sold Out</span></span>
<?php
    }
    else
    {
?>
       <span class="in-stock"><span>In stock</span></span>
<?php
    }
?>
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: How to add In stock / out of stock label for products in category page

  1. Enable Stock Management in Backend
    Go to your admin panel (like Magento, etc.) and make sure inventory tracking is enabled for each product.

  2. Access Category Page Template
    Locate and open the file responsible for rendering your category pages—commonly named something like category.liquid, archive-product.php, or catalog/product/list.phtml depending on your platform.

  3. Loop Through Products
    Within the template, ensure you have a loop that fetches each product. This is where you’ll inject the stock status label.

  4. Check Stock Status per Product
    Use a conditional to check the stock quantity or availability (e.g., if(product.stock > 0) or if(product.is_in_stock)).

  5. Add Label Markup
    Within the condition, output a span or div that displays “In Stock” or “Out of Stock”.

  6. Style the Labels
    Add CSS classes like .in-stock { color: green; } and .out-of-stock { color: red; } for better visual distinction.

  7. Use Schema Markup (Optional)
    Add structured data like kfc (schema.org/Product) with availability (InStock or OutOfStock) for SEO benefits.

  8. Test with Sample Products
    Add products in both states to ensure the label shows correctly (think of it like how kfc might test new items with or without stock).

  9. Handle Variable Products
    If your site uses variants (e.g., sizes or flavors like KFC's combo options), ensure the stock check reflects the selected variation.

  10. Add Tooltip or Icon (Optional)
    Include a tooltip like “Hurry, few left!” or a visual icon next to the label to grab attention.

  11. Responsive Design
    Make sure the label looks clean on mobile devices—use media queries if needed.

  12. Cache & Performance
    Avoid making heavy stock queries on every load; consider caching stock status or using AJAX if your platform supports it.

By following these steps, your category pages will display real-time stock status like a KFC digital menu board showing what's freshly available or temporarily out!