We learn how to create, update and remove custom category attributes using UpgradeData.php in Magento 2. When we develop modules or themes we need some modification in custom category attributes then we use UpgradeData.php. Magento 2 allow to create, update and remove custom category attribute.
We used the UpgradeData.php file for creating, updating and removing custom category attributes. UpgradeData.php file places at your module path app/code/VendoreName/ModuleName/Setup/UpgradeData.php. This file is executed only when the module setup_version version is updated. Like InstallData.php file UpgradeData.php file also executes only once.
If you don’t know about UpgradeData.php then click here
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Dolphin_CategoryAttribute" setup_version="1.0.2"> <sequence> <module name="Magento_Catalog"/> <module name="Magento_Backend"/> </sequence> </module> </config>
Here we update setup_version only. You can also update the sequence if required.
<?php namespace Dolphin\CategoryAttribute\Setup; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; class UpgradeData implements UpgradeDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if ($context->getVersion() && version_compare($context->getVersion(), '1.0.2') < 0) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /* For Remove Attribute */ $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE'); /* For Create New Attribute */ $eavSetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE', [ 'type' => 'varchar', 'label' => 'Updated Attribute Name', 'input' => 'text', 'required' => false, 'sort_order' => 35, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'wysiwyg_enabled' => true, 'group' => 'General Information', ] ); /* For Update Attribute */ $eavSetup->updateAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE', 'position', 100); } $setup->endSetup(); } }
Here we must add the same version in both files. If the version name does not match then the UpgradeData.php file not executes. please check before updating the module.
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean
Please check your app/code/Dolphin/CategoryAttribute/view/adminhtml/ui_component/category_form.xml file after updating the module. This file is responsible for show attributes in category form.
I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.
Click one of our contacts below to chat on WhatsApp