Read More
Read More
Menu Close

    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.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    3 comments

  • 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