How to get child product of composite products?
magento

How to get child product of composite products?

KrikaSoft
28 Sep 2014 08:20

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

Author

KrikaSoft
Hello, I'm
KrikaSoft

KrikaSoft offers innovative IT solutions and services to businesses worldwide. Specializing in Web development, Web customization, and Website design, we excel in Magento, WordPress, and other PHP frameworks. From Hybrid Application Development to seamless user experiences, trust us as your technology partner for reliable and cutting-edge solutions. Experience excellence with KrikaSoft!

Latest Post