I have programmatically created some custom product attributes as part of an import routine. These have been assigned to an attribute set and they appear as expected in the admin alongside the appropriate products. However, the values will not save either in the admin OR programmatically via a script. That is, until I set "visible on product view page on front-end" to true.
Why should that make a difference? Does setting that trigger some additional setting up of the attributes that I may have missed in the creation process? Once a value has been saved - I can switch off that option and it still works. Oddness.
Here is the function that is used to create the attributes
public function newMagentoAttribute($name=null,$set=null,$sortOrder=null,$details=null,$productType="simple,grouped",$atttributeType="text"){
/*with reference to code found here: http://pradeepkumarrcs.blogspot.co.uk/2011/12/creating-new-attribute-and-adding-to.html [jh]*/
$attr_data = array(
'attribute_code' => $name,
'is_global' =>1,
'frontend_input' => $atttributeType, //'boolean',
'default_value_text' => '',
'default_value_yesno' => '0',
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => '0',
'is_required' => '0',
'apply_to' => array($productType), //array('grouped')
'is_configurable' => '0',
'is_searchable' => '1',
'is_visible_in_advanced_search' => '0',
'is_comparable' => '0',
'is_used_for_price_rules' => '0',
'is_wysiwyg_enabled' => '0',
'is_html_allowed_on_front' => '0',
'is_visible_on_front' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'frontend_label' => array($name)
);
$model = Mage::getModel('catalog/resource_eav_attribute');
$model->addData($attr_data);
$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
$model->setIsUserDefined(1);
if(is_numeric($sortOrder)){
$model->setPosition($sortOrder);
}
try {
$model->save();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
//-------------- add attribute to set
$attribute_set_id=$setup->getAttributeSetId('catalog_product', $set);
// $attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
$attribute_id=$setup->getAttributeId('catalog_product', $name);
$groupId = $this->createGroupInSet($attribute_set_id,"AE");
$setup->addAttributeToSet('catalog_product',$attribute_set_id,$groupId, $attribute_id);
$this->existingAttrs[$name] = "";
} catch (Exception $e) {
$this->echoOut( 'ERROR: Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage() );
}
}
Ideally I am looking for a "what might be going on is..." or "it doesn't work because..." type answer here - I want to understand the process. Maybe it is the simple act of editing the attribute in the admin that triggers something to be set somewhere that makes it "complete"?
* EDIT * Turns out I do not even have to change any of the attribute settings - I just need to load it then save it the admin. Ideas?
Thanks