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 😂

12 Comments

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

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

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

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

Write A Comment