Create an Attribute Set in Magento 2 via Installable Script

Product attributes and attribute sets are playing the major role in Magento catalog. In both the Magento community and enterprise edition, we can create product attributes and attribute sets on the admin side easily. But sometimes like creating new module or customising the native features of Magento, we have to create an installable script to create an attribute set and product attribute.

In one of my previous blog, I have explained how to “Create a Custom Attribute Group & Product Attribute in Magento 2 via InstallData.php”. Here, I am going to show you how to create a product attribute set in Magento 2 programmatically.

Let’s assume that we are going to create a new product attribute set called “Watch” by using the below code.

Step 1: Module Registration

In order to create new module/installable script, first we have to create a new file named registration.php file in the below file path,

File Path: app/code/BlogTreat/MyAttributeSet/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'BlogTreat_MyAttributeSet',
    __DIR__
);

Step 2: Define new module

Then, create a module.xml file in the below file path to define our new Magento 2 extension,

File Path: app/code/BlogTreat/MyAttributeSet/etc/module.xml

<?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="BlogTreat_MyAttributeSet" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Step 3: Create a Setup File

Finally, Create a setup file InstallData.php in the below file path, and in this file, we have to provide the information about our attribute set,

File Path: app/code/BlogTreat/MyAttributeSet/Setup/InstallData.php

<?php
namespace BlogTreat\MyAttributeSet\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
	) {
		$setup->startSetup();
		$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' => 'Watch',
			'entity_type_id' => $entityTypeId,
			'sort_order' => 100,
		];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId)->save();
    }
}

After completing the above steps run the below SSH command in your Magento 2 installed root directory,

php bin/magento setup:upgrade

Then, clear all the Magento cache and check whether the new attribute set has been successfully created on Magento 2 admin by navigating to Magento 2 Admin > STORES > Attribute Set.

Note: We have tested the above code on Magento 2.3.

Hope this helps.

Leave a Reply

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