Attributes in Magento 2 are of great help! However, not all the time the default attributes are enough when it comes to satisfying the business requirements.
Also, you may have created a custom group in Magento 2 admin panel where you want to create a category attribute. For that, you need to implement the below method.
When you are developing a module and need to customize the native feature or add a new feature altogether, adding a custom group is a must. For example, integrating a third-party API and using a category attribute!
And, you may find this method to add category attribute to custom attribute group in Magento 2 helpful for the same.
Method to Add Category Attribute to Custom Attribute Group in Magento 2:
1. Create your category attribute using InstallData.php
<?php namespace [Vendor]\[Module]\Setup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; use Magento\Catalog\Model\Category; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( Category::ENTITY, 'custom_category_attribute', [ 'group' => 'Custom Category Group', 'type' => 'text', 'label' => 'Category Attribute', 'input' => 'text', 'required' => false, 'sort_order' => 100, 'global' => ScopedAttributeInterface::SCOPE_STORE, 'visible' => true, 'user_defined' => true, 'backend' => '' ] ); } }
2. Create category_form.xml file at
[Vendor]\[Module]\view\adminhtml\ui_component\category_form.xml
<?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="custom_category_group"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="label" xsi:type="string">Custom Category Group</item> <!-- your category group name --> <item name="collapsible" xsi:type="boolean">true</item> <item name="sortOrder" xsi:type="number">99</item> </item> </argument> <field name="custom_category_attribute"> <!-- your attribute code --> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="sortOrder" xsi:type="number">100</item> <item name="dataType" xsi:type="string">string</item> <item name="label" xsi:type="string" translate="true">Category Attribute Name</item> <item name="formElement" xsi:type="string">input</item> <item name="notice" xsi:type="string">add comment</item> </item> </argument> </field> </fieldset> </form>
That’s it.
You can also create custom order attribute in Magento 2 which will be seen in admin grid which will help your business to manage and fulfill online orders efficiently.
Also, do share the solution with the Magento community via social media.
Thanks.