1

I'm trying to show the name + quantity in stock for each simple product of a configurable product. I'm trying

<?php
if($_product->isConfigurable()) {
/*  $itemsinstock = 0; */
        echo '<p>Aantal in voorraad van:</p>';
        echo '<table>';
        foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
                $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
                $stock = round($stock,2);
                echo "<tr><td>". $simple->getName()." <td>&nbsp;:&nbsp;</td><td style='font-weight : bold; text-align : right;'>$stock</td></tr>";
/*              $itemsinstock+= $stock; */
        }
    echo '</table>';
/*  echo $itemsinstock; */
} else {
        echo "<p class='availability in-stock'>". $this->__('Availability:') ."&nbsp;<span>". number_format(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(), 0) ."</span></p>";
}

?>

I do see the quantity, but the name is empty. When I replace getName by getId, I get the correct id.

I read somewhere that the name is an attribute, but I can't get the name. I must do something wrong, but what/where?

Can some of you point me to the right direction?

2
  • See this answer magento.stackexchange.com/a/50194/208 Jan 7, 2016 at 13:10
  • Your answer is working Lord Skeletor. But how must I implement this the correct way? I edited /app/etc/config.xml directly for the test, but I want to do this the best way (I've done it wrong before and am busy cleaning up my mistakes in a clean install).
    – user34592
    Jan 9, 2016 at 22:33

2 Answers 2

1

Well here is the code you can try, this will get you simple product names too.

<?php
$product = Mage::getModel('catalog/product')->load(3);
if($product->isConfigurable()):
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
foreach($childProducts as $child) {
    print_r($child->getName());
    // You can use any of the magic get functions on this object to get the value
}
endif;

I hope this helps you out.

3
  • Load of a full product is not a good idea.it make site slow
    – Amit Bera
    Jan 3, 2016 at 11:24
  • This didn't work: Fatal error: Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getUsedProducts()
    – user34592
    Jan 9, 2016 at 22:18
  • Updated my answer for checking if product is configurable. Please have a go with this.
    – Vicky Dev
    Jan 10, 2016 at 6:59
0

There are an alternative solution by which you can get name use.

First get simple product ids from that configurable product then filter a product collection by those ids and using addFieldToSelect('name') at add product collection.

`

$ids=Mage::getResourceSingleton('catalog/product_type_configurable')
            ->getChildrenIds($_product);
 $_subproducts = Mage::getModel('catalog/product')->getCollection()
    ->addIdFilter ($ids)->addFieldToSelect('name');
        foreach ( $_subproducts  as $simple) {
                $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
                $stock = round($stock,2);
                echo "<tr><td>". $simple->getName()." <td>&nbsp;:&nbsp;</td><td style='font-weight : bold; text-align : right;'>$stock</td></tr>";
              $itemsinstock+= $stock; 
        }
1
  • To bad, this didn't work either. Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Product_Collection::addFieldToSelect()
    – user34592
    Jan 9, 2016 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.