I want to add options in an attributes programmatically like in Size attribute i want S,M,L,XL as an option . My code is creating attribute but i want options with attributes.

require_once '../app/Mage.php';

// Attribute basic data
$attributeName = 'Sizes'; // Name of the attribute
$attributeCode = 'Sizes'; // Code of the attribute
//$attributeGroup = 'Custom Group'; // Group to add the attribute to
// Attribute configuration data
$data = array(
'type' => 'varchar', // Attribute type
'input' => 'select', // Input type
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
'required' => true, // Is this attribute required?
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'used_in_product_listing' => true,
// Adding basic data
'label' => $attributeName,

$data['value']['any_option_name'][0] = 'S';

// Installer code to create attribute with new installer class (can be used in non-EAV setup)
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute('catalog_product', $attributeCode, $data);

// Notice: Default Attribute Set have ID 4

// Add the attribute to the all attribute proper sets and proper groups:
$entity = Mage_Catalog_Model_Product::ENTITY;
$attributeSetIds = $installer->getAllAttributeSetIds($entity);
foreach($attributeSetIds as $attributeSetId)
$installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeCode);


You need to create your own source model class such as following:


Then add in your $data array the line:

'source' => 'model URI here'

Reinstall attribute. In eav_attribute table the source model will be changed for "sizes" attribute.

P.S. About model URI you can read here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.