Programmatically assign products to configurable
<?php
define('MAGENTO', realpath('/var/www/magento'));
ini_set('memory_limit', '128M');
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$title = 'Test Video';
$description = 'Test Video';
$bodyContent='This is a test video';
$itemNum='testVideo2.1';
$price='129.95';
$product = Mage::getModel('catalog/product');
$product->setTypeId('configurable'); //
$product->setTaxClassId(0); //none
$product->setWebsiteIds(array(1)); // store id
$product->setAttributeSetId(26); //Videos Attribute Set Id
/* Media Format Values
<option value="6">dvd</option>7
<option value="5">vhs</option> */
$product->setSku($itemNum);
$product->setName($title);
$product->setDescription($description);
$product->setInDepth($bodyContent);
$product->setPrice($price);
$product->setShortDescription(ereg_replace("n","",$description));
$product->setWeight(0);
$product->setStatus(1); //enabled
$product->setVisibilty(4); //catalog and search
$product->setMetaDescription(ereg_replace("n","",$description));
$product->setMetaTitle($title);
$product->getTypeInstance()->setUsedProductAttributeIds(array(491)); //491 is the attribute id for Media Format
/* configurable product data;
5791 and 5792 are the product ids of the two simple products that I crated throug the admin interface;
you can change the pricing value if the configured product costs more or less than the parent
*/
$data = array('5791'=>array('0'=>array('attribute_id'=>'491','label'=>'vhs','value_index'=>'5','is_percent'=>0,'pricing_value'=>'')),'5792'=>array('0'=>
array('attribute_id'=>'491','label'=>'dvd','value_index'=>'6','is_percent'=>0,'pricing_value'=>'')));
$product->setConfigurableProductsData($data);
/* configurable attributes */
$data = array('0'=>array('id'=>NULL,'label'=>'Media Format','position'=> NULL,'values'=>array('0'=>array('value_index'=>5,'label'=>'vhs','is_percent'=>0,
'pricing_value'=>'0','attribute_id'=>'491'),'1'=>array('value_index'=>6,'label'=>'dvd',
'is_percent'=>0,'pricing_value'=>'0','attribute_id'=>'491')
),'attribute_id'=>491,'attribute_code'=>'media_format','frontend_label'=>'Media Format',
'html_id'=>'config_super_product__attribute_0'));
$product->setConfigurableAttributesData($data);
$product->setCanSaveConfigurableAttributes(1);
try{
$product->save();
echo "$price, $itemNum addedn";
}
catch (Exception $e){
echo "$price, $itemNum not addedn";
echo "exception:$e";
}
?>