Question

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");
    }
?>
Était-ce utile?

La solution

$_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

Autres conseils

I sum all products to a number and round it withouts commas:

$itemsinstock = 0;
foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
    $stock = round($stock,2);
    echo $simple->getName()." : $stock pieces";
    echo '<br/>';
    $itemsinstock+= $stock;
}
echo $itemsinstock;

Please tell me about file location where the bellow code will be added.

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/>';
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top