Create, Update and Remove Custom Category Attributes Using UpgradeData.php In Magento 2

Written by Mahesh Makwana

Jul 25, 2021

Create, Update and Remove Custom Category Attributes Using UpgradeData.php In Magento 2

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

  • Step-1: Update module.xml at app/code/Dolphin/CategoryAttribute/etc
<?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.

  • Step-2: Create UpgradeData.php at app/code/Dolphin/CategoryAttribute/Setup
<?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.

So after you create and update the above files Please run Magento commands

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.

Mahesh Makwana

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP