Show inventory of simple product of configurable products in Magento

oodalgic picture oodalgic · Feb 13, 2013 · Viewed 11.8k times · Source

I am quite new to Magento. As far as I understand, I need to form configurable products, to manage different size of a product. I want to show stock qty of each size seperately in the product view page. Is this possible? My attribute name is 'size'. I have used the following code to get stock qty. However, it is unable to get stock qty of indivual simple product of a configurable product.

<?php   
$__manStock = $_product->getStockItem()->getManageStock();

$__invAmt =(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty('small');
if ($__manStock > 0)
    {
    echo $this->__("$__invAmt");
    }
?>

Answer

dagfr picture dagfr · Feb 15, 2013

$_product is your configurable product.

To get all its simple products use :

$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);

So you might have something like :

foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
     $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
     echo $simple->getName()." with size ".$simple->getSize()." have a stock of $stock";
     echo '<br/>';
 }

I let you adapt to your precise needs and ask question if needed