4

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?

7
$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
            ]
        );
    }
| improve this answer | |
  • When I update like this, It's value is stored as null in flat table. – SIBHI S Dec 5 '17 at 13:46
  • One more question, do you want to add price attribute? – Khoa TruongDinh Dec 5 '17 at 13:53
  • Did you allow showing this attribute in catalog product listing? – Khoa TruongDinh Dec 5 '17 at 14:08
  • Yes, I need as price attribute. Yes I'm allowing this in listing – SIBHI S Dec 5 '17 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. – Khoa TruongDinh Dec 6 '17 at 14:09

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.