Magento 2How To GuideSeptember 30, 2020

How to Create Attribute Set Programmatically in Magento 2?

How to Create Attribute Set Programmatically in Magento 2?

Attribute sets demonstrate the characteristics of a product. They are a list of individual product attributes that define that particular product and tell customers more about it. Product attributes are visible to customers and affect their purchase decisions. Attribute sets should be created after creating product attributes.

You may need to create new attribute sets while upgrading your Magento 2 version, migrating to Magento 2, or doing some custom work. Although Magento 2 allows this to be done manually, it requires time and efforts with the risks of things going wrong. A better way is to automate this and create an attribute set programmatically.

Following is the script to create an attribute set programmatically in Magento 2.

Note: You need to create an installer file InstallData.php in Company\Mymodule\Setup and add the following code.
namespace Company\Mymodule\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)
 $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' => 'My_Custom_Attribute_Set',
 'entity_type_id' => $entityTypeId,
 'sort_order' => 50,


If it's difficult for you to execute the above script to create attribute sets programmatically, then you can check this magento user guide to add new attribute sets in your store.

If you have any issue in creating attribute set programmatically in Magento 2, then feel free to contact our support team for a quick fix.

Related Articles: