5

I have to change the type of an attribute from text to decimal. I used upgrade script below. But it is not working

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

class UpgradeData implements UpgradeDataInterface
{

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


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

        if (version_compare($context->getVersion(), '1.0.1', '<')) {

            $entityType = $eavSetup->getEntityTypeId('catalog_product');

            $eavSetup->updateAttribute($entityType, 'as_low_as', 'frontend_input','decimal', null);
        }
    }
}

How can we update the attribute type of an existing attribute?

3 Answers 3

10
$eavSetup->updateAttribute($entityType,'as_low_as','frontend_input','text');
$eavSetup->updateAttribute($entityType,'as_low_as','backend_type','decimal');
  • Frontend input should be text

  • Backend type should be decimal

If wanting it to be a price, don't forget to add backend model: Magento\Catalog\Model\Product\Attribute\Backend\Price. And frontend_input should be price, backend_type should be decimal.

$eavSetup->updateAttribute($entityType, 'as_low_as', 'frontend_input','price', null);
$eavSetup->updateAttribute($entityType,'as_low_as','backend_type','decimal');
$eavSetup->updateAttribute($entityType,'as_low_as','backend_model','Magento\Catalog\Model\Product\Attribute\Backend\Price');

[EDIT]

We can remove the old one, and add new one:

 if (version_compare($context->getVersion(), '1.0.4', '<')) {
        $eavSetup->removeAttribute($entityType, 'as_low_as');
        $eavSetup->addAttribute(
            'catalog_product',
            'as_low_as',
            [
                'label' => 'As Low As',
                'input' => 'price',
                'type' => 'decimal',
                'source' => '',
                'required' => false,
                'visible' => true,
                'system' => false,
                'default' => '',
                'backend' => 'Magento\Catalog\Model\Product\Attribute\Backend\Price',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'user_defined' => false,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false
            ]
        );
    }
10
  • When I update like this, It's value is stored as null in flat table.
    – SIBHI S
    Dec 5, 2017 at 13:46
  • One more question, do you want to add price attribute? Dec 5, 2017 at 13:53
  • Did you allow showing this attribute in catalog product listing? Dec 5, 2017 at 14:08
  • Yes, I need as price attribute. Yes I'm allowing this in listing
    – SIBHI S
    Dec 5, 2017 at 14:10
  • 1
    I have a suggestion for you! Before updating, you export the current data cab. And then, upgrade data attribute. Finally, importing data again. Dec 6, 2017 at 14:09
0
<?php 
namespace vendor\Custom\Setup;

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

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

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $setup->startSetup();
            $entityTypeId = 4;
            $attribute_id = 114;
            $propertyName1 = 'is_filterable';
            $propertyName2 = 'is_filterable_in_search';
            $propertyValue = 1;
            $this->eavSetupFactory->updateAttribute($entityTypeId,$attribute_id,$propertyName1,1,null); 
            $this->eavSetupFactory->updateAttribute($entityTypeId,$attribute_id,$propertyName2,1,null);
            $setup->endSetup();
        } 
    } 

}
?>

0

Please use patch file instead of UpgradeData.php

app/code/Vendorname/ModuleName/Setup/Patch/Data

UpdateProductAttribute.php

<?php

namespace Vendorname\ModuleName\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class UpdateProductAttribute implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $entityType = $eavSetup->getEntityTypeId('catalog_product');

        // Update you_attr_code Attribute
        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'frontend_input', 'price', null);

        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'backend_model', 'Magento\Catalog\Model\Product\Attribute\Backend\Price');

        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'attribute_model', 'Magento\Catalog\Model\ResourceModel\Eav\Attribute');

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $entityType = $eavSetup->getEntityTypeId('catalog_product');

        // Revert you_attr_code Attribute
        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'frontend_input', 'text', null);

        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'backend_model', null);

        $eavSetup->updateAttribute($entityType, 'you_attr_code', 'attribute_model', null);


        $this->moduleDataSetup->getConnection()->endSetup();
    }

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

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

Here I convert my attribute to price input type

For More: https://developer.adobe.com/commerce/php/development/components/declarative-schema/patches/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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