1

I am trying to use the function addAttributeOption to add a new option to an attribute. I do not want to change the current option order or defaults but simply want to append a new option to the end of the list.

Firstly I tried to get all the options on the attribute and simply append the new one as follows.

$attributeModel->getSource()->getAllOptions();

But then it complains about the lack of default value. Ideally I do not want to have to reformat the array returned from getAllOptions as it only returns value and label.

1
  • strange that peploes downvoted for you too :-) Commented Aug 5, 2016 at 13:56

2 Answers 2

3

Ok so it turns out I was being silly. The function addAttributeOption allows you to add and update options.

To add a new option you simply need an array with the following format.

$option['attribute_id'] = $attributeId;
$option['value'][]      = $storeValues;

Where $storeValues is simply an array where the key is the store id and the value is the value you want for that store.

Then by calling $installer->addAttributeOption($option); your new option will be added.

To update an option you simply need to change your option array to have the option id as the key.

$option['attribute_id']     = $attributeId;
$option['value'][$optionId] = $newStoreValues;
1

this works perfect for me

$arg_attribute = 'manufacturer';
$arg_value = 'new option to be added';

$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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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