Request Quote

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 Magneto 2, you can check the wonderful blog from here

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

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.

 

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php