0

I need to change existing category attribute code from "thumbnail" to "thumbnail_migration" and just after that create a new category attribute with name "thumbnail".

In upgrade data I have following code:

if (version_compare($context->getVersion(), '2.9.6', '<')) {
        $this->renameAttribute($setup);
        $this->backupShopbyRoot();
    }
    if (version_compare($context->getVersion(), '2.9.7', '<')) {
        $this->addCategoryThumbnail($setup);
        $this->backupShopbyRoot();
    }

I have increased module version to 2.9.7 from 2.9.5 in module.xml.

This is code to update attribute code:

public function renameAttribute(ModuleDataSetupInterface $setup) {
    $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
    if ($categorySetup->getAttribute('catalog_category', 'thumbnail')) {
        $attributeThumbnail = $categorySetup->getAttribute('catalog_category', 'thumbnail');
        if($attributeThumbnail['attribute_id'] == 'XXX') {
            $categorySetup->updateAttribute('catalog_category', 'thumbnail', array('attribute_code' => 'thumbnail_migration'));
        }
    }
}

This is function to addattribute:

  private function addCategoryThumbnail(ModuleDataSetupInterface $setup)
{
   if ($categorySetup->getAttribute('catalog_category', 'thumbnail')) {
        return;
    }
  $categorySetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'thumbnail', [
        'type' => 'varchar',
        'label' => 'Thumbnail',
        'input' => 'image',
        'backend' => \Magento\Catalog\Model\Category\Attribute\Backend\Image::class,
        'required' => false,
        'sort_order' => 5,
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'General Information',
    ]);

    }

But I am unable to do so, onle one action i.e. updateattribute is performed at last and then I need to downgrade module version to addattribute.

Can this be done without this manual effort.

May be I think the issue is because I am updaing and adding attribute with same code. Any help ??

Your Answer

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

Browse other questions tagged or ask your own question.