Add attribute options Magento programmatically

This tutorial explains how to import the attribute options to Magento backend from the CSV pro-grammatically.

Please see the sample table below :

CSV to Magento Options

Here I am explaining how to read the colour values from the CSV file and create the magento attribute options as shown below using the php coding.
Magento Options

Step by step explanation :

  1. Connect to the Magento
  2. Read and sort data from the CSV
  3. Create attribute from the CSV data

Connect to the Magento

As first step we need to connect to the magento database for that include the Mage.php and create mage object as shown below.

require_once '../app/Mage.php';
ini_set('display_errors', 1);
Mage::app('admin');
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Read and sort data from the CSV

As the next step read the CSV data and store the color values in the options array . Remove the duplicated values from the array using the array_unique() function and sort the data using asort() function.

function readOptions() {
    
    $options = $uniqueValues = array();
    $file = 'csv/options.csv';
    $handle = fopen($file, "r");
    $headers = false;
    
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                 $options[] = trim($data[4]);
            }
        }
        fclose($handle);
    }
    $uniqueValues = array_unique($options);
    asort($uniqueValues);
    $optionValues = array_values($uniqueValues);
    createAttribute($optionValues);
}

Create attribute from the CSV data

For inserting the color values to the color attribute first we need to retrieve the attribute id . Then we need to assign the color values to the options array as shown below.

//Reading existing options
function readExistingAttribute( $attributeCode ) {
    
    $attr = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode );
    $options = $attr->getSource()->getAllOptions();
    $optionsExists = array();
    
    foreach($options as $option) {
        $optionsExists[] = $option['label'];
    }
    
    return $optionsExists;
    
}

// Compare with Existing options
 
function cmpOptions( $optionsValues, $optionsExists ) {
  
    $newOptions = array_diff( $optionsValues, $optionsExists );
    return $newOptions;
    
}

function createAttribute( $options , $attributeCode) {
    $option = array('attribute_id' => 
    Mage::getModel('eav/entity_attribute')->getIdByCode(
         Mage_Catalog_Model_Product::ENTITY, 
         $attributeCode
        )
    );

    for ($i = 0; $i < count($options); $i++) {
        
        $option['value']['option'.$i][0] = $options[ $i ]; // Store View
        $option['value']['option'.$i][1] = $options[ $i ]; // Default store view
        $option['order']['option'.$i] = $i; // Sort Order
        echo 'Insert new option : '.$options[ $i ].PHP_EOL;

    }

    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->addAttributeOption($option);
}

If you have any further queries please leave a reply.

Share this Tutorial

2 comments on “Add attribute options Magento programmatically

  1. jalal hab on

    Hello
    I am a beginner in magento
    I would like to know how to import my attributes in cvs form because on your script I have figured out how to use your php file

    Reply
    • pearl on

      Please create the php file in the root or shell folder.Then navigate to the shell folder through the command prompt and run php filename.php

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*