Assigning A Group To The Attribute Set Through Patch File (Programmatically)
Method To Assigning A Group To The Product Attribute.
1- For Assigning A Group To The Product Attribute you need to create a custom module.
Like:
Vendor/Module
Esparksinc/AttrGroup
2- Create DemoAttributeSetAndItsGroup.php file inside.
app/code/Esparksinc/AttrGroup/Setup/Patch/Data
And paste this code into it:
<?php /** * Esparks. * */ namespace Esparksinc\AttrGroup\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; use Magento\Eav\Setup\EavSetup; 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(); $firstAttributeGroupName = 'Demo Group1'; $newAttributeSetId = $categorySetup->getAttributeSetId($entityTypeId, 'Demo Attribute Set'); $categorySetup->addAttributeGroup( $entityTypeId, $newAttributeSetId, $firstAttributeGroupName, 200 // sort order ); $firstAttributeGroupId = $categorySetup->getAttributeGroupId( $entityTypeId, $newAttributeSetId, $firstAttributeGroupName ); // Assign the attribute set to the attribute group $attributeCode = 'eco_collection'; // Replace with your actual attribute code $categorySetup->addAttributeToGroup( $entityTypeId, $newAttributeSetId, $firstAttributeGroupId, $attributeCode ); } catch (\Exception $e) { return $e->getMessage(); } } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } }
The provided code is a Magento 2 data patch that creates a new attribute set and its associated attribute group. Here’s a concise definition:
The code defines a class named `DemoAttributeSetAndItsGroup` that implements the `DataPatchInterface` in the `Esparksinc\AttrGroup\Setup\Patch\Data` namespace. This class is responsible for creating a new attribute set and adding an attribute group to it.
The class constructor accepts three dependencies: `ModuleDataSetupInterface`, `AttributeSetFactory`, and `CategorySetupFactory`, which are used for database setup and attribute set/group creation.
The `apply` method is executed when the patch is applied. It uses the `CategorySetupFactory` to retrieve the entity type ID for products and the default attribute set ID. It then creates a new attribute set with the name “Demo Attribute Set” and a sort order of 200. The attribute set is saved and initialized from the default attribute set. Next, a new attribute group named “Demo Group1” is added to the attribute set with a sort order of 200. Finally, the attribute with the code “eco_collection” is assigned to the newly created attribute group.
The `getDependencies` method returns an empty array, indicating that this patch doesn’t have any dependencies on other patches.
The `getAliases` method also returns an empty array, indicating that no aliases are defined for this patch.
Overall, this code creates a new attribute set, adds an attribute group to it, and assigns a specific attribute to the attribute group using Magento 2 setup and category setup functionalities.
Run these commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy-f
php bin/magento cache:clean
chmod -R 777 var/pub/generated
See the result magento admin panel on stores => Attribute => Attributeset:
Open Demo Attribute Set to see the group Demo Group 1 which is assigned to Demo Attribute Set :
Note:
This patch file works once if you want to change something you need to delete the patch from the database then run this patch again.