Insert attribute option and return option id in Magento 1.x
This article presents three methods to add attribute options in a loop and return the inserted option_id correctly.
If you do this in a loop to add the options, you may find out that if you just try to getOptionId($value) on the attribute will return the option_id correctly only for the first inserted option, the others returning null and actually whole getAllOptions() will return an invalid options array.
Side note – what is the difference between option_id and value_id ?
There may be a lot of confusion around attributes option_id, option, value, value_id, label.
label vs title -vs- option vs value
- Because attributes are using the ‘label’ term for the attribute title on each store, most dev’s are also using the term label when referring to attribute option values.
- in the backend, it’s named ‘option’. option value represents the value per store for the option. These are found in the backend, under “Manage Options (values of your attribute)”
- in the database, it’s stored in a field named ‘value’
option_id vs value_id
There are two tables keeping the options:
- eav_attribute_option
- eav_attribute_option_value
Because Magento allows setting different values per store, value_id is the unique identifier of each store value. So for every unique option_id, there can be multiple value_id’s assigned to it, each assigned to a store_id.
The methods
ini_set('display_errors', 1);
require_once 'app/Mage.php';
Mage::app();
$attributeCode = 'mydropdown';
$attributeValues = array('a', 'b', 'c');
foreach ($attributeValues as $attributeValue) {
//choose one method and comment the other ones
///--------------------METHOD 1 - failsafe
//add the new option
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$value['option'] = array($attributeValue);
$result = array('value' => $value);
//$attribute->setData('option', $result);
$attribute->setData('option', $result);
$attribute->save();
//get its option_id
$optionId = Mage::getModel('eav/entity_attribute_source_table')
->setAttribute($attribute)
->getOptionId($attributeValue);
var_dump($optionId);
///--------------------METHOD 2 - to be retested as it may fail
//add the new option
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$value['option'] = array($attributeValue);
$result = array('value' => $value);
//$attribute->setData('option', $result);
$attribute->setData('option', $result);
$attribute->save();
//get option id, don't reuse $attribute, use $attr for some reason to be uncovered
$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($attributeCode);
$optionId = null;
if ($attr->usesSource()) {
$optionId = $attr->getSource() //returns Mage_Eav_Model_Entity_Attribute_Source_Table and does setAttribute()
->getOptionId($attributeValue);
}
var_dump($optionId);
///--------------------METHOD 3 - fastest
//add the new option
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$option['attribute_id'] = $attribute->getId();
$option['value']['option'][0] = $attributeValue;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
//get its option_id
$valueId = $setup->getConnection()->lastInsertId();
$optionId = Mage::getModel('eav/entity_attribute_option')
->getCollection()
->setStoreFilter(0)
->addFieldToFilter('tsv.value_id', array('eq' => $valueId))
->getFirstItem()->getId();
var_dump($optionId);
}



