Configuring Attribute sets in Magento 2

Working with Attributes sets is one of the first steps for creating a product. The attribute sets will be used as a template for the product record each time when a product is created. Magento 2 supports store owners to configure the default attribute set. In this post, we will show you the way to create default sets for Magento 2.

Create default attribute sets

Create an attribute set

  1. Magento Admin sidebar > Stores > Attributes > Attribute Set > Add New Set
  2. Enter a name in Name box and set the Based On
  3. Click Save Then you will see next page displaying:
  • Edit Attribute Set Name column: the name of attribute set
  • Groups column: list of the current selection of attribute groups
  • Unassigned Attributes column: list of the selection of attributes currently not assigned to the attribute set
  1. Drag the attribute from Unassigned Attributes column to an appropriate folder in Groups column
  2. Click Save button

Create a new attribute group

  1. In Groups column, click to Add New button
  2. In Name box, fill in a name then tap OK button
  3. Do one in two following performances:
  • Drag unassigned attributes to the new group
  • Drag attributes from any other group to the new group

Create custom attribute sets

  1. Insert any table related data after the module installation via upgradeData. Add following code in php under app/code/naespace/module/setup:
    /*
    * 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();
                   }
                   }
    } ?>
  2. Use following code to update the version in xml under app/code/namespace/etc:
    <?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>
  3. Upgrade the module setup using upgrade command via CLI

    *Note: You can upgrade the module via CLI by using command: php bin/magento setup upgrade

That’s all. If you have any question, contact us for the help.

Thanks for reading!