Reading list Switch to dark mode

    How to programmatically add more options to an attribute in magento2

    How to programmatically add more options to an attribute in magento2.

    The attributes are basically the property of the product. And, the value of the attributes is known as the attribute options. By default, the attributes of a product are the name of the product, its description, and its price. 

    Suppose you have created a custom attribute with some default options  and now you want to add more options afterward. Then you can create more options later with the help of this blog.

    Here I am using an attribute “color_test” i.e. attribute_code and now explaining how to add options to attribute.


    Searching for an experienced
    Magento 2 Company ?
    Read More

    Step 1 : Create construct method in your controller and inject all the dependencies that are mentioned below :

    protected $_eavSetupFactory;
    protected $_storeManager;
    protected $_attributeFactory;
    public function __construct(
        Context $context,        
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory
    ) {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_storeManager = $storeManager;
        $this->_attributeFactory = $attributeFactory;

    Step 2 : After creating the constructor create a method in your controller named


    Inside “addOptionsToAttribute” function,

    Create an array of options which you want to add to attribute an example for the same is given below : 

    $attribute_arr = ['Yellow','White','Black'];

    Step 3 : Now Load the attribute by attribute code. Here I have taken an example of attribute code as  “color_test” to get attribute id

    $attribute_id = $attributeInfo->getAttributeId();

    Step 4 : Now create attribute options array to assign the option to the desired attribute.

    $option['attribute_id'] = $attributeInfo->getAttributeId();
    foreach($attribute_arr as $key=>$value){
    $option['value'][$value][0]=$value; }

    Step 5 : Now add this option array to attribute to your desired attribute using EavSetupFactory as shown below :

    $eavSetup = $this->_eavSetupFactory->create();


    Now execute this code, you will see that the desired attribute will be having  these options in magento 2 .

    This is how you can programmatically add more options to any desired attribute in magento2.


    You can also visit below links :

    . . .
    Add a comment

    Leave a Comment

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


  • Erfan Imani
    Why not use `AttributeOptionManagementInterface`?
  • Fred Orozco Diaz
    Be careful using method. Using the MagentoEavSetupEavSetupFactory or even the MagentoCatalogSetupCategorySetupFactory class may lead to the following problem:
  • Matt Timmins
    I get this error:

    Parse error: syntax error, unexpected ‘protected’ (T_PROTECTED), expecting end of file in /home/jellyrol/public_html/import_values.php on line 3

    I uploaded the file to the Magento Root folder. Is this correct?

    Hope you can advise.

  • Back to Top
    I really appreciate the quality of support they are very helpful I recommended this powerful module best solution with excellent support.
    Rzouga Aloui
    Talk to Sales


    Live Chat

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home