0

I have a custom product attribute which is under Advanced Pricing group . I am trying to change attribute group of this attribute via data patch using below code :

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpdateCustomAttributeGroup implements DataPatchInterface
{

    /**
     * @var EavSetupFactory
     */
    private $eavSetup;
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup
    )
    {
        $this->eavSetup = $eavSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }

    /**
     * @inheritDoc
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function apply()
    {
        $eavSetup = $this->eavSetup->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->updateAttribute(
                'catalog_product',
                'custom_attribute_code',
                'group', 
                'General'
        );

    }
}

Above code isn't impacting already assigned group of this attribute. How to update group of any product attribute programatically? Please note, this attribute might be assigned to multiple attribute set.

0

VENDOR\MODULE\Setup\UpgradeData.php

<?php

namespace VENDOR\MODULE\Setup;

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

class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
          $setup->startSetup();
        if (version_compare($context->getVersion(), '2.0.3', '<')) {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->updateAttribute(
                \Magento\Catalog\Model\Product::ENTITY, 
                'attribute_code', 
                ['group' => 'General',]
            );
        }

    }
}

Or you can delete old attribute and create new one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.