For some unique customization, we need to create category attributes to fulfil client requirements. Magento introduce data patch to create attributes after version Magento 2.3. So, We need to create category attribute using data patch in magento2.

This Blog for Magento version >= 2.4.4 && PHP >= 8.1

Here I created simple category attribute with datatype text

Create file: app/code/MageDad/Module/Setup/Patch/Data/AddCodeCategoryAttribute.php

PHP
<?php

declare(strict_types=1);

namespace MageDad\Module\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Catalog\Model\Category;

class AddCodeCategoryAttribute implements DataPatchInterface
{
    public function __construct(
        private ModuleDataSetupInterface $moduleDataSetup,
        private EavSetupFactory $eavSetupFactory,
    ) {
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'code', [
            'type' => 'text',
            'label' => 'Category Code',
            'note' => 'Akeneo code entity',
            'input' => 'text',
            'default' => '',
            'sort_order' => 5,
            'required' => false,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'visible_on_front' => true
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

We need to run magento setup upgrade command to run this data patch.
php bin/magento setup:upgrade

Create file – app/code/MageDad/Module/view/adminhtml/ui_component/category_form.xml

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="code">
            <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">100</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 Code</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>


We can see catrgory attribute in admin like below screenshot.

create category attribute using data patch in magento2

We try to create quality blog with tested code. You can see github commit with same code.

I hope this blog is useful to create category attribute using data patch in magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😂

27 Comments

  1. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  2. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  3. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  4. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  5. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  6. Explore the ranked best online casinos of 2025. Compare bonuses, game selections, and trustworthiness of top platforms for secure and rewarding gameplayBonus offer.

  7. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  8. Thank you, your article surprised me, there is such an excellent point of view. Thank you for sharing, I learned a lot.

  9. When it comes to roof installation in Lancaster, Roof Installation Pros stands out for quality and reliability. Their expert team works with all roof types, delivering strong, protective installations that add value. Highly rated for affordable, on-time, and professional roofing work.

  10. Searching for trusted roof installation in Lancaster? Roof Installation Pros provides expert services across all roofing styles. Their experienced team guarantees strong, lasting installations that enhance your home’s protection and market value. Excellent service, always on time and within budget.

  11. Searching for trusted roof installation in Lancaster? Roof Installation Pros provides expert services across all roofing styles. Their experienced team guarantees strong, lasting installations that enhance your home’s protection and market value. Excellent service, always on time and within budget.

  12. Searching for trusted roof installation in Lancaster? Roof Installation Pros provides expert services across all roofing styles. Their experienced team guarantees strong, lasting installations that enhance your home’s protection and market value. Excellent service, always on time and within budget.

  13. Get reliable roof installation services in Lancaster with Roof Installation Pros. Whether it’s a new roof or a replacement, their experienced crew delivers quality craftsmanship that lasts. Known for fair pricing and dependable results that boost home value—your roof is in safe hands.

  14. Get reliable roof installation services in Lancaster with Roof Installation Pros. Whether it’s a new roof or a replacement, their experienced crew delivers quality craftsmanship that lasts. Known for fair pricing and dependable results that boost home value—your roof is in safe hands.

  15. Searching for trusted roof installation in Lancaster? Roof Installation Pros provides expert services across all roofing styles. Their experienced team guarantees strong, lasting installations that enhance your home’s protection and market value. Excellent service, always on time and within budget.

  16. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

Write A Comment