0

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') 
                      ->addCategoryFilter($category);

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

If anybody know, please help me guys

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')
        ->addCategoryFilter($category);
    $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) {
        $groupedProduct->setGroupedLinkData($relation_data);
        try {
            $groupedProduct->save();
        } catch (Exception $ex) {
            Mage::logException($ex);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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