How to programmatically add attribute option values in data upgrade script?

Add bellow code in your upgrade script file

<?php   
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

if ($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']        =  array('Red','Black', 'Yellow');
    $installer->addAttributeOption($option);
}

//OR
/*
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']['r'][0] = 'Red';
    $option['value']['b'][1] = 'Black';
    $option['value']['y'][2] = 'Yellow';
    $installer->addAttributeOption($option);
}*/

$installer->endSetup();

Check duplicate option value code:

<?php   
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

 if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $newOptions =  array('Red','Black', 'Yellow');
    $exitOptions =  array();
    $options = Mage::getModel('eav/entity_attribute_source_table')
                        ->setAttribute($attribute)
                        ->getAllOptions(false);
    foreach ($options as $option) {
        if (in_array($option['label'], $newOptions)) {
            array_push($exitOptions, $option['label']);
        }else {

        }
    }
    $insertOptions = array_diff($newOptions, $exitOptions);
    if(!empty($insertOptions)) {
        $option['attribute_id'] = $attribute->getId();
        $option['value']        =  $insertOptions;  
        $installer->addAttributeOption($option);
    }            
}

$installer->endSetup();

try this,

for single value:-

$arg_attribute = 'color';
$arg_value = 'red';

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();

$option['attribute_id'] = $attr_id;
$option['value']['any_option_name'][0] = $arg_value;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

for multiple values:-

$arg_attribute = 'color';
$key_data = array('red','black','orange');
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
foreach($key_data as $key_value)
{   
    $option = array();
    $arg_value = trim($key_value);
    $attr_id = $attr->getAttributeId();
    $option['attribute_id'] = $attr_id;
    $option['value']['any_option_name'][0] = $arg_value;
    $setup->addAttributeOption($option);
}

'any_option_name' would be a color_name (ex: red) arg_value would be it's integer optionId afaik.

The thing that would also need to be acquired first, is what the next unused optionId is. To be used for this new attribute option.


For example, you want to add Men value to gender option.

First you have to create your upgrade script in module directory, e.g. app/code/local/MyCompany/MyModule/data/mymodule_setup/data-upgrade-0.1.0-0.1.1.php.

Then fill it with code like this:

<?php

$this->startSetup();

$genderAttribute = Mage::getModel('eav/entity_attribute')
    ->loadByCode('catalog_product', 'gender'); // 'gender' is your attribute code

$this->addAttributeOption([
    'attribute_id' => $genderAttribute->getId(),
    'value' => [[0 => 'Men', 1 => 'Men', 10 => 'Men']] // array indexes are store IDs
]);

$this->endSetup();