Import attribute options magento 2 programmatically

This tutorial explains how to import attribute options magento 2 programmatically.Here it adding the attribute options progammatically from the CSV file.

Please click here to refer the tutorial that explains how to delete attribute options programmatically and other magento tutorials.

The following code reads the data from the options as shown below and compare that with the existing attribute options and add the new options programmatically.

import attribute options magento 2 programmatically

The above figure shows the existing attribute options.

Step by step explanation

  • Read attribute options
  • Get existing attribute options
  • Identify new attribute options
  • Import attribute options programmatically.

create-attribute-options-magento-2-programmatically

Read attribute options

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

use \Magento\Framework\App\Bootstrap;
include('../app/bootstrap.php');

// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');

Get existing attribute options

Click here for detailed explanation.

function getExistingOptions( $object_Manager ) {
    
    $eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
    $attribute = $eavConfig->getAttribute('catalog_product', 'color');
    $options = $attribute->getSource()->getAllOptions();

    $optionsExists = array();
    
    foreach($options as $option) {
        $optionsExists[] = $option['label'];
    }
    
    return $optionsExists;

}

Import attribute options programmatically

// add new attribute options
function addNewOptions( $object_Manager, $newOptions, $storeArray ) {
    
    $option = array();
    $eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
    $attribute = $eavConfig->getAttribute('catalog_product', 'color');
    $option['attribute_id'] = $attribute->getAttributeId();
    
    foreach($newOptions as $key => $value){
        
        $option['value'][$value][0]=$value;
        
        foreach($storeArray as $storeKey => $store) {
            $option['value'][$value][$storeKey] = $value;
        }   
        echo 'Added value : '.$value.PHP_EOL;
    }
 
    $eavSetup = $object_Manager->get('\Magento\Eav\Setup\EavSetup');
    $eavSetup->addAttributeOption($option);
}

// Compare with Existing options

function cmpOptions( $optionsValues, $optionsExists ) {
  
    $newOptions = array_diff( $optionsValues, $optionsExists );
    return $newOptions;
    
}

// Get all stores

function getAllStores( $object_Manager ) {
    $storeManager = $object_Manager->get('Magento\Store\Model\StoreManagerInterface');
    $stores = $storeManager->getStores();
    $storeArray[0] = "All Store Views";       

    foreach ($stores  as $store) {
        $storeArray[$store->getId()] = $store->getName();
    }
    return $storeArray;
}

Please click here to refer the tutorial that explains how to delete attribute options programmatically and other magento tutorials.

If you any further queries please leave a reply.

Share this Tutorial

2 comments on “Import attribute options magento 2 programmatically

  1. Randy B on

    Great resource, thank you very much.
    In readOptions() function there is a reference to unlisted function:
    createAttribute($optionValues);
    Can you provide a Magento 2 suitable function (I saw one is included for Magento 1 tutorial).
    Thanks!

    Reply

Leave a Reply

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

*