Categories: General

How to Create Attribute Set Programmatically & Assign Attributes to It in Magento 2

When your Magento 2 stores is bulked with too many attribute sets or while importing from older Magento to a newer version, you may face some issues like older attributes cannot fit the newer versions or attribute sets cannot be managed manually. Creating new attribute set and assigning attributes to this newly created attribute set may be a cumbersome task if done manually as you have to follow local development to live publication process. What if you can automate creating attribute set and assign attributes to it programmatically in Magento 2?! It seems like saving efforts and time, right? Today, MageComp has come up with the simplest way to create attribute set programmatically & assign attributes to it in Magento 2.

All you need to do is, go to Vendor/Extension/Setup/InstallData.php and paste this and it will work like charm to create attribute set automatically.


That’s it, if you have done everything properly, it will definitely create new attribute set with assigned attributes inside your Magento 2 store backend. Feel free to ask questions or send your feedback regarding this code through commenting.
Happy Coding!

Click to rate this post!
[Total: 8 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

    • InstallData is only read when installing the module, so bin/magento setup:upgrade does not update.

      Delete your YourVendor_YourModule from the setup_module table and run bin/magento setup:upgrade again

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago