{"id":167,"date":"2018-08-29T12:25:02","date_gmt":"2018-08-29T12:25:02","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/08\/29\/create-attribute-set-programmatically-in-magento-2\/"},"modified":"2025-05-22T17:21:05","modified_gmt":"2025-05-22T11:51:05","slug":"create-attribute-set-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-attribute-set-programmatically-in-magento-2\/","title":{"rendered":"How to Create Attribute Set Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An attribute set is a group of multiple individual product attributes that define&nbsp;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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,&nbsp;today I\u2019ve come up with the custom code to&nbsp;<em><strong>create&nbsp;attribute set programmatically in Magento 2.&nbsp;<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Script to create attribute set programmatically in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Setup;\n \nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Catalog\\Setup\\CategorySetupFactory;\nuse Magento\\Eav\\Model\\Entity\\Attribute\\SetFactory as AttributeSetFactory;\n \nclass InstallData implements InstallDataInterface\n{\n   private $attributeSetFactory;\n   private $attributeSet;\n   private $categorySetupFactory;\n \n   public function __construct(AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory )\n       {\n           $this->attributeSetFactory = $attributeSetFactory;\n           $this->categorySetupFactory = $categorySetupFactory;\n       }\n \n   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n   {\n       $setup->startSetup();\n       $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);\n \n       $attributeSet = $this->attributeSetFactory->create();\n       $entityTypeId = $categorySetup->getEntityTypeId(\\Magento\\Catalog\\Model\\Product::ENTITY);\n       $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);\n       $data = [\n           'attribute_set_name' => 'MyCustomAttribute',\n           'entity_type_id' => $entityTypeId,\n           'sort_order' => 200,\n       ];\n       $attributeSet->setData($data);\n       $attributeSet->validate();\n       $attributeSet->save();\n       $attributeSet->initFromSkeleton($attributeSetId);\n       $attributeSet->save();\n \n       $setup->endSetup();\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also create attribute set in Magento 2 from the admin panel with the method given in&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-attribute-set\/\" target=\"_blank\" rel=\"noreferrer noopener\">guide to Magento 2 attribute set<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An attribute set is a group of multiple individual product attributes that define&nbsp;the characteristics of the products. It is used every time while creating a&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":15613,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/15613"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}