Add category attribute Programmatically in Magento 2.

We can create custom category attribute programmatically in Magento 2 using simple module. In our demo we have created a simple text field attribute for a category.

You can see a category attribute in the backend using Catalog -> Categories Page.
Click on Any category and you can see our custom attribute in General Section after the category name.

Create custom category attribute we need to create InstallData.php file for define our custom category attribute name.

let’s start with simple custom module to create custom category attribute,
You need to create first registration.php and module.xml file for defining our module to Magento. Here I have used Rbj as Packagename where CategoryAttrbute is the module name.

Path: app/code/Rbj/CategoryAttribute/registration.php


Create a module.xml file, Path: app/code/Rbj/CategoryAttribute/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Rbj_CategoryAttribute" setup_version="1.0.0">
			<module name="Magento_Catalog"/>

module.xml is depends on Magento_Catalog module.

Create InstallData.php file to define our custom category attribute,

Path: app/code/Rbj/CategoryAttribute/Setup/InstallData.php

namespace Rbj\CategoryAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface

    private $eavSetupFactory;

     * Constructor
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
    public function __construct(EavSetupFactory $eavSetupFactory)
        $this->eavSetupFactory = $eavSetupFactory;

     * {@inheritdoc}
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

                'type' => 'varchar',
                'label' => 'Category Custom field',
                'input' => 'text',
                'sort_order' => 100,
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''

Our Custom attribute code is custom_category_field and attribute title is Category Custom field and display inside General Information group.

Now we need to define our custom attribute field to category_form.xml

Path: app/code/Rbj/CategoryAttribute/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" ?>
<form xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
	<fieldset name="general">
		<field name="custom_category_field">
			<argument name="data" xsi:type="array">
				<item name="config" xsi:type="array">
					<item name="required" xsi:type="boolean">true</item>
					<item name="validation" xsi:type="array">
						<item name="required-entry" xsi:type="boolean">true</item>
					<item name="sortOrder" xsi:type="number">100</item>
					<item name="dataType" xsi:type="string">string</item>
					<item name="formElement" xsi:type="string">input</item>
					<item name="label" translate="true" xsi:type="string">Category custom field</item>

We have set our custom attribute in general fieldset to display our attribute in General Group of a category page.

Now Run Setup Upgrade command to install our module in Magento.

Using Command line, Go to Magento instance where you have installed Magento, Open Command Line,

php bin/magento setup:upgrade
php bin/magento cache:flush
php bin/magento indexer:reindex
Category attribute
Custom Category attribute

In front, You can get the value of Category attribute by load category object and call the function like below,

Load Category data by, Get Category data by category id

$getCategory = CATEGORY_OBJECT;
echo $getCategory->getCustomCategoryField();