Question

I have 20 product attributes. I need to change those attributes' saving scope to "Global".

How can I achieve this?

Was it helpful?

Solution

The quick and easiest way to update the scope is with MySQL query (given below).

UPDATE `catalog_eav_attribute` SET `is_global`= 1 WHERE `attribute_id` IN (45,46,47);

Where 45, 46 and 47 is the attribute ids.

You can run below script to update the attributes scope programmaticaly.

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Sales\Model\Order;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('catalog_eav_attribute');

// UPDATE DATA
$value = 1;
$attributeIds = 45,46,47;
$sql = "UPDATE " . $tableName . " SET is_global = ". $value ." WHERE attribute_id IN (". $attributeIds .");
$connection->query($sql);

Hope it helps!!!

OTHER TIPS

You can update the product attribute using the UpgradeData script.

UpgradeData.php

<?php

namespace VendorName\ModuleName\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeData implements UpgradeDataInterface
{
    public function __construct(
        EavSetupFactory $eavSetupFactory
    )
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /* Compate you module version */
        if (version_compare($context->getVersion(), '0.1.1', '<')) {
            $updateAttributes = array('attribute_code_1','attribute_code_2','attribute_code_3','attribute_code_4','attribute_code_5','attribute_code_6','attribute_code_7');

            foreach ($updateAttributes as $attribute) {
                $eavSetup->updateAttribute(\Magento\Catalog\Model\Product::ENTITY, $attribute, 'global', \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL);
            }
        }
    }
}

Hope this helps!

You can update it using below SQL query :

UPDATE `catalog_eav_attribute` SET `is_global`= 1 WHERE `attribute_id`=45;

For multiple :

UPDATE `catalog_eav_attribute` SET `is_global`= 1 WHERE `attribute_id` IN (45,46,47);

You can set your attribute_id which you want to update.

After execute this commad :

php bin/magento ind:rei
php bin/magento c:c

You can save scope of custom category attribute like this using upgrade schema:

<?php

namespace Vendor\Custom\Setup;

use Magento\Catalog\Model\Category;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Eav\Setup\EavSetup;

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * @var EavSetup
     */
    private $eavSetupFactory;

    /**
     * UpgradeSchema constructor.
     * @param EavSetup $eavSetupFactory
     */
    public function __construct(
        EavSetup $eavSetupFactory
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '2.0.5', '<')) {
            $setup->startSetup();
            $this->eavSetupFactory->updateAttribute(
                Category::ENTITY,
                'custom_attribute',
                'is_global',
                ScopedAttributeInterface::SCOPE_STORE,
                null
            );
            $setup->endSetup();
        }
    }
}

Change category_form.xml to include store view to display that in admin interface

 <settings>
    <validation>
         <rule name="required-entry" xsi:type="boolean">false</rule>
    </validation>
    <dataType>boolean</dataType>
    <label translate="true">Exclude from Sitemap</label>
    <scopeLabel>[STORE VIEW]</scopeLabel>
</settings>

You can save custom product attributes like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface;')->load($product_id);

$attributeCode="available_city";

$product->setAvailableCity("value"); 

$product->getResource()->saveAttribute($product, $attributeCode);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top