02 May, 2012

Add/Update Attribute Option Values Programmatically in Magento

10:45 AM

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);

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

8 comments:

  1. 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?

    ReplyDelete
  2. @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.

    ReplyDelete
  3. In your code,
    $option['value']['any_option_name1'][0] = $arg_value1;
    $option['value']['any_option_name2'][0] = $arg_value2;

    How can I add the POSITION & IsDefault of 'any_option_name1' or 'any_option_name2' ?

    Thanks.

    ReplyDelete
  4. Here is my code refering your code.

    $attribute_model->setData('option', array(
    'value' => array(
    'any_option_name1' => array(0=>'Black', 1=>'검정', 6=>'黑色'),
    'any_option_name2' => array(0=>'Red', 1=>'빨강', 6=>'水洗'),
    )
    ));

    I would like to add any value corresponding the "POSITION and IsDefault"
    Is it possible ?

    Thanks

    ReplyDelete
    Replies
    1. @COBAY, Pleas refer this:
      $value = array();
      $order = array();

      // Adding two new options to my select
      $value['any_option_name1'] = array('0' => 'Black', '1' => '검정');
      $order['any_option_name1'] = '0';

      $value['any_option_name2'] = array('0' => 'Red', '1' => '빨강');
      $order['any_option_name2'] = '1';


      $results = array('value' => $value, 'order' => $order);
      $attribute_model->setData($results);

      Please refer to original article at http://www.webspeaks.in/2012/05/addupdate-attribute-option-values.html

      Delete
    2. ahhh, "order" field.
      It works well.
      Thank you and good article.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete

 

© 2013 Jquery Giants. All rights resevered. Designed by Templateism

Back To Top