In this blog, we are going through how to add custom category attributes in Magento 2.
Why Create a Custom Category Attribute in Magento 2? Because in a category you need to add some external text, numbers, multiple choice categories, etc. you can create category attribute
In this blog, we create category attributes using the latest technology “data/patch”.let’s start with how to create categories attribute.
⇒ First of all, you create “CategoryAttribute.php” in this directory “Vendor_name/Module_name/Setup/Patch/Data/”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php /** * @author CynoInfotech Team * @package vendor_module */ declare (strict_types = 1); namespace vendor\module\Setup\Patch\Data; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Catalog\Model\Category; /** * Class CategoryAttribute for Create Custom Category Attribute using Data Patch. */ class CategoryAttribute implements DataPatchInterface { /** * ModuleDataSetupInterface * * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * EavSetupFactory * * @var EavSetupFactory */ private $eavSetupFactory; /** * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create([‘setup’ => $this->moduleDataSetup]); $eavSetup->addAttribute(Category::ENTITY, ‘category_attribute’, [ ‘type’ => ‘text’, ‘label’ => ‘Custom Category Attribute’, ‘input’ => ‘text’, ‘default’ => 0, ‘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 []; } } |
⇒ After that you need to create “category_form.xml” in this directory “vendor_name/module_name/view/adminhtml/ui_component“.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version=“1.0” ?> <!— /** * @author CynoInfotech Team * @package vendor_module */ —> <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=“category_attribute”> <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”>Custom Category Attribute</item> </item> </argument> </field> </fieldset> </form> |
⇒ and last you need to run this command in the root directory
- php -dmemory_limit=2G bin/magento setup:upgrade
- php -dmemory_limit=2G bin/magento setup:static-content:deploy -f -j
- php -dmemory_limit=2G bin/magento cache:flush
⇒ after checking the custom category created successfully. If you have any questions regarding this blog let me know in the comment section.
Thank You !!!!
One response to “MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE”
Hey there! I’ve been reading your weblog for some time now and finally got the bravery to go ahead and give you a shout out from Dallas Texas! Just wanted to say keep up the great work!