Show stock levels in Magento

Magento is a really good e-commerce platform yet there are a number of quirks or missing features that you come across as you start developing ecommerce websites.

One quirk, or ‘missing feature’, ┬áis that there is no ‘show all’ functionality within the default pagination. From within the administration user interface you can set the number of products per page to show but not to show all (This is now available in the latest versions of Magento).

Another missing feature is that by default, the number of items within stock are not shown on the product page. The interface shows if the product is in/out of stock but not how many there are in stock. I know this may not be something you would necessarily want to show, but again you’d expect this to be a setting within the admin UI.

Showing stock levels on you product page within Magento is not particularly difficult for simple product types & I’ll show you how to achieve it. It means editing a template file within your Magento template, so you’ll need ftp access to your server.

1) The file you need to edit is app/design/frontend/default/default/template/category/product/view/type/simple.phtml

2) The code you need to replace is:

<?php if($_product->isSaleable()): ?>
<p><?php echo $this->__('Availability: In stock.') ?></p>
<?php else: ?>
and replace with this:
<?php if($_product->isSaleable()): ?>
<p><?php echo $this->__(‘Availability: ‘) ?>
<?=(int)Mage::getModel(‘cataloginventory/stock_item’)
->loadByProduct($_product)->getQty() ?>
<?php echo $this->__(‘ in stock.’) ?> </p>
<?php else: ?>
Show Stock Levels in Magento
Show Stock Levels in Magento

What this means is that if the product is in stock, the customer sees how many items are in stock. Not what everybody wants, but our client did. You can see the result in this image.

14 thoughts on “Show stock levels in Magento

  1. Any suggestions on how to get the current stock on the admin sales order page per row? It would be nice to see at a glance, in the admin when looking at the order, how many we have in stock of each product ordered.

  2. Hi JT,
    Off hand no! I would imagine you’d have to extend the sales order module & it would probably only work for the most recent order, otherwise you’d need to keep a counter of stock decrement at the time of that order being processed. Then you’d have to decide if you wanted to decrement when ordering or when shipping, which in turn would open a new can of worms.

    The more I think about this one the more complex it could become & the less utility I think it might offer. For example, if you kept a counter against each order, the second most recent order stock count could be wrong, because the last order may decrement the same stock. Then you’d have to run this query n times for every order (with n items ordered). And you’d have to run those queries for every order. This is not something I’d like to execute in a busy environment.

    However, I could be totally wrong! What’s your view on this one?

  3. Thank you for this post! It needs to be updated though, I’m using Magento ver. 1.5.0.1 and here’s the updated file directory to be edited and code:

    app/design/frontend/default//template/catalog/product/view/type/default.phtml

    Replace:
    __('Availability: In stock.') ?>

    With:

    __('Availability: ') ?> loadByProduct($_product)->getQty() ?> __(' in stock.') ?>

  4. <?=(int)Mage::getModel('cataloginventory/stock_item')

    I noticed this same line of code in a number of forum posts. It's not correct. I've set my store up with the below:

    loadByProduct($_product)->getQty();
    if ($__invAmt > 0):

    echo $__invAmt,’ In Stock Ready for next day shipping!’;
    else:
    echo ‘Usually ships next day’;
    endif;
    ?>

    If there is more than none in stock, then the stock amount is displayed, otherwise it says ‘usually ships next day’.

  5. [code]
    loadByProduct($_product)->getQty();
    if ($__invAmt > 0):

    echo $__invAmt,’ In Stock Ready for next day shipping!’;
    else:
    echo ‘Usually ships next day’;
    endif;
    ?>[/code]

  6. Hi Simon, when you say “its not correct” I find that interesting, because this code does work – as you can see from the screen dump that accompanies this article. So, in what way doesn’t it work?

  7. Hi eddie, I have installed magento version 1.8.0 and the path is not the same. You can add a post to this version. thank you very much

  8. I’ve found a quick way to show levels – custom stock status extension (it’s made by Amasty). It also has some cool features like ribbons and badges for products.

  9. Hi Linda, thanks for the tip. They have a number of good extensions, as do other Mage extension shops (WebShopApps is one). However, I think that if you can, it’s a good idea to have as few 3rd party extensions as possible – it reduces possible conflicts & sources of trouble, & eases long term maintenance. I know it’s not always possible, but for something like this, an extension may be overkill.
    Cheers,

  10. Hi Mike,
    Well, you’d need to get the count of all the simple products associated with that parent product. There’s some code here that might prove useful -> http://www.tripleginteractive.com/blog/magento/magento-configurable-products-simple-product-data/
    However, I’m not sure if this would be misleading – if it said you had a stock count of 30, people might assume that means you have 30 of each variant, which might not be true.
    Best wishes,
    Eddie

Leave a Reply