0

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.

  • strange that peploes downvoted for you too :-) – Baby in Magento Aug 5 '16 at 13:56
2

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, privacy policy and cookie policy

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