According to Magento, if you want to create a configurable product then you need first create an attribute:
Scope: should Global
Type: Dropdown
Use To Create Configurable make it yes.
Then you need assign this attribute to attribute set
try it
//this $data - array
$sku_config = $data['sku']."_config";
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$website_ids = array(1);
$array_category_id = Mage::helper('parser/categorylist')->getCategoryArray();
$configProduct = Mage::getModel('catalog/product');
try {
$configProduct
->setStoreId(self::STORE_ID)//you can set data in store scope
->setWebsiteIds($website_ids)//website ID the product is assigned to, as an array
->setAttributeSetId(self::ATTRIBUTE_SET)//ID of a attribute set named 'default'
->setTypeId(self::PRODUCT_TYPE_CONFIGURABLE)//product type
->setCreatedAt(strtotime(self::PRODUCT_TIME_CREATED))//product creation time
->setSku($sku_config)//SKU
->setName($data['name'])//product name
->setWeight($data['characteristic']['weight'])
->setStatus(self::PRODUCT_STATUS_ENABLED)//product status (1 - enabled, 2 - disabled)
->setTaxClassId(self::PRODUCT_TAXT_CLASS_NONE)//tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)//catalog and search visibility
->setManufacturer($this->hasAttribute('manufacturer', $data['brand'])) //manufacturer id
->setPrice($data['price'])//price in form 11.22
->setCost($data['price'])//price in form 11.22
->setDescription($data['description'])
->setShortDescription($this->getShortDescriptions($data['description']))
->setCategoryIds(array($array_category_id[$name_cat]))
->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'is_in_stock' => 1 //Stock Availability
)
);
if(isset($data['spec_price']) && !empty($data['spec_price'])){
$configProduct->setSpecialPrice($data['spec_price']);
}
// add image to media gallery
$importDir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . DS;
foreach ($data['url_img_small'] as $key => $fileName) {
$filePath = $importDir . $fileName;
$type = ($key == 1) ? array('image', 'thumbnail', 'small_image') : null;
if (file_exists($filePath)) {
try {
$configProduct->addImageToMediaGallery($filePath, $type, true, false);
} catch (Exception $e) {
Mage::log($e->getLine()."::".$e->getMessage(), null, 'error_download_img.log');
}
}
}
//magic it's here
if(isset($data['color']) && !isset($data['size_data'])) {
$colorAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');// ID = 92
$configProduct->getTypeInstance()->setUsedProductAttributeIds(array($colorAttributeId)); //attribute ID of attribute 'color' in my store
$configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
foreach ($data['color'] as $key => $val) {
$simple_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $key);
$simple_product_id = $simple_product->getId();
$configurableProductsData[$simple_product_id] = array( //['920'] = id of a simple product associated with this configurable
'0' => array(
'label' => $simple_product->getAttributeText('color'), //attribute label
'attribute_id' => $colorAttributeId, //attribute ID of attribute 'color' in my store
'value_index' => (int)$simple_product->getColor(), //value of 'Green' index of the attribute 'color'
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $data['price'] //value for the pricing
),
);
}
}
if(isset($data['color']) && isset($data['size_data']) && isset($data['prod_id'])) {
$sizeAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'size');
$colorAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');
$configProduct->getTypeInstance()->setUsedProductAttributeIds(array($sizeAttributeId,$colorAttributeId)); //attribute ID of attribute 'color' in my store
$configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
$array_sku = $data['prod_id'];
foreach ($array_sku as $key => $val) {
$simple_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $val);
$simple_product_id = $simple_product->getId();
$configurableProductsData[$simple_product_id] = array( //['920'] = id of a simple product associated with this configurable
'0' => array(
'label' => $simple_product->getAttributeText('size'), //attribute label
'attribute_id' => $sizeAttributeId, //attribute ID of attribute 'color' in my store
'value_index' => (int)$simple_product->getSize(), //value of 'Green' index of the attribute 'color'
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $data['price'] //value for the pricing
),
'1' => array(
'label' => $simple_product->getAttributeText('color'), //attribute label
'attribute_id' => $colorAttributeId, //attribute ID of attribute 'color' in my store
'value_index' => (int)$simple_product->getColor(), //value of 'Green' index of the attribute 'color'
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $data['price'] //value for the pricing
)
);
}
}
$configProduct->setConfigurableProductsData($configurableProductsData);
$configProduct->save();
} catch (Exception $e) {
Mage::log($e->getLine()."::".$e->getMessage(),null,"error_create_config_product.log");
}
}
also need attribute properties seting "Use To Create Configurable Product" - YES
Best Answer
It looks like you are creating your attribute correctly, add some options to it. Once your attribute is complete, you need to add it to the attribute set by going here: Catalog > Attributes > Manage attribute sets. Once there add your attribute to the attribute set:
Once you do that, you may need to re-index. Now you can create a configurable product, go to Catalog > Manage Products then select "Add a Product" on the New Product screen select your attribute set, and select "Configurable Product" front the Product Type drop down list. When you click continue you should see your attribute as a checkbox:
Select your attribute and proceed to create your product like any other product. Hope this helps.