How to Add Category Attribute to Custom Attribute Group in Magento 2
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:
- Create your category attribute using InstallData.php
123456789101112131415161718192021222324252627282930313233343536373839404142<?phpnamespace [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' => '']);}} - Create category_form.xml file at [Vendor]\[Module]\view\adminhtml\ui_component\category_form.xml
123456789101112131415161718192021222324<?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.
Do let me know in the Comments section below if it helped and how! Also, any doubts regarding the implementation of the code can be asked below and I’ll be glad to help you out.
Also, do share the solution with the Magento community via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Hello Sanjay Jethva,
Thank you for the Article. It is very useful for people who are beginners in the industry.
Hello
We’re glad to know that!
Thank You
Hi, I tried above tutorial but Value is not saved in Admin upon clicking ” Save “. Any suggestions ?
Hello Muhammad,
You may have made a mistake in your code.
Please recheck the code as it’s working fine from our end.
Thank You.
how to display the field values in frontend
Hello,
First, load $category
You will get the value by:
$category->getCustomCategoryAttribute()
Thank you.
Why save the category_form.xml in the Vendor directory?
Hii,
It’s not Vendor directory, you will replace [Vendor]\[Module] with your existing extension
if not then create custom extension and replace [Vendor]\[Module] with it.
Thanks.