I found this article to add new options for a attribute using program perfectly:
http://www.danneh.org/2011/01/getting-value-of-attribute-option-and-adding-a-new-attribute-option-in-magento/
But every attributes will always create with position '0' and I want to sort them because their values are numbers.
Can I set attribute options' position when I create them or update the position after I create them?
-
this is pretty simple (in magento admin), while you go to Manage Label/ Option tab, there is a box to type 0,1,2 or whatever you need .. if that u are referring to– echoashuCommented Sep 4, 2014 at 7:27
-
@echoashu-I know I can change it using admin panel but currently I want the program can set the option's position in auto when the option is created.– Bruce ChenCommented Sep 4, 2014 at 7:39
Add a comment
|
1 Answer
Try:
public function addAttributeOption($attributeCode, $optionValue, $sortPosition) {
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$option = array('value' => array($optionValue, $optionValue), // set default value, store value
'order' => array($sortPosition)); // set sort posiiton
$attribute->setOption($option);
$attribute->save();
return $this->getAttributeOptionValue($attributeCode, $optionValue);
}