Magento – Set Dropdown Attribute Value of a Product, Add Option if not Present
A quick little function for PHP scripts. Dropdown attributes have a limited set of allowed values. Say you have products that you wish the customer to be able to filter by Manufacturer on the front-end: the manufacturer attribute must be a drop-down in order for it to be filterable. If your products are added from an importer script, the script needs to be able to add a missing Manufacturer to the options of the attribute, if it is not already on the list. This function handles that transparently. If the value exists, it is set. If it does not exist, it is created and then set.
function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) {
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
// determine if this option exists
$value_exists = false;
foreach($options as $option) {
if ($option['label'] == $arg_value) {
$value_exists = true;
break;
}
}
// if this option does not exist, add it.
if (!$value_exists) {
$attribute->setData('option', array(
'value' => array(
'option' => array($arg_value,$arg_value)
)
));
$attribute->save();
}
$product->setData($arg_attribute, $arg_value);
}
An example:
setOrAddOptionAttribute($product, 'manufacturer', 'Samsung');Google+

Hi Scott, thanks for the post. I’ve been trying to set the value of a drop down attribute using your code. Any value that doesn’t exits is created successfully but I can’t get it to save for a product. Does it work for CE 1.6? Any help would be appreciated.
Hi Marc,
I have been using the posted code myself with CE 1.6, so yes, it should work. I may be pointing out what you already know here, but you will need to call $product->save() after calling setOrAddOptionAttribute(). Also, xheck to see if the updated attribute value is showing in the admin area but not on the frontend store. If it is in admin but not the frontend, it could be that you have not saved the product to the correct store and website using $product->setStoreId() and $product->setWebsiteIds(), or it could be that the old value is being cached.
Hi Scotty,
i used your code and it’s really well i also faced same problem like Marc and i also tried whatever solution you suggest then also it doesn’t work for me then finally just two line code adding in your code work for me
$option_id = $attribute->getSource()->getOptionId($arg_value);
$product->setData(‘manufacturer’,$option_id)->save();
i worked a lot to getting this may be it will helpful to any other and save time.
Ok so first, thank you for this code, i’m new to Magento and it really help me. However, in my case (CE 1.7, multiple websites/storeviews) the value store by the product is not the text value but the id. In other words : the data set should be the $option['value'] and not the $option['label'] (here equals to $arg_value). I’m not sure this could be generalised, but if it could help someone else, I would be glad.
THANKS SO MUCH! I was search of this for quite a while. It works, Tanks