Reading list Switch to dark mode

    How to programmatically add more options to an attribute in magento2

    Updated 19 January 2023

    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 ?
    Find out 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;
        parent::__construct($context);
    }

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

    addOptionsToAttribute.
     

    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

    $attributeInfo=$this->_attributeFactory->getCollection()
                   ->addFieldToFilter('attribute_code',['eq'=>"color_test"])
                   ->getFirstItem();
    $attribute_id = $attributeInfo->getAttributeId();

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

    $option=array();
    $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();
    $eavSetup->addAttributeOption($option);

     

    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 : 

    https://webkul.com/blog/magento2-how-to-create-product-custom-attribute-from-installer-in-custom-module/

    https://devdocs.magento.com/guides/v2.3/extension-dev-guide/attributes.html

    . . .

    Leave a Comment

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


    3 comments

  • Erfan Imani
  • Fred Orozco Diaz
  • Matt Timmins
  • Back to Top

    Message Sent!

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

    Back to Home