6 December, 2023
Custom Attribute Set in Magento 2

Creating Custom Attribute Set in Magento 2 via UpgradeData.php

Attribute Set:

Attribute set is a list of certain relevant attributes to each product types. It is used each time when a new product is created. Magento 2 provides a Default attribute set. Users also have the option to create their own custom attribute sets in Magento 2.

In this article, I will explain how you can create custom attribute sets in Magento 2. And also

Read About : Ho to Create a Product Attribute in Magento 2

Why via upgradeData:

If we need to insert any table related data (like attributes/attributeset/attibutegroup) after the module installation, we go for upgradeData.

* We declare the namespace of our module to avoid conflicts around multiple modules and introduce more flexibility

namespace Apptha\Blog\Setup;

*The below are the namespaces and and classes to be included in order to create new custom attribute set programmatically

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

* UpgradeDataInterface brings the ‘upgrade’ method which must be implemented
class UpgradeData implements UpgradeDataInterface
 	private $eavSetupFactory;
	private $attributeSetFactory;
	private $attributeSet;
	private $categorySetupFactory;
   	public function __construct(EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory )
        		$this->eavSetupFactory = $eavSetupFactory; 
        		$this->attributeSetFactory = $attributeSetFactory; 
        		$this->categorySetupFactory = $categorySetupFactory; 
 public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
      *checking the current version of the module 
      *this function is implemented from  ModuleContextInterface
    if(version_compare($context->getVersion(), '1.1', '<')) {
		$attributeSet = $this->attributeSetFactory->create();
		$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
		$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
		$data = [
		'attribute_set_name' => 'Apptha_Custom_Attribute_Set', // define custom attribute set name here
		'entity_type_id' => $entityTypeId,
		'sort_order' => 200,
} ?>

Add these code in upgradeData.php under app/code/namespace/module/setup and update the version in module.xml under app/code/namespace/module/etc as below


Upgrade the module setup using upgrade command via CLI and see the custom attribute set is created.

The command to upgrade the module via CLI is given below.

php bin/magento setup:upgrade

Alex Sam is a digital marketer by choice & profession. He munches on topics relating to technology, eCommerce, enterprise mobility, Cloud Solutions and Internet of Things. His other interests lies in SEO, Online Reputation Management and Google Analytics. Follow Me Socially: Habr , Dev.to & Viblo .

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *