Reading list Switch to dark mode

    Update Product Custom Attribute Scope in Magento 2

    Updated 22 February 2024

    Here we learn in Magento2 – How to update the product custom attribute’s scope in the custom module.
    If you want to create a new custom attribute in Magento 2, you can check the wonderful blog here

    Update Product Custom Attribute Scope in Magento 2

    If there is an attribute with store view scope.

    1st Method: Using the UpgradeData file

    First, we need to create a file named UpgradeData.php in our custom module Setup folder.
    complete path: app/code/ModuleNameSpace/YourModuleName/Setup/UpgradeData.php

    <?php
    
    namespace ModuleNameSpace\YourModuleName\Setup;
    
    use Magento\Framework\Setup;
    use Magento\Eav\Setup\EavSetupFactory;
    
    class UpgradeData implements Setup\UpgradeDataInterface 
    {
        /**
         * EAV setup factory
         *
         * @var EavSetupFactory
         */
        private $_eavSetupFactory;
        /**
         * @param EavSetupFactory  $eavSetupFactory
         */
        public function __construct(
            EavSetupFactory $eavSetupFactory
        ) {
            $this->_eavSetupFactory = $eavSetupFactory;
        }
    
        public function upgrade(
            Setup\ModuleDataSetupInterface $setup,
            Setup\ModuleContextInterface $moduleContext
        ) {
            $setup->startSetup();
            $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->updateAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'your_attribute_code',
                'is_global', \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL
            );
            $setup->endSetup();
        }
    }

    Now, you have to update the version of the module from the module.xml file.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    2nd Method: Using Data Patch

    In this method, we need to create a data patch file as UpdateAttributes.php in our custom module Setup folder. Note: You can also use the different file names and class names.
    complete path: app/code/ModuleNameSpace/YourModuleName/Setup/Patch/Data/UpdateAttributes.php

    To learn more about data and schema patches, click here.

    <?php
    
    namespace ModuleNameSpace\YourModuleName\Setup\Patch\Data;
    
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Framework\Setup\Patch\DataPatchInterface;
    
    class UpdateAttributes implements DataPatchInterface
    {
        /**
         * @var ModuleDataSetupInterface
         */
        private $moduleDataSetup;
        /**
         * @var EavSetupFactory
         */
        private $eavSetupFactory;
    
        /**
         * @param ModuleDataSetupInterface $moduleDataSetup
         * @param EavSetupFactory $eavSetupFactory
         */
        public function __construct(
            ModuleDataSetupInterface $moduleDataSetup,
            EavSetupFactory $eavSetupFactory
        ) {
            $this->moduleDataSetup = $moduleDataSetup;
            $this->eavSetupFactory = $eavSetupFactory;
        }
    
        /**
         * Add eav attributes
         */
        public function apply()
        {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
    
            $eavSetup->updateAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'your_attribute_code',
                'is_global',
    \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL
            );
        }
    
        /**
         * Get dependencies
         */
        public static function getDependencies()
        {
            return [];
        }
    
        /**
         * Get Aliases
         */
        public function getAliases()
        {
            return [];
        }
    }

    After adding these files, update your custom product attribute’s scope in your custom module in your Magento 2 instance by running the following command in your magento2 root directory through the terminal.

    php bin/magento setup:upgrade
    Update Product Custom Attribute Scope in Magento 2

    now, the scope of the attribute changes to the Global

    is_global’ flag is used instead of ‘global’ in updating the scope of an attribute.

    Hope it will help you. Thank you.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Shaun Lewandowski
    • Rajat Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home