How to Add Custom Attribute to a Category in Magento 2.

Magento 2 is a powerful e-commerce platform that offers a wide range of features and functionalities to its users. One of the features of Magento 2 is the ability to add custom attributes to various entities, including categories. In this blog post, we will discuss how to add a custom attribute for a category in Magento 2.

Step 1: Create a module

The first step is to create a module. You can create a module using the following command in your terminal:

php bin/magento module:create --name=Namespace_Modulename --area=adminhtml

Step 2: Create an InstallData.php file

Next, you need to create an InstallData.php file in the Setup directory of your module. In this file, you will define the installation script for your custom attribute. Here's an example:

<?php
namespace Namespace\Modulename\Setup;

use Magento\Catalog\Model\Category;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            Category::ENTITY,
            'custom_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Custom Attribute',
                'input'        => 'text',
                'sort_order'   => 100,
                'global'       => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible'      => true,
                'required'     => false,
                'user_defined' => true,
                'group'        => 'General Information',
                'backend'      => '',
                'frontend'     => '',
                'note'         => ''
            ]
        );

        $setup->endSetup();
    }
}

In the above code, we are adding a new attribute with the code "custom_attribute" to the category entity. We have specified the type of attribute, label, input type, sort order, visibility, required field, and other properties.

Step 3: Run the setup script

Now, you need to run the setup script to add the custom attribute to the category entity. Run the following command in your terminal:

php bin/magento setup:upgrade

Step 4: Add the attribute to the category form

Finally, you need to add the custom attribute to the category form so that it can be edited from the admin panel. To do this, create a category_form.xml file in the view/adminhtml/ui_component directory of your module. Here's an example:

<?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="general">
        <field name="custom_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Custom Attribute</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="sortOrder" xsi:type="number">100</item>
                    <item name="visible" xsi:type="boolean">true</item>
                <item name="required" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
   </fieldset>
</form>

In the above code, we are adding the custom attribute to the "general" fieldset of the category form. We have specified the label, data type, form element, sort order, visibility, and required field for the attribute.

Step 5: Clear the cache

Once you have added the attribute to the category form, clear the cache using the following command:

php bin/magento cache:clean

That's it! You have successfully added a custom attribute for the category entity in Magento 2. You can now view and edit the attribute from the category edit page in the admin panel.