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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago