I have written the following code to create an attribute in magento 1.9
$name = $data['name'];
$attributeSetId = $data['attributeSetId'];
$attributeGroupId = $data['attributeGroupId'];
//create a new attribute
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attribute = array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => $data['title'],
'input' => $data['type'],
'class' => '',
'source' => 'catalog/product_attribute_source_layout',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
);
if(array_key_exists('options', $data) && !empty($data['options']) && $data['type'] == 'select') {
Mage::log('adding select options to attribute');
$values = array();
foreach ($data['options'] as $option) {
$values[$option['value']] = $option['label'];
}
$options = array(
'values' => $values
);
$attribute['option'] = $options;
}
if(array_key_exists('isGlobal', $data) && !empty($data['isGlobal']) && $data['isGlobal']) {
$attribute['global'] = 1;
}
if(array_key_exists('isConfigurable', $data) && !empty($data['isConfigurable']) && $data['isConfigurable']) {
$attribute['is_configurable'] = 1;
}
$entityTypeId = $setup->getEntityTypeId('catalog_product');
$setup->addAttribute($entityTypeId, $name, $attribute);
Which is creating the attribute correctly in Magento, along with the attribute options. Which I can see are correct when I edit the attribute in the admin site.
However when I go to use the attribute on a product the attributes are incorrect.
Instead of being my options, for example (small,medium,large) I am seeing the following values in the dropdown (No layout updates, Empty, 1 column, 2 columns with left bar, 2 columns with right bar, 3 columns). Which are being pulled from the "Page Layout" attribute.
Elsewhere in my code I am pulling the attribute options out from the attribute, when I use the following code I get my correct options:
$options = Mage::getResourceModel('eav/entity_attribute_option_collection');
$attributeOptions = $options->setAttributeFilter($attribute->getId())
->setStoreFilter(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)
->toOptionArray();
When I use the following code, it pulls out the incorrect attribute options:
$attributeOptions = $attribute->getSource()->getAllOptions(false);