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
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 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 commandphp 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 😄
11 Comments
https://t.me/Top_BestCasino/162
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.com/sl/register?ref=I3OM7SCZ
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
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. https://www.binance.info/register?ref=IHJUI7TF
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
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/pt-BR/register-person?ref=GJY4VW8W
If you’re looking for a place with a ton of options then vn123game is it. I play almost every day and have yet to get bored. Highly recommended!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
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.