How to get product stock quantity & other stock information + Magento


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.

0 comments: