Magento – Programmatically Create Grouped Product for Import Code

  • magento create product programmatically
  • magento create grouped product programmatically

I assume you have managed to add simple products from code so this article will cover you how to programmatically create a grouped product type from Magento module and add/assign simple products to the grouped product as associated products.

Our solution is very simple and straight forward using Mangeto model. The key is this function setGroupedLinkData

1. New your catalog product model as when you create simple product and set necessary product data as always (SKU, name,….)

2. Assign simple products (you will need a data array with simple products as key). Here is example code from one of our custom module import

if ($data['type'] == 'grouped' && !empty($data['grouped'])) {

	$associated_skus = explode(',', $data['grouped']);
	$relation_data = array();
	foreach($associated_skus as $associated_sku){
		$temp = Mage::getModel('catalog/product');
		$temp_id = $temp->getIdBySku($associated_sku);
		$relation_data[$temp_id] = array('qty' => 0,
								'position' => 0
							);
	}

	$product->setGroupedLinkData($relation_data);					

}

3. Save product

Go to backend and front-end your grouped product now is created.

Comments Closed