Magento – insert new attribute option programmatically

April 26th, 2012 by laeeq | 2 comments

As We know, its not easy at first to add new attribute options from code in Magento. it took time to find out solution. Actually the main problem is that we need to get the option value of new option after saving that’s essentail point when doing this from Magento.

We can find many functions that allow you to insert new attribute option. In order having option value, I tried to reload options list via Magento attribute model object but we meet Magento cache issue which you never get the new option saved with getAllOptions function.

At last , I got a solution which calling attribute source table directly to read all attribute options. Below are 2 usefull functions which is ready for you to use in your code

Function 1:

  1. public function addAttributeValue($arg_attribute, $arg_value)
  2. {
  3. $attribute_model        = Mage::getModel(‘eav/entity_attribute’);
  4. $attribute_code         = $attribute_model->getIdByCode(‘catalog_product’, $arg_attribute);
  5. $attribute              = $attribute_model->load($attribute_code);
  6. if(!$this->attributeValueExists($arg_attribute, $arg_value))
  7. {
  8. $value['option'] = array($arg_value,$arg_value);
  9. $result = array(‘value’ => $value);
  10. $attribute->setData(‘option’,$result);
  11. $attribute->save();
  12. }
  13. $attribute_options_model= Mage::getModel(‘eav/entity_attribute_source_table’) ;
  14. $attribute_table        = $attribute_options_model->setAttribute($attribute);
  15. $options                = $attribute_options_model->getAllOptions(false);
  16. foreach($options as $option)
  17. {
  18. if ($option['label'] == $arg_value)
  19. {
  20. return $option['value'];
  21. }
  22. }
  23. return false;
  24. }

Function 2:

  1. public function attributeValueExists($arg_attribute, $arg_value)
  2. {
  3. $attribute_model        = Mage::getModel(‘eav/entity_attribute’);
  4. $attribute_options_model= Mage::getModel(‘eav/entity_attribute_source_table’) ;
  5. $attribute_code         = $attribute_model->getIdByCode(‘catalog_product’, $arg_attribute);
  6. $attribute              = $attribute_model->load($attribute_code);
  7. $attribute_table        = $attribute_options_model->setAttribute($attribute);
  8. $options                = $attribute_options_model->getAllOptions(false);
  9. foreach($options as $option)
  10. {
  11. if ($option['label'] == $arg_value)
  12. {
  13. return $option['value'];
  14. }
  15. }
  16. return false;
  17. }

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. danielwiseqz
    September 28th, 2012 at 11:40 | #1

    But wanna comment on few general things, The website design and style is perfect, the content is real fantastic : D.

  2. November 8th, 2012 at 07:12 | #2

    I was more than happy to uncover this great site. I wanted to thank you for your time due to this wonderful read!! I definitely loved every part of it and I have you bookmarked to look at new information in your web site.

  1. No trackbacks yet.