How To Create Custom Attribute Set Programmatically In Magento 2

To Create Custom Attribute Set Using Installer Script In Magento 2 – Then You need to create a installer file “InstallData.php” in Vendor\CustomModule\Setup and then add following code.

<?php namespace Vendor\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Catalog\Setup\CategorySetupFactory;

class InstallData implements InstallDataInterface { 
    private $attributeSetFactory; 
    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); // Default attribute set Id
        $data = [
            'attribute_set_name' => 'Customattributeset', //attribute set name
            'entity_type_id' => $entityTypeId,
            'sort_order' => 50,
        $attributeSet->initFromSkeleton($attributeSetId)->save(); // based on default attribute set

We are done with that. Now run setup:upgrade command and after complete setup upgrade process check attribute set section on admin and you see new attribut set with name “Customattributeset”.


You are successfully done with that. Bookmark it for your future reference. Do comment below if you have any other questions or doubts on this post.

P.S. Do share this note with your team.