Wednesday 2 May 2018

Magento 2 : How to get associated products of a grouped product in product list page

The following sample code how to load a group product by object manager and get all child products (associated simple products) as an array.


$product_id = $_product->getId();
$groupedProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
$_children=$groupedProduct->getTypeInstance()->getAssociatedProducts($groupedProduct);
                                    if(count($_children)>0){                                      
                                    foreach ($_children as $child){
                                    echo "Here are your child Product Ids ".$child->getID()."\n";                                       
                                        }                                      

                                    }