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,
		];
		$attributeSet->setData($data);
		$attributeSet->validate();
		$attributeSet->save();
		$attributeSet->initFromSkeleton($attributeSetId);
		$attributeSet->save();
	}
	}
} ?>

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

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Namespace_ModuleName" setup_version="1.1" />
</config>

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

Related posts

One thought on “Creating Custom Attribute Set in Magento 2 via UpgradeData.php

  1. Fabian

    The save function will be deprecated for entities as far as I know

Leave a Reply

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

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">