1

I am adding new options to some existing attributes using the method below.

$option['attribute_id'] = $attribute->getId();
$option['value'][$value][0] = $value;
Mage::getModel('eav/entity_attribute')->addAttributeOption($option);

This is working fine but how can I insert a position/sort value for the option.

asked Nov 18, 2015 at 17:07

1 Answer 1

1

This is function addAttributeOption($option) in app/code/core/Mage/Eav/Model/Entity/Setup.php

 public function addAttributeOption($option)
 {
 $optionTable = $this->getTable('eav/attribute_option');
 $optionValueTable = $this->getTable('eav/attribute_option_value');
 if (isset($option['value'])) {
 foreach ($option['value'] as $optionId => $values) {
 $intOptionId = (int) $optionId;
 if (!empty($option['delete'][$optionId])) {
 if ($intOptionId) {
 $condition = array('option_id =?' => $intOptionId);
 $this->_conn->delete($optionTable, $condition);
 }
 continue;
 }
 if (!$intOptionId) {
 $data = array(
 'attribute_id' => $option['attribute_id'],
 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
 );
 $this->_conn->insert($optionTable, $data);
 $intOptionId = $this->_conn->lastInsertId($optionTable);
 } else {
 $data = array(
 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
 );
 $this->_conn->update($optionTable, $data, array('option_id=?' => $intOptionId));
 }
 // Default value
 if (!isset($values[0])) {
 Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
 }
 $condition = array('option_id =?' => $intOptionId);
 $this->_conn->delete($optionValueTable, $condition);
 foreach ($values as $storeId => $value) {
 $data = array(
 'option_id' => $intOptionId,
 'store_id' => $storeId,
 'value' => $value,
 );
 $this->_conn->insert($optionValueTable, $data);
 }
 }
 } else if (isset($option['values'])) {
 foreach ($option['values'] as $sortOrder => $label) {
 // add option
 $data = array(
 'attribute_id' => $option['attribute_id'],
 'sort_order' => $sortOrder,
 );
 $this->_conn->insert($optionTable, $data);
 $intOptionId = $this->_conn->lastInsertId($optionTable);
 $data = array(
 'option_id' => $intOptionId,
 'store_id' => 0,
 'value' => $label,
 );
 $this->_conn->insert($optionValueTable, $data);
 }
 }
}

You can using array have index 'sort_order' to sort attribute. If it not setup, it have value as 0.

answered Nov 18, 2015 at 17:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.