Magento – how to set associated products when i add a new Grouped product

$newProduct = Mage::getModel('catalog/product');
$newProduct->setSku('testsku');
$newProduct->setPrice(100);
$newProduct->setAttributeSetId(4); 
$newProduct->setCategoryIds(array(3,4));
$newProduct->setTypeId("grouped");
$newProduct->setName('Product Name');
$newProduct->setDescription('The Product Description');
$newProduct->setShortDescription('Brief Description');
$newProduct->setStatus(1);
$newProduct->setTaxClassId('2');            
$newProduct->setCreatedAt(strtotime('now'));
$newProduct->save();

But I don’t know how to add Associated products to my new Grouped product.
I tried this code:

$new_product = Mage::getResourceModel('catalog/product_type_grouped')
        ->setUsedProducts($newProduct->getId(), array(7390));

It is not correct. I hope somebody can help me.

Greetings, Rapi

One thought on “Magento – how to set associated products when i add a new Grouped product”

  1. Recently I need this for configurable products in magento. Maybe this will help:

    $loader = Mage::getResourceModel('catalog/product_type_configurable')->load($configurableProduct, NULL);
    $children = $configurableProduct->getTypeInstance()->getUsedProductIds();   // actual list of children 
    
    $children[] = $product->getId();    // some simple product id
    $children = array_unique($children);
    $loader->saveProducts($configurableProduct, array_values($children));   // save new list of children to configurable product
    

Leave a Reply

Your email address will not be published. Required fields are marked *