I'm using a script to programmatically create attribute options - the code is as follows:

            echo "Creating option ....\n";
            $attr_model = Mage::getModel('catalog/resource_eav_attribute');
            $attr = $attr_model->loadByCode('catalog_product', $key);
            $attr_id = $attr->getAttributeId();
            if (!$attr_id){
                    echo "Cannot find Attribute $code\n";
                    return 0;
                $option['attribute_id'] = $attr_id;
                $option['value'][$value][0] = $value;
                $option['value'][$value][1] = $value;

                $setup = new Mage_Eav_Model_Entity_Setup('core_setup');


And for most values it's working absolutely fine.

However certain values are not being created - it's not throwing any errors, but it doesn't create the options.

One example is 55 mg/kg, which makes me suspect that it's an escaping issue.

Are there any particular escaping rules that I need to follow for this?


1 Answer 1


Having dug into this further, I've found out that if the array key you use in the $option['value'] array starts with a number, Magento treats this differently as part of the addAttributeOption() call (casting it as an (int), and then assuming you are editing an existing attribute option)

Ensuring the array key doesn't start with a number (I did this by pre-pending 'opt' onto the key) solves the issue.

  • First could be the option_id(don't know for sure if setup handles it the same) So start with changing [$value] to [0] and with more options increment more
    – Jeroen
    Mar 12, 2015 at 19:57

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.