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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.