Blog

How to create Magento 2 Category Attributes Programmatically
Magento2-category-attributes

In this blog, you will learn how to create custom Magento 2 Category Attributes programmatically by using a simple module. We have created a simple text field attribute for a Magento 2 category.

Here are the snippets that you can achieve immediately. You need to follow the below code for your module.

How to add Magento 2 Category Attributes programmatically?

By default, Magento does not provide the facility to add category attributes from admin. But we can create the custom category attributes programmatically. By following these steps that I mention below, you can create the category attributes.

Let us start with the simple module to create a custom category attribute in Magento 2. Now create registration.php and module.xml files to define the module. Therefore, I have used Webepower as Packagename where the CustomAttribute is the module name.

Path: app/code/Webepower/CustomAttribute/registration.php

<?php

Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Webepower_CustomAttribute',
    __DIR__
);

Create a file module.xml

Module.xml is actually dependent on the Magento_Catelog module.

Path: app/code/Webepower/CustomAttribute/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

    <module name="Webepower_CustomAttribute" setup_version="1.0.0" />

</config>

Create a file InstallData.php

Now you have to create InstallData.php to define the custom category attribute.

Path: app/code/Webepower/CustomAttribute/Setup/InstallData.php

<?php

namespace Webepower\CustomAttribute\Setup;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;

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

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    )
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'category_custom_attribute',
            [
                'type' => 'text',
                'label' => 'Category Custom Attribute',
                'input' => 'text',
                'sort_order' => 100,
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'system' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => '',
                'is_visible' => true,
            ]
            );
    }
}

Define the custom attribute field in category_form.xml

Path: app/code/Webepower/CustomAttribute/view/adminhtml/ ui_component/category_form.xml

<?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="category_custom_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">333</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 Attributee</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Read More:- Magento 2 Migration Cost: Move your e-commerce store to Magento 2

Now, we have set our custom category attribute in fieldset to show the attribute in the General group of a category page. Run the following Setup Upgrade command to install our module in Magento 2.

Go to Magento instance where you have installed Magento, then Open Command Line:

php bin/magento setup:upgrade
php bin/magento cache:flush
php bin/magento indexer:reindex

Now, you can see the custom category attribute in the backend by navigating the location catalog -> categories pages. Click on any category to see the custom category attribute in the general section.

Happy Reading!