Home » Magento1 February 2012

Magento: How to get product stock quantity & other stock information?

Here is a quick code to get any product’s stock information like quantity (qty), minimum quantity (min_qty), stock availability (is_in_stock), minimum and maximum sale quantity (min_sale_qty and max_sale_qty), etc.

First load the product. Product can be loaded in different ways. Here are the two different ways to load any product in Magento:-

1. Load product by product ID

$id = 52;
$_product = Mage::getModel('catalog/product')->load($id);

2. Load product by SKU

$sku = "microsoftnatural";
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Now, get stock information for the loaded product.

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

You can check stock data in this way:-

echo "<pre>"; print_r($stock->getData()); echo "</pre>";

Or, you can print individually like this:-

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: How to get product stock quantity & other stock information?

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Subesh Pokharel

    Not Working… where is the reference to $stock>>

  • Subesh Pokharel

    Not Working… where is the reference to $stock>>

  • BC

    I would love to see this code extended to include an update query to the product quantity and product status flags. We’re thinking through how best to link and update changes to inventory made externally to Magento.

  • Emil Kroer

    Just what i needed 

  • Rick

    Finally I find the right snippet. Thank you! :)

  • Rooney

    Very useful code. Thank you for sharing.

  • http://www.facebook.com/kifofos KiFofos Kids

    I need a help.
    I have configurable products and i want that the drop down shows me, even if the product is out-of-stock. Whit the anouce “this size is out of stock”.
    Can You help me!