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 😂

92 Comments

  1. **breathe**

    breathe is a plant-powered tincture crafted to promote lung performance and enhance your breathing quality.

  2. Với mục tiêu mang đến sân chơi an toàn, 188v con áp dụng công nghệ mã hóa SSL tiên tiến, đảm bảo thông tin cá nhân và giao dịch luôn được bảo vệ tuyệt đối. Đây là yếu tố then chốt giúp nền tảng này giữ được uy tín trên thị trường cá cược. TONY12-30

  3. Right, gonna be honest…the name ‘xxaaslot’ caught my eye. Slots look pretty standard though. Maybe some hidden gems? Worth a browse: xxaaslot

  4. W88is… hmmm, sounds familiar. I think I saw some ads for them. Is it just me, or does anyone else find their website kinda confusing? Anyway, take a peek yourself! w88is.

  5. **balmorex**

    balmorex is an exceptional solution for individuals who suffer from chronic joint pain and muscle aches.

  6. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  7. **finessa**

    Finessa is a natural supplement made to support healthy digestion, improve metabolism, and help you achieve a flatter belly.

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

  9. 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.

  10. 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?

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

  12. 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.

  13. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  14. Backbiome is an advanced daily wellness supplement formulated to help support spinal comfort, reduce feelings of built-up tension, and promote freer, smoother movement throughout backbiome everyday life.

  15. Boostaro is a modern men’s wellness boostaro formula created to support daily vitality, stamina, and confidence through a practical, natural routine.

  16. 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?

  17. [9425]bet368 | Link Vào Chính Thức – Tải App APK và Cổng Game Uy Tín,bet368 cung cấp hướng dẫn chi tiết luật chơi baccarat và cách nạp tiền casino nhanh chóng. Trải nghiệm cổng game slots uy tín ngay hôm nay để nhận ưu đãi hấp dẫn. visit: bet368

  18. It really felt like my lucky day when I joined this site. The graphics are great and the gameplay is very addictive. Five stars from me! luckyday33

  19. 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?

  20. 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?

  21. Love the selection of live dealer games. It really feels like I am at a real casino without leaving my living room. Cheers to rk88bet for the great setup.

  22. I love the layout and the overall vibe of this site. It feels professional and trustworthy. I am spending a lot of my free time on idasia128 lately.

  23. Fast deposits and even faster withdrawals. That is all I really care about and this site delivers every time. Check out kr10bet now.

  24. Finally found a version that actually works on my phone without crashing. The interface is very user friendly. Just download the phjuan365apk and start winning.

  25. Really impressed with the payout speed here. I had a great run last night and the withdrawal was processed almost instantly. Keep it up brd333bet!

  26. 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?

  27. I’ve been looking for a smooth app experience and this one delivers. The loading speed is impressive and the interface is very intuitive. Highly recommend 5777betloginapp for anyone who hates lagging.

  28. Just signed up yesterday and the welcome bonus is actually generous. The graphics are crisp and the gameplay is very intuitive. Love 777games55 so far!

  29. The betting odds on this site are much better than others I have used. The app is very responsive and easy to navigate. Give bdbolbybol a try!

  30. Love the vibe of this platform. It feels very fair and the payouts are consistent. I’m really enjoying my time at ktwin33 so far.

  31. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  32. Finally found a site that doesn’t lag during peak hours. The interface is super clean and getting in is a breeze. Highly recommend ph7777login for a hassle-free session.

  33. I’ve tried a few sites lately and this one is definitely the smoothest. The login process is a breeze and the games load instantly. Highly recommend checking out 7e777gamelogin for a great experience.

  34. The best platform I have encountered in the region. Very user friendly and the promotions are actually useful for players. Big thanks to ph39jiliph.

  35. I’ve been looking for a reliable spot like this for a while. The interface is super smooth and the payouts are fast. Definitely worth checking out idsenopati2

  36. The registration process was a breeze and I was playing in under two minutes. Everything is very professional and secure. Glad I found the ap999login portal.

  37. The customer support is actually helpful which is rare these days. I had a small issue with my deposit and they sorted it in minutes. Give au8802com a go.

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

Write A Comment