Question

I searched the web. But was unable to find any proper guide to create grouped product programmatically.

Can anyone provide proper guide for this?

Links to web posts, books, pdf and any other online guides are highly appreciated.

Thanks in advance

Was it helpful?

Solution

The following code worked perfectly for me to create Grouped product with it's associate products. Hope this will help someone to create grouped products programmatically.

public function createGroupedProduct()
    {
        $sku = 'AL108';
        $title = 'my test product';
        $description = 'this is a description about the product...';

        $product = new Mage_Catalog_Model_Product();

        $product->setSku($sku.'-grouped');
        $product->setAttributeSetId(63); // put your attribute set id here.
        $product->setTypeId('grouped');
        $product->setName($title);
        $product->setCategoryIds(array(35)); // put your category ids here
        $product->setWebsiteIDs(array(1,2,3));// put your website ids here
        $product->setDescription($description);
        $product->setShortDescription($description);
        $product->setPrice(1000);
        $product->setWeight(200);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $product->setStatus(1);
        $product->setTaxClassId(0); 
        $product->setStockData(array(
                                    'is_in_stock'             => 1,
                                    'manage_stock'            => 0,
                                    'use_config_manage_stock' => 1
                                    ));

        try {
// Save the grouped product.
            $product->save();       
            $group_product_id = $product->getId();

// You need to create an array which contains the associate product ids.
            $simpleProductId[0] = 1483;
            $simpleProductId[1] = 1484;
            $simpleProductId[2] = 1485;
                $simpleProductId[3] = 1486;
            $simpleProductId[4] = 1487;

            $products_links = Mage::getModel('catalog/product_link_api');

// Get grouped product id.
            $group_product_id = $product->getId();

// Map each associate product with the grouped product.
            foreach($simpleProductId as $id){
                $products_links->assign ("grouped",$group_product_id,$id);
            }

        } catch (Exception $ex) {
            echo $ex->getMessage();
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top