In this blog, we are going to learn how we can create a custom attribute set and its custom attributes group.
In Magento 2.3 and in its latest versions, for database table creation, we use the db_schema.xml file.
For more information, you can check to Configure declarative schema. But sometimes, we need to add some additional attributes or attribute set and its custom attributes group.
Then we can do it by following the below steps.
1. Create DemoAttributeSetAndItsGroup.php file inside app/code/Vendor/CustomModule/Setup/Patch/Data/ directory.
<?php /** * Webkul Software. * * @category Webkul * @package Vendor_CustomModule * @author Webkul Software Private Limited * @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\Eav\Model\Config as EavConfig; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory; use Magento\Eav\Api\AttributeGroupRepositoryInterface; class DemoAttributeSetAndItsGroup implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @var EavConfig */ private $eavConfig; /** * @var AttributeGroupInterfaceFactory */ private $attributeGroupFactory; /** * @var AttributeGroupRepositoryInterface */ private $attributeGroupRepository; /** * Initialize dependencies * * @param ModuleDataSetupInterface $moduleDataSetup * @param AttributeSetFactory $attributeSetFactory * @param EavConfig $eavConfig * @param AttributeGroupInterfaceFactory $attributeGroupFactory * @param AttributeGroupRepositoryInterface * @return void */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, AttributeSetFactory $attributeSetFactory, EavConfig $eavConfig, AttributeGroupInterfaceFactory $attributeGroupFactory, AttributeGroupRepositoryInterface $attributeGroupRepository ) { $this->moduleDataSetup = $moduleDataSetup; $this->attributeSetFactory = $attributeSetFactory; $this->eavConfig = $eavConfig; $this->attributeGroupFactory = $attributeGroupFactory; $this->attributeGroupRepository = $attributeGroupRepository; } /** * {@inheritdoc} */ public function apply() { try { $entityTypeId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); $attributeSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY) ->getDefaultAttributeSetId(); $attributeSet = $this->attributeSetFactory->create(); $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(); $attributeGroupName = 'Demo Group1'; $attributeGroup = $this->attributeGroupFactory->create(); $attributeGroup->setAttributeSetId($attributeSet->getAttributeSetId()); $attributeGroup->setAttributeGroupName($attributeGroupName); $this->attributeGroupRepository->save($attributeGroup); } 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.