Question

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;
            }else{
                $option['attribute_id'] = $attr_id;
                $option['value'][$value][0] = $value;
                $option['value'][$value][1] = $value;

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


            }

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?

Thanks!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top