We’ll show you how to add category attributes in magento2 and display in category form. We show here 3 easy step for add category attribute and display attribute to category form.

Step 1: Create patch file for create category attribute. Create file below.
app/code/MageDad/Module/Setup/Patch/Data/AddAttributeCategoryAttribute.php

PHP
<?php

declare(strict_types=1);

namespace MageDad\Module\Setup\Patch\Data;

use Magento\Catalog\Model\Product as ProductModel;
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;
use Psr\Log\LoggerInterface;
use Throwable;

/**
 * Add attribute to category
 */
class AddAttributeCategoryAttribute implements DataPatchInterface
{
    public function __construct(
        private ModuleDataSetupInterface $moduleDataSetup,
        private EavSetupFactory $eavSetupFactory,
        private LoggerInterface $logger,
    ) {
    }

    /**
     * Attribute used for assigning Amplience content to category
     *
     * {@inheritdoc}
     */
    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(Category::ENTITY, 'attribute_code', [
            'type' => 'text',
            'label' => 'Attribute Name',
            'note' => 'Attribute notes',
            'input' => 'text',
            'default' => '',
            'sort_order' => 5,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'visible_on_front' => true
        ]);
    }

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

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


Step 2: In this step we are going to add attribute in category form.

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="attribute_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">Attribute Name </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Step 2: Run setup upgrade command
php bin/magento setup:upgrade

I hope this blog is useful to add categroy attribute 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. Thank you ❤️

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

56 Comments

  1. Just had a session on ph4444 and won some really good prizes tonight! It’s pretty good & I am happy with my experience, I will try again for sure! Give it a shot!: ph4444

  2. Truy cập bắn cá 888SLOT , bạn có thể nhận ngay tiền thưởng từ giải Jackpot khi hạ gục boss thành công. YL, JDB, BBIN,… cung cấp hơn 1.000+ sinh vật biển bí ẩn đi kèm với hơn 55+ loài cá đặc biệt sở hữu hệ số nhân cực cao, cho phép bạn hốt gấp 750X tiền thưởng về túi. TONY12-30

  3. Alright folks, let me tell ya, 365betcassino ain’t messing around. They’ve got the games, they’ve got the vibes, and honestly, I’ve had some pretty sweet wins here. Give it a whirl, you might just surprise yourself! Check ’em out over at 365betcassino.

  4. **mitolyn reviews**

    Mitolyn is a carefully developed, plant-based formula created to help support metabolic efficiency and encourage healthy, lasting weight management.

  5. **mitolyn**

    Mitolyn is a carefully developed, plant-based formula created to help support metabolic efficiency and encourage healthy, lasting weight management.

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

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

  8. Signed up with Fun88fun120 last week! The registration was seamless and user interface is pretty easy to understand. Definitely recommend giving it a whirl folks. fun88fun120

  9. Trying my hand at bet522bet today. The odds seem pretty good, and the site loads fast. Hopefully, I’ll walk away a winner. Check it out if you want bet522bet.

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

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

  12. Finally a betting site that understands what local players need. The odds are competitive and the live match streaming works flawlessly. I really enjoy the weekly cashback offers because they give me more chances to play. Support team is always polite and ready to help. inz7

  13. The exchange feature here runs so smoothly and I can move funds around without any hassle. I really appreciate how transparent the rules are and how fair every game outcome feels. The team keeps improving the platform based on feedback and that level of care makes a huge difference. kheloexchange

  14. Looking for some exciting slot spins that actually pay out. This platform delivers exactly that. I love how the game library keeps updating with fresh titles and the interface runs perfectly on my phone. Withdrawals reached my e-wallet overnight without any drama. A solid choice for slot enthusiasts. jokerslotz777

  15. If you like Jili games, this is the absolute best place to play them. The win rates feel much fairer here. Check out jilijdb for the best action.

Write A Comment