Create Custom Attribute Set and their Custom Attribute Groups In Magento2.3 and Magento2.4
In this blog, we are going to learn how we can create custom AttributeSet and their custom attributes group.
In Magento 2.3 and in its latest versions, for database tables creation, we use the db_schema.xml file.
For more information, you can check to Configure declarative schema.
But sometimes, if we need to add some additional attributes or attribute set and its custom attributes group.
Then we can do it by following the below step.
1. Create DemoAttributeSetAndItsGroup.php file inside app/code/Vendor/CustomModule/Setup/Patch/Data/ directory.
<?php /** * Webkul Software. * * @category Webkul * @package Vendor_CustomModule * @author Webkul * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Vendor\CustomModule\Setup\Patch\Data; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; class DemoAttributeSetAndItsGroup implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @var CategorySetupFactory */ private $categorySetupFactory; /** * @param ModuleDataSetupInterface $moduleDataSetup * @param AttributeSetFactory $attributeSetFactory * @param CategorySetupFactory $categorySetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ try { $categorySetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]); $attributeSet = $this->attributeSetFactory->create(); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $data = [ 'attribute_set_name' => 'Demo Attribute Set', 'entity_type_id' => $entityTypeId, 'sort_order' => 200, ]; $attributeSet->setData($data); $attributeSet->validate(); $attributeSet->save(); $attributeSet->initFromSkeleton($attributeSetId); $attributeSet->save(); $attributeGroupName1 = 'Demo Group1'; $newAttributeSetId = $categorySetup->getAttributeSetId($entityTypeId, 'Demo Attribute Set'); $categorySetup->addAttributeGroup( $entityTypeId, $newAttributeSetId, $attributeGroupName1, 200 // sort order ); $attributeGroupId1 = $categorySetup->getAttributeGroupId( $entityTypeId, $newAttributeSetId, $attributeGroupName1 ); } catch (\Exception $e) { return $e->getMessage(); } } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } }
2. In the following images, you can see our custom attribute set “Demo Attribute Set” and its custom attribute group “Demo Group1”.


Hope this will be helpful. Thanks 🙂
Be the first to comment.