Search This Blog

Apr 20, 2012

Magento: How to get Quantity, Minimum Quantity and Minimum Sale Quantity

Here is easy tip on how to query or obtain stock information such quantity and minimum quantity etc. All you need to do is load the product first, and then load the stock item model.

To load product, you can either do the following:

a. Load product by product Id. Ex.
$productId = 1;
$product = Mage::getModel('catalog/product')->load($id);
b. Load product through SKU. Ex.
$productsku ='yoursku';
$product = Mage::getModel(‘catalog/product’)-> ->loadByAttribute('sku', $ productsku);

Now that you load the product, you can load the stock item model by coding the following:

$stockInfo = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

Take note that the parameter on the loadByProduct($product); method is the product you declared above.

Now you can use var_dump function to show the stock information of your product.

var_dump($stock->getData());
 

you can also print each object by doing this way:

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

4 comments:

web develoment gurgaon said...

Really greate and impressive post, I really search this type of topic and really appreciate it...thanks
Regards:
Gap Infotech Team

Janz said...

Hi, Thanks for visiting. Really appreciate your nice comment.

Regards,
Janzell

Anonymous said...

En magento comunity 1.7:

echo $stockInfo->getMinSaleQty();

Anonymous said...

Solved 2 days of searching :) thank you