How to add a customized category attribute in Magento 2

To add a customized category attribute in Magento 2, Follow the steps given below:

1. Create a new module:

  • In this blog, the module name is CreateProductAttribute which is located at the following path: app/code/Esparksinc/CategoryAttibute

2. Edit the "module.xml" file:

  • Paste the following code inside the <module> element in "module.xml" file located at the following path: "app/code/Esparksinc/CategoryAttibute/etc/module.xml"

 <sequence> 

            <module name=”Magento_Backend”/> 

             <module name=”Magento_Sales”/> 

            <module name=”Magento_Quote”/> 

            <module name=”Magento_Checkout”/> 

            <module name=”Magento_Cms”/> 

 </sequence> 

3. Create install script file. with the name "InstallData".php :

  • Create a new file "InstallData.php" file at the following path:             "app/code/Esparksinc/CategoryAttibute/Setup/InstallData.php" 
  • Paste the following code in it:

<?php 

namespace Esparksinc\CategoryAttibute\Setup; 

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; 

/** 

 * @codeCoverageIgnore 

 */ 

class InstallData implements InstallDataInterface 

    /** 

     * EAV setup factory 

     * 

     * @var EavSetupFactory 

     */ 

    private $eavSetupFactory; 

    /** 

     * Init 

     * 

     * @param EavSetupFactory $eavSetupFactory 

     */ 

    public function __construct(EavSetupFactory $eavSetupFactory) 

    { 

        $this->eavSetupFactory = $eavSetupFactory; 

    } 

    /** 

     * {@inheritdoc} 

     * @SuppressWarnings(PHPMD.ExcessiveMethodLength) 

     */ 

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 

    { 

        /** @var EavSetup $eavSetup */ 

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

        if (version_compare($context->getVersion(), ‘1.0.0’) < 0){ 

           $eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, ‘custom_attr’); 

           $eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, ‘custom_attr’, [ 

                    ‘type’ => ‘varchar’, 

                    ‘label’ => ‘Custom Attribute’, 

                    ‘input’ => ‘text’, 

                    ‘required’ => false, 

                    ‘sort_order’ => 110, 

                    ‘global’ => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 

                    ‘group’ => ‘General Information’, 

                    “default” => “”, 

                    “class”    => “”, 

                    “note”       => “” 

                    ] 

                ); 

           } 

      } 

4. Display the category attribute in admin page:

  • To render the UI component of category, we create category_form.xml file. To render attribute field,  we will add a field in field set named general. In general field set we will add field with argument. Paste the following code inside the <fieldset name="general"> in the "category_form.xml" file located at the following path. "app/code/Esparksinc/CategoryAttibute/view/adminhtml/ui_component/category_form.xml". 

<field name=”custom_attr”> 

            <argument name=”data” xsi:type=”array”> 

                <item name=”config” xsi:type=”array”> 

                    <item name=”sortOrder” xsi:type=”number”>110</item> 

                    <item name=”dataType” xsi:type=”string”>string</item> 

                    <item name=”formElement” xsi:type=”string”>input</item> 

                    <item name=”label” xsi:type=”string” translate=”true”>Custom Attribute</item> 

                    <item name=”notice” xsi:type=”string” translate=”true”></item> 

                    <item name=”additionalClasses” xsi:type=”string”></item> 

                </item> 

            </argument> 

</field> 

1. Execute magento commands:

  • php bin/magento setup:upgrade
  • php bin/magento cache:flush
After that login to admin panel and go to CATALOG->Categories 
Click on the category on left side and in category you will be able to see the result. 
You can add value of added attribute.


That’s the detailed process of adding customized category attribute in Magento 2. Esparks is a leading Magento development agency with major expertise in Magento.