I want to get the remain quantity of a given product. I'm going to create a report in admin panel which highlights the remaining quantity and re-order level quantity against each product. I was able to get the re-order level quantity by inventory details. And I want to get remain quantity for a given product. Please any suggestions?
2 Answers
Try
<?php
$num= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo "Remaining products: ".$num;
?>
-
1Hi, I tried this. But this returns the entire quantity for the given product. Not the remaining quantity after completing orders. Commented Mar 11, 2013 at 4:40
-
1go to
system>config>inventory
tab and check outDecrease Stock When Order is Placed
isyes
or not.– Vishnu RCommented Mar 11, 2013 at 4:46
If you are looking for the remaining quantity for products in the cart, you need to take into account configurable vs simple products:
<?php
$quote = Mage::getModel('checkout/cart')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
$cart_product = $item->getProduct();
if ($option = $item->getOptionByCode('simple_product')) {
$cart_simple_product = $option->getProduct();
} else {
$cart_simple_product = $this->getProduct();
}
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($cart_simple_product);
$max_available_quantity = (int)$stock->getQty();
}
?>