I am making a simple plugin that creates few example products/categories and attributes for the customers to see as an example when they start they online shop using Magento. It creates everything but has some problems with attributes.
The problem is, that attribute is created, it is added to the default set and is shown in the products and Magento admin system. But when I try to save the product, the attribute value is not saved unless I first go to the attribute and re-save it manually.
Here is my attribute creation code:
private function createAttribute()
{
$attribute = Mage::getModel('catalog/resource_eav_attribute');
$_attribute_data = array(
'attribute_code' => 'demo_size',
'is_global' => '1',
'frontend_input' => 'select', //'boolean',
'default_value_text' => '',
'default_value_yesno' => '0',
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => '0',
'is_required' => '1',
'apply_to' => '0', //array('grouped')
'is_configurable' => '1',
'is_searchable' => '0',
'is_visible_in_advanced_search' => '1',
'is_comparable' => '1',
'is_used_for_price_rules' => '0',
'is_wysiwyg_enabled' => '0',
'is_html_allowed_on_front' => '1',
'is_visible_on_front' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'frontend_label' => 'Demo Size',
'option' => array(
'value' => array(
'optionone' => array('XXL'),
'optiontwo' => array('L'),
'optionthree' => array('S'),
),
),
'default' => array(
'optionthree'
)
);
$attribute->addData($_attribute_data);
$attribute->setAttributeSetId(4);
$attribute->setAttributeGroupId(7);
$attribute->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
$attribute->setIsUserDefined(1);
try {
$attribute->save();
$this->attId = $attribute->getId();
} catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }
}
So am I missing something when the attribute is created, or is there anything else I have to do, so that the attribute would work properly without me having to re-save it manually?