Skip to content

Instantly share code, notes, and snippets.

@lukluk
Created January 22, 2014 02:50
Show Gist options
  • Save lukluk/8552698 to your computer and use it in GitHub Desktop.
Save lukluk/8552698 to your computer and use it in GitHub Desktop.
Magento Add Attribute Option
public function attributeValueExists($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_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}
return false;
}
public function addAttributeValue($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_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
if(!$this->attributeValueExists($arg_attribute, $arg_value))
{
$value['option'] = array($arg_value,$arg_value);
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
}
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}
return true;
}
public function getAttributeValue($arg_attribute, $arg_option_id)
{
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_table = Mage::getModel('eav/entity_attribute_source_table');
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table->setAttribute($attribute);
$option = $attribute_table->getOptionText($arg_option_id);
return $option;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment