How to add attribute options values into dropdown attribute programmatically

Written by Jigar Patel

Jun 17, 2023

How to add attribute options values into dropdown attribute programmatically

In Magento 2, attribute options are used to define the selectable values for dropdown, multiselect, and other similar attributes. These options allow you to provide predefined choices for customers when they are selecting attribute values for products.

Add attribute options values into a dropdown attribute in the Magento 2 Admin panel, following these steps:

  1. Log in to your Magento 2 Admin panel.
  2. Navigate to Stores -> Attributes -> Product.
  3. Find the attribute you want to modify and click on its row to edit it.
  4. Scroll down to the Manage Labels/Options section.
  5. Under the Options tab, you’ll see a list of existing attribute options (if any). To add a new option, click on the Add Option button.
  6. In the Option Value field, enter the label for the new option. For example, if you want to add an option “Option 1”, enter “Option 1” in the field.
  7. If you want to assign a specific value to the option, you can enter it in the Sort Order field. Otherwise, Magento will assign a value automatically.
  8. Click on the Save Attribute button to save the changes.
  9. Repeat steps 5-8 for each additional option you want to add.
  10. Once you’ve added all the desired options, click on the Save Attribute button to save the attribute.

The attribute options you added will now be available in the dropdown attribute when you create or edit products in the Magento 2 Admin panel. Remember to clear the cache if necessary to see the changes immediately.

Add attribute options values  into an exits dropdown attribute in Magento 2 programmatically using CSV, you can follow these steps:

  1. Create CSV with color and attribute values as shown below & keep your CSV at colorOptions.csv

How to add attribute options values into dropdown attribute programmatically

  1. Create colorsAttribute.php at Magento 2 root directory and paste the below code & execute the script using the command line
<?php

    ini_set('display_errors', '1');
    ini_set('error_reporting', E_ALL);

    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $entityType = 'catalog_product';
    $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
    $path  =  $directory->getRoot().'/scripts/';
    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
    $eavSetup = $objectManager->get('\Magento\Eav\Setup\EavSetup');
    $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
    $stores = $storeManager->getStores();
    $storeArray[0] = "All Store Views";
    foreach ($stores  as $store) 
    {
       $storeArray[$store->getId()] = $store->getName();
    }

    $fname = 'colorOption.csv';

    $file = fopen($path.$fname, "r");
    while (($data = fgetcsv($file, 100000, ",")) !== FALSE)
    {
        foreach((array)$data[0] as $attributeCode)
        {
        $option = array();
        $attribute = $eavConfig->getAttribute($entityType, $attributeCode);
        $option['attribute_id'] = $attribute->getAttributeId();
        $options = $attribute->getSource()->getAllOptions();
        if($data[1])
        {
        foreach((array)$data[1] as $key => $value)
        {
            $str = '"'.$value.'"';
            $option['value'][$str][0]=str_replace('"','', $str);
            foreach($storeArray as $storeKey => $store) 
            {
              $option['value'][$str][$storeKey] = str_replace('"','', $str);
            }   

        }

        $eavSetup->addAttributeOption($option);


        }
        }
    }
        fclose($file);

    echo "Attribute option values has been associated to color attribute SUCCESSFULLY";
  1. run Re index from command line php bin/magento indexer:reindex

I Hope, This instruction will be helpful for you.
Happy Coding with magento2!! 🙂
If you have any difficulties regarding this blog, do consider posting them in the Comments section below!

I’m here to help.
Thank you!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP