Adding Customized category attribute in Magento

Adding Customized Category Attribute in Magento 2


To learn how to add a customized category attribute in Magento 2, we will follow the steps below:

1- Create Module file module.xml 

We will start with the module.xml file which is located in 


Copy below content and paste in module.xml file 


<?xml version="1.0"?> 

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> 

    <module name="Esparks_CategoryAttibute" setup_version="1.0.0"> 


            <module name="Magento_Backend"/> 

             <module name="Magento_Sales"/> 

            <module name="Magento_Quote"/> 

            <module name="Magento_Checkout"/> 

            <module name="Magento_Cms"/> 




2- Create Module registration file registration.php in directory /app/code/Esparks/CategoryAttibute 

Copy below content and paste in registration.php file 






3- Create install script file with name InstallData.php 

Place this file in directory /app/code/Esparks/CategoryAttibute/Setup/ 

Copy below content and paste in InstallData.php file 


namespace Esparks\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. All configurations relate to this field. 

Copy below content in category_form.xml located in directory /app/code/Esparks/CategoryAttibute/view/adminhtml/ui_component/ 


<?xml version="1.0" encoding="UTF-8"?> 

<form xmlns:xsi="" 


    <fieldset name="general"> 


        <field name="custom_attr"> 

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

                <item name="configxsi:type="array"> 

                    <item name="sortOrderxsi:type="number">110</item> 

                    <item name="dataTypexsi:type="string">string</item> 

                    <item name="formElementxsi: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="additionalClassesxsi:type="string"></item> 









That's it.
5- After that run upgrade command 

php bin/magento setup:upgrade 
6- Run Flush Cache command  

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.


Creating custom category attribute in magento 2