Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    How to programmatically add more options to an attribute in magento2

    How to programmatically add more options to an attribute in magento2 – If you have created an attribute of select type with some options and want to add more options to this attribute using code then you can do it by following this post. Here I am using an attribute “color_test” and now explaining how to add options to attribute.

    Step 1 : Create construct method


    Step 2 : Create an array of options which you want to add to attribute


    Step 3 : Load attribute by attribute code “color_test” to get attribute id


    Step 4 : Now create attribute options array


    Step 5 : Now add this option array to attribute


    Now execute this code, you will see the attribute with these options in magento.

    . . .

    Comments (3)

    Add Your Comment

  • 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: https://github.com/magento/magento2/issues/4896
  • 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
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close