How can I associate simple products to a configurable product programmatically?
Magento – How to associated products to configurable product programmatically
configurable-productproduct
Related Solutions
First get the configurable product:
$productId = 100;//change this to what you need
$conf = Mage::getModel('catalog/product')->load($productId);
$productIds = $conf->getTypeInstance()->getUsedProductIds();
//delete the simple products
foreach ($productIds as $id) {
$product = Mage::getModel('catalog/product')->load($id)->delete();
}
//delete the conf product
$conf->delete();
I know this is no the fastest way to do it and it uses load
in loops but if you don't need to delete hundred of products or you don't need to call this constanlly you should be OK
i use this code to insert and update configurable product in my custom extention.
where all data get in data variable
$pid is configurable product id.
$aid is attribute id.
before you use this code u set session variable is edit to 0 or 1. 0- if new product , 1- if edit existing product
$data = $this->getRequest ()->getPost();
$pid=Mage::getSingleton('core/session')->getAttributeId();
$aid=Mage::getSingleton('core/session')->getAttributeValue();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$prd = Mage::getModel('catalog/product')->load($pid);
try
{
$totalRow=count($data['product_id']);
if($totalRow>0)
{
$configurable = Mage::getModel('catalog/product')->load($pid);
if(!Mage::getSingleton('core/session')->getIsEdit(0))
{
$configurable->getTypeInstance()->setUsedProductAttributeIds(array($aid));
}
$configurable->save();
$productModel = Mage::getModel('catalog/product');
$configAttributeCode = $productModel->getResource()->getAttribute($aid)->getAttributeCode();
$simpleProducts = Mage::getResourceModel('catalog/product_collection')
->addIdFilter($data['product_id'])
->addAttributeToSelect($configAttributeCode);
$configurableProductsData = array();
$configurableAttributesData = $configurable->getTypeInstance()->getConfigurableAttributesAsArray();
for($l=0;$l<$totalRow;$l++)
{
$_product = Mage::getModel('catalog/product')->load($data['product_id'][$l]);
$_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$_product->getResource()->save($_product);
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute($aid);
$att_label = $attr->getSource()->getOptionText($data['attribute_type'][$l]);
$productData = array(
'label' => $att_label, //attribute label
'attribute_id' => $aid, //attribute ID
'value_index' => $data['attribute_type'][$l],
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $data['product_price'][$l] //value for the pricing
);
$configurableProductsData[$_product->getId()] = $productData;
$configurableAttributesData[0]['values'][$l] = $productData;
}
$configurable->setConfigurableProductsData($configurableProductsData);
$configurable->setConfigurableAttributesData($configurableAttributesData);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$configurable->setCanSaveConfigurableAttributes(true);
$configurable->setStatus(1);
$configurable->save();
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
Best Answer
Assuming you know the product ids and the simple products have values for the configurable attribute: