Reputation: 438
With a simple product I can do this:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$product = $item->getProduct();
$stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
However, if the item is a configured product, it tries to get the stock of its parent configurable product, which in most cases has an inventory of zero.
How can I get the stock quantity of a configured product, preferably without looping through its configurable parent's child products?
Upvotes: 0
Views: 1392
Reputation: 5410
You can set a variable that determines the stock status depending on the stock status of the children of the configurable product:
$inStock = true;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cart = Mage::getModel('checkout/cart')->getQuote();
$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$_product = $item->getProduct();
$configuredProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$children = $configuredProduct->getUsedProductCollection()->addAttributeToSelect("*")->addFilterByRequiredOptions();
foreach($children as $simpleProduct) {
foreach ($cart->getAllItems() as $item) {
if ($item->getProduct()->getId() != $simpleProduct->getId()) continue;
$stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
if ($stockQty <= 0) {
$inStock = false;
break;
}
}
}
Upvotes: 3