Magento 2 Create Category Attribute Programmatically

In this article I am going to explain how to create category attribute programmatically in Magento 2.

First of all you need to create one module. If you already have a module then go with that but don't forget to change the setup version and if you are new in Magento then check our another article How To Create a Magento 2 Module.

Let's get started!!

Now I assume you already have magento module.

To create the category attribute programmatically in module we need to create InstallData class in our Magento 2 Extension

Let's Create InstallData.php file at app/code/Devhooks/HelloWorld/Setup/InstallData.php and paste the below code.

Copy
<?php
namespace Devhooks\HelloWorld\Setup;

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)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'custom_category_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Custom Category Atrribute',
                'input'        => 'text',
                'sort_order'   => 120,
                'source'       => '',
                'global'       => 1,
                'visible'      => true,
                'required'     => false,
                'user_defined' => false,
                'default'      => null,
                'group'        => '',
                'backend'      => ''
            ]
        );
    }
}

In above code we have created a text type category attribute. Replace, custom_category_attribute with your attribute code and change the lable and other attribute setting as per your need.

Now, we need to display custom category attribute in category add/edit page in admin. For, that we need to create category_form.xml file in our module. Magento UI component will mearge all the category_form.xml file togather and render the configurations from this file.

Let's create category_form.xml file at app/code/Devhooks/HelloWorld/view/adminhtml/ui_component/category_form.xml and paste the below code.

Copy
<?xml version="1.0" ?>
<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_category_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">120</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">Custom Category Atrribute</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Replace your code with custom_category_attribute, label with Custom Category Atrribute and change other settings as per your need.

Now, open Command line in folder root of magento and run both commands for create new category attribute programmatically.

Copy
php bin/magento setup:upgrade

php bin/magento cache:flush

php bin/magento indexer:reindex

Bingo!! we are done, now go to the Admin > Catalog > Categories > Click on any category and find your new category attribute.

That's it!!

Also, refer Magento 2 Add Product Attribute with Options Programmatically article, if you want to add select type product attribute programmatically.

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.