Magento – Import Attribute Position Values

attributesimportmagento-1.7magmi

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!

Best Answer

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();
Related Topic