Pre Black Friday Sale. 20% OFF on all products. Coupon code: fme20off

How to Create Attribute Set Programmatically in Magento 2?

How to Create Attribute Set Programmatically in Magento 2?

Magento is undoubtedly a powerful eCommerce platform. The topmost reason merchants prefer it over other platforms is because of its customisability and rich features. Speaking of features, Magento easily allows merchants to create and assign attribute set. In this article, we will discuss everything there is to know about Magento 2 attribute set and how to create a Magento attribute set programmatically.

What is a Magento Attribute Set?

It demonstrates 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 considerable time and efforts. On top of it, there’s a risk of things going wrong, thereby undermining the user experience.

Therefore, a better approach is to do it programmatically. By automating the Magento 2 create attribute set feature, you can save valuable time and effort that can be used elsewhere. If you have very few product catalogs, you may opt for the manual method as it certainly much easier.

Magento 2 Create 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.
<?php
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)
 {
 $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' => 'My_Custom_Attribute_Set',
 'entity_type_id' => $entityTypeId,
 'sort_order' => 50,
 ];
 $attributeSet->setData($data);
 $attributeSet->validate();
 $attributeSet->save();
 $attributeSet->initFromSkeleton($attributeSetId);
 $attributeSet->save();

 $setup->endSetup();
 }
}

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.

Final Thoughts on Magento Attribute Set

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:

This blog was created with FME's SEO-friendly blog