Programmatically Update Attribute Option Values for Each Store View

attributesproduct-attribute

I have an attribute with the input type Dropdown named "Example Attribute" and two Store Views.

This attribute has a few options, but only the Admin values are set:

enter image description here

How can I programmically modify the values for each Store View? For example let's say I'd like to add the name of the Store View to each value:

enter image description here

I have found some very complicated answers which basically propose to completely overwrite all the values, but none did exactly what I need. I have tried various snippets of code that I found but none have worked for me. I've also looked through Magento's core classes but I was unable to find anything related to my problem.

Best Answer

While all of the above answers are correct, I have also found that if you want to just update one option, you can use a shorthand version:

No need to update all the array of options, just set the one you like, like this

Here is an example adding an option to an attribute with values:

//option id = 42
//store value => beer
//default value => Beer
//2nd store value => Bière (french)


//load attribute as in other examples
$attribute = $attribute_model->load($attribute_code);

$option_id = 42; // NUMERIC VALUE
$opt_default_name='beer';
$opt_default_store = 'Beer';
$opt_2nd_store = 'Biére';

$attribute->setData('option', array('value'=> array(
    $option_id => array ($opt_default_name, $opt_default_store,  $opt_2nd_store));
)));
$attribute->save();

This will just modify the option "beer" for the default and second store (ok. the example is quick, just check the others for correctly getting store IDs..)