Echo Product Quantity in Magento
There are several different ways of echoing the various attributes of product data in Magento. There are also ways of using PHP to echo other attributes that are more hidden such as the inventory levels, costs and other bits important information. If you’ve ever wondered if it’s possible to find the quantity of a product in your store to be echoed out or used otherwise, the code that finds the value is below:
<?php (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>
There are several ways to use this data. Perhaps if you want to show the customers how many items are in stock, you could show them as below – a low stock count may encourage customers to buy the product if they think it may not be available for much longer. If you visit large e-commerce sites such as Amazon, they show in red text when the stock reaches a critical level. A message such as ‘Only 2 left in stock – Hurry’ is another factor that can drive sales – something that’s easy to achieve in the world of e-commerce website design.
The way you’d do this is shown below:
<?php $stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($stock_count <10) {
echo "<span class='red'>Only $stock_count left in stock - Hurry!</span>";
} ?>
Another idea may be the following (if you have backorders enabled):
<?php $stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($stock_count ==0) {
echo "Delivery times for this product may be up to 7-10 days within the UK";
} ?>
Knowing the product quantity data opens up a number of ways of controlling messages on the product page, let me know if you’ve have any creative ideas for using this data by commenting below – only on the Magento Blog.

Fantastic! Just what I was looking for!
thnx for the code
Excelent post…. im happy.
Saludos desde México!
How to echo formatted data???
Great post ilu
Hi, tnx for code, very useful on simple products. But when comes to configurable product (where is no Qty data) is stock always =0. :/
So if we have a least one configurable procuct, this code is useless. unfortunately.
Any idea for solution? Is possible to get code: IF product is configurable do NOT show this little pice of code?
tnx,br