Add/Update Attribute Option Values Programmatically in Magento

17 comments
Adding/Updating the attribute option values programmatically in Magento can be tedious sometimes. Adding attribute options through custom codes is not big deal but updating the values for already added attribute options is tricky enough.
Problem: Lets understand this by an example. Suppose we have a product attribute 'manufacturer'. It is of type select/dropdown. And we have few options added in it like Mac, Google, Android etc. Now at some point in our Magento application we want to update these options with Apple, Nexus, HTC etc without using the admin panel, then it is a problem.


Solution: To update the option values programmatically, I came up with a simple solution which is as follows. For this, you need to find the 'option_id' of the attribute options in the 'eav_attribute_option_value' table.

Then create an option array and save it in the appropriate model.


Updating Attribute options

//Get the eav attribute model
$attr_model = Mage::getModel('catalog/resource_eav_attribute');

//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);

//Create an array to store the attribute data
$data = array();

//Create options array
$values = array(
 //15 is the option_id of the option in 'eav_attribute_option_value' table
 15 => array(
   0 => 'Apple' //0 is current store id, Apple is the new label for the option
  ),
 19 => array(
   0 => 'Nexus'
  ),
 12 => array(
   0 => 'HTC'
  ),
);

//Add the option values to the data
$data['option']['value'] = $values;

//Add data to our attribute model
$attr_model->addData($data);

//Save the updated model
try {
 $attr_model->save();
 $session = Mage::getSingleton('adminhtml/session');
 $session->addSuccess(
  Mage::helper('catalog')->__('The product attribute has been saved.'));

 /**
  * Clear translation cache because attribute labels are stored in translation
  */
 Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
 $session->setAttributeData(false);
 return;
} catch (Exception $e) {
 $session->addError($e->getMessage());
 $session->setAttributeData($data);
 return;
}
This is all and you are done.

Adding new Attribute option

$arg_attribute = 'manufacturer';
$arg_value = 'value 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);

17 comments

I am little bit confused! I just want to make sure, did you mean update old value (attribute which is manufacturer ) with new one? If that so, what will happen ordered item attribute values?

@Oğuz Çelikdemir
I am not updating the attribute itself. In fact I am updating the 'options' for that attribute. Suppose you want to change the name of a manufacturer from 'A' to 'X' through your code, then this article is for you.

Thank you , i have little confused where can use this php code in magento

Hello,

For Add Product Options Value Using Magento API and Excel Sheet great help at folllowing url :

http://www.webslike.com/Thread-How-To-Add-Product-Options-Value-Using-Magento-API-and-Excel-Sheet

Hello,

For Add Product Options Value Using Magento API and Excel Sheet great help at folllowing url :

http://www.webslike.com/Thread-How-To-Add-Product-Options-Value-Using-Magento-API-and-Excel-Sheet

This is actually really useful and pointed me in the right direction. Not sure why its so hard to dig this information out of the magento website...

ThanX Buddy :)

How to get Last insert attribute options value id

How can I set labels for specific store view

How can I set labels for specific store view?

Where to place this code in magento ?

We would love to hear from you...

back to top