How to get child product of composite products?

In Magento there are mainly two type of products that are simple type and composite type product. These two type of products are further divided into six type.

Simple Type

  • Simple product
  • Downloadable product
  • Virtual product.

Composite Type

  • Configurable product.
  • Bundle product.
  • Grouped product.

Some times we need to get further detail for child product of Composite/Parent product which are used in parent product. To get detail of child products we can use type-instance of parent product as shown below.

1) Configurable product.

To get detail info of configurable product type we can see a configurable model class

Mage_Catalog_Model_Product_Type_Configurable

/** Configurable product Id */

$productId = '1';
$confProduct = Mage::getModel('catalog/product')->load($productId);
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$confProduct);
foreach($childProducts as $chlProduct) {
      echo $chlProduct->getId() . '--' . $chlProduct->getName() . '<br/>';
}

ORĀ 

$childProducts = $confProduct->getTypeInstance(true)->getUsedProducts(null, $confProduct);

Note: ‘TRUE’ is used for whether it should use singleton or not

2) Grouped Product.

To get detail info of Grouped product type we can see a Grouped model class.

Mage_Catalog_Model_Product_Type_Grouped

/** Grouped product Id */

$productId = '1'; $grpProduct = Mage::getModel('catalog/product')->load($productId);
$childProducts = Mage::getModel('catalog/product_type_grouped')->getUsedProducts($grpProduct);
 
foreach($childProducts as $chlProduct) {
 echo $chlProduct->getId() . '--' . $chlProduct->getName() . '<br/>';
}

OR

$childProducts = $grpProduct->getTypeInstance(true)->getAssociatedProducts($grpProduct);

Note: ‘TRUE’ is used for whether it should use singleton or not

3) Bundle Product.

To get detail info of Bundle product type we can see a Bundle model class

Mage_Bundle_Model_Product_Type

/** Bundle product Id */

$productId = '1'; $bndlProduct = Mage::getModel('bundle/product')->load($productId);
$childProducts = Mage::getModel('bundle/product_type')->getSelectionsCollection(
 $bndlProduct->getTypeInstance(true)->getOptionsIds($bndlProduct),
 $bndlProduct
);
foreach($childProducts as $chlProduct) { 
        echo $chlProduct->getId() . '--' . $chlProduct->getName() . '<br/>'; 
}

OR

$childProducts = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
 $bndlProduct->getTypeInstance(true)->getOptionsIds($bndlProduct),
 $bndlProduct
 );

Note: ‘TRUE’ is used for whether it should use singleton or not