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 😂

Write A Comment