1

I know using MAGMI we can import to Magento much easier. For example, if you are importing products with an attribute value that does not exist yet, it will be created on the fly by MAGMI.

However, I have not found a way to import the attribute position value.

This results in attribute being created on the fly and being out of order.

Since I have over 300 different values, I was wondering if there was a way to import my attribute position values as well?

This can be with MAGMI or any other method.

Any help is greatly appreciated!

2

I don't know if this helps you much, but I did something like this using a custom script like this.
In my case I had to read the values from a CSV file, but anything goes as long as you are able to get your data into an array that looks like this:

$values = array(
    array(
        'value'=>'Some value',
        'position'=>'3'
    ),
    ....
);

I also did this for the manufacturer attribute but it works for any other attribute. Let's assume you have the array in the form described above. Here goes:

$manufacturer = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer'); //get the main attribute
$storeViews = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0)); //get all stores
$ids = $storeViews->getAllIds();//get all store ids
$saveOptions = array();//init options to save
if ($manufacturer){
    $_options = $manufacturer->getSource()->getAllOptions(false);//get all existing options so you won't add duplicates
    $options = array();
    foreach ($_options as $_option){ //format the existing options for save
        $options[$_option['label']] = $_option['value'];
    }
}
//read from the array to import $values
foreach ($values as $value){
    if (!isset($options[$name])){//check if the option does not exist
        $index = 'option_'.(count($toInsert) + 1);
        $toInsert[$index] = array();//if it doesn't exist add it to import array
        $toInsert[$index][0] = $value['value'];//set the option label for admin view
        foreach ($ids as $store){
           //empty values for stores - you can also set translated values if you need for each store
           $toInsert[$index][$store] = '';
        }
        $toInsertOrder[$index] = $value['position'];//set the option position
    }
}

Now you should have in the array $toInsert the labels that need to be added, and in $toInsertOrder the positions of the labels. just set the options to the attribute and save it.

$result = array('value' => $toInsert,'order' => $toInsertOrder);
$manufacturer->setData('option', $result);
$manufacturer->save();
| improve this answer | |
0

I have made it and it will definitely help you.

function addOptionAttribute($arg_attribute, $arg_value,$arg_position) {
    $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_options_model->setAttribute($attribute);
    $options = $attribute_options_model->getAllOptions(false);


    $attribute->setData('option', array(
        'value' => array(
            'option' => array($arg_value,$arg_value,$arg_value)
        ),
        'order'=> array(
            'option' => $arg_position
        )
    ));
    $attribute->save();
}
| improve this answer | |

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.