How to Create Attribute Set Programmatically in Magento 2
An attribute set is a group of multiple individual product attributes that define the characteristics of the products. It is used every time while creating a new product in Magento 2 to bring all the attribute options to set during data entry which would be available to the customers.
While upgrading to the latest Magento version, migrating to Magento 2 or for custom purpose, you may require to create a bunch of attribute sets. Doing it manually through the Magento 2 backend requires tons of efforts and time. To ease and quick up the task, today I’ve come up with the custom code to create attribute set programmatically in Magento 2.
Script to create attribute set programmatically in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace Vendor\Extension\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; class InstallData implements InstallDataInterface { private $attributeSetFactory; private $attributeSet; private $categorySetupFactory; public function __construct(AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $attributeSet = $this->attributeSetFactory->create(); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $data = [ 'attribute_set_name' => 'MyCustomAttribute', 'entity_type_id' => $entityTypeId, 'sort_order' => 200, ]; $attributeSet->setData($data); $attributeSet->validate(); $attributeSet->save(); $attributeSet->initFromSkeleton($attributeSetId); $attributeSet->save(); $setup->endSetup(); } } |
That’s it!
This is the easiest method to create Magento 2 attribute set programmatically, however, doubts about the implementation are welcome in the comments section below.
You can also create attribute set in Magento 2 from the admin panel with the method given in guide to Magento 2 attribute set.
Rate the post with 5 stars if found useful.
Thank You!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
It’s an amazing piece of writing designed for all the online
visitors; they will get advantage from it I am sure.
Thanks!
Use $this->attributeSetRepository->save($attributeSet); // Magento\Eav\Api\AttributeSetRepositoryInterface
instead of $attributeSet->save();
Thank you for posting this tutorial which is very helpful.
I am a beginner and have one query that is it essential to run commands or not after adding the code to my Magento 2 project?
Waiting for your prompt response…..
Yes Chris,
you need to run the commands after adding the code.
Great Work! Attribute are used for multiple purposes in Magento. This is a very easy to follow guide for creating an attribute and attribute set programmatically in Magento. I found it very easy to understand. thanks