Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Update Product Custom Attribute Scope in Magento 2

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

    Update Product Custom Attribute Scope in Magento 2

    If there is an attribute with store view scope.

    1=>First we need to create an installer file name UpgradeData.php in our custom module Setup folder
    complete path: app/code/ModuleNameSpace/YourModuleName/Setup

    <?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();
        }
    }
    

    2=> Now you have to update the version of the module from the module.xml

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

    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.

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    2 comments

  • Shaun Lewandowski
    this helped a lot! thanks!
    • Rajat Singh (Moderator)
      You are most welcome.
  • Back to Top