How to form a new attribute for category in Magento 2

Form a new attribute for category

There are few ways to broaden the functionality of Magento categories, which will show you how to do it.

By modifying and adding some of the table directly, you can do it, but if you do not know what you’re doing, you may waste time. So, you can follow these steps and create custom type attributes within minutes. If you want to create a custom type attribute for your ecommerce site you can try the following steps

Step 1: Go to your model and create the Setup folder, create InstallData.php within it, press this complete code   app/code/[Name_space]/[Your_module]/Setup/InstallData.php


namespace [Name_space]\[Your_module]\Setup;

use Magento\Framework\Module\Setup\Migration;

use Magento\Framework\Setup\InstallDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Catalog\Setup\CategorySetupFactory;

class InstallData implements InstallDataInterface



* Category setup factory


* @var CategorySetupFactory


private $categorySetupFactory;


* Init


* @param CategorySetupFactory $categorySetupFactory


public function __construct(CategorySetupFactory $categorySetupFactory)


     $this->categorySetupFactory = $categorySetupFactory;



* {@inheritdoc}

* @SuppressWarnings(PHPMD.ExcessiveMethodLength)


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


     $installer = $setup;


     $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

     $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);

     $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);


     \Magento\Catalog\Model\Category::ENTITY, 'new_attribute');


     \Magento\Catalog\Model\Category::ENTITY, 'new_attribute', [

      'type' => 'int',

      'label' => 'New Atrribute ',

      'input' => 'select',

      'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

      'required' => false,

      'sort_order' => 100,

      'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,

      'group' => 'General Information',






Step 2: Include category_form.xml in app/code/[Name_space]/[Your_module]/view/adminhtml/ui_component and push


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

<form xmlns:xsi=""


<fieldset name="general">

     <field name="new_attribute">

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

         <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>

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

                 <item name="sortOrder" xsi:type="number">60</item>

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

                 <item name="formElement" xsi:type="string">select</item>

                 <item name="label" xsi:type="string" translate="true">New Attribute</item>






<span style="line-height:13px">

Last Update: April 7, 2018  

March 17, 2018   698   Nandini Ramachandran    Operations    
Total 0 Votes:

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *