I have grouped product with the name Group1. And also I have category with the same name(Group1). I have several simple product under the Group1 category. How to add these products under this category only as Associated product to grouped product programmatically?

I got a product collection under the category named as Group1 by

$productCollection = Mage::getResourceModel('catalog/product_collection') 

Now i want to add these products to the Grouped product as Associated product

If anybody know, please help me guys

1 Answer 1


If simple products are under Group1 category try following code to associate them to grouped product of this category:

    $productCollection = Mage::getResourceModel('catalog/product_collection')
    $relation_data     = array(); //Grouped - child relation data
    $groupedProduct    = false; //Grouped product
    foreach ($productCollection as $product) {
        if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_GROUPED) {
            $groupedProduct = $product;
        } else if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
            $relation_data[$product->getId()] = array(
                'qty'      => 1,
                'position' => 0,
                'ids'      => $product->getId()
    if ($groupedProduct) {
        try {
        } catch (Exception $ex) {

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.