Create configurable product (way magento model) #magento #product #configurable
//Read more at http://www.danneh.org/2012/05/creating-products-programmatically-magento/#ixzz3SI3MiY11
try {
$magento_categories = array(3);
$dataArray = array();
array_push( $dataArray['186408'],
array( "attribute_id" => 92,
"label" => 'color negro',
"is_percent" => false,
//"pricing_value" => $simpleArray['price']
)
);
$cProduct = Mage::getModel('catalog/product');
$cProduct->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
->setTaxClassId(5)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setWebsiteIds(array(1))
->setCategoryIds($magento_categories)
->setAttributeSetId(4)// You can determine this another way if you need to.
->setSku("C" . '1234569')
->setName('name of the product')
->setShortDescription('descrition')
->setDescription('long description')
->setPrice(10.0)
->setTaxClassId(0);
// $cProductTypeInstance = $cProduct->getTypeInstance(); // This array is is an array of attribute ID's which the configurable product swings around (i.e; where you say when you // create a configurable product in the admin area what attributes to use as options) // $_attributeIds is an array which maps the attribute(s) used for configuration so their numerical counterparts. // (there's probably a better way of doing this, but i was lazy, and it saved extra db calls); // $_attributeIds = array("size" => 999, "color", => 1000, "material" => 1001); // etc..
// $cProductTypeInstance->setUsedProductAttributeIds(array($_attributeIds[$configurable_attribute]));
// $attributes_array = $cProductTypeInstance->getConfigurableAttributesAsArray();
// foreach($attributes_array as $key => $attribute_array) {
// $attributes_array[$key]['use_default'] = 1;
// $attributes_array[$key]['position'] = 0;
// if (isset($attribute_array['frontend_label'])) {
// $attributes_array[$key]['label'] = $attribute_array['frontend_label'];
// } else {
// $attributes_array[$key]['label'] = $attribute_array['attribute_code'];
// }
// }
$attributes_array = array();
$attributes_array[92] = array(
'use_default' => 1,
'position' => 0,
'label' => 'color negro'
);
Zend_Debug::dump($attributes_array);
// $att_size = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','size');
// $att_sizes = $this->__getAttList('size');
// $att_color = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','color');
// $att_colors = $this->__getAttList('color');
// $cProduct->setConfigurableAttributesData(array(
// array_merge($att_color->getData(), array('label' => '', 'values' => $color_values))
// ));
// ->setConfigurableProductsData($associatedArrays);
// ->setUrlKey(getProductUrlKey($main_product_data['product_name']));
$cProduct->setStockData(array('use_config_manage_stock' => 1,
'is_in_stock' => 1,
'is_salable' => 1 ));
$super_attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','color');
$configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
$newAttributes[] = array(
'id' => $configurableAtt->getId(),
'label' => $configurableAtt->getLabel(),
'position' => $super_attribute->getPosition(),
'values' => $configurableAtt->getPrices() ? $cProduct->getPrices() : array(),
'attribute_id' => $super_attribute->getId(),
'attribute_code' => $super_attribute->getAttributeCode(),
'frontend_label' => $super_attribute->getFrontend()->getLabel(),
);
$cProduct->setCanSaveConfigurableAttributes(true);
$cProduct->setCanSaveCustomOptions(true);
// $existingAtt = $cProduct->getTypeInstance()->getConfigurableAttributes();
//
// Zend_Debug::dump($existingAtt->debug());
if(!empty($newAttributes)){
// $cProduct->setCanSaveConfigurableAttributes(true);
$cProduct->setConfigurableAttributesData($newAttributes);
$cProduct->setConfigurableProductsData($dataArray);
$cProduct->save();
Zend_Debug::dump($cProduct->debug());
}
//$cProduct->save();
} catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}