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);

$attributeCode = 'color';
// read new option values from CSV
$newOptionValues = readOptions();
// read existing options
$optionsExists = readExistingAttribute( $attributeCode );
//compare with existing options;
$newOptions = cmpOptions( $newOptionValues, $optionsExists );
createAttribute( $newOptions, $attributeCode);

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]);
    $uniqueValues = array_unique($options);
    $optionValues = array_values($uniqueValues);
    return $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' => 

    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');

If you have any further queries please leave a reply.

Share this Tutorial

6 comments on “Add attribute options Magento programmatically

  1. Koko on

    I’m new on magento an try your code. But I got error
    Exception: Warning: Missing argument 2 for createAttribute()
    I think on ReadOptions Function there are a code all createAttribute(), but the parameter doesn’t match.
    How to fix this?

  2. jalal hab on

    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

    • 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


Leave a Reply

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