Magento 2.x – Update attribute type programmatically
If you need to update an attribute type programmatically, that you have done before (or not), you will need to proceed as follows, using an InstallData.php script in a custom module:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $productEntityTypeId = $eavSetup->getEntityTypeId('catalog_product'); if (version_compare($context->getVersion(), '1.0.3', '>=')) { // Remove previous creation $eavSetup->removeAttribute($productEntityTypeId, 'attribute_name'); // Create the attribute again $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'attribute_name', [ 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'My Label', 'input' => 'text', 'class' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => 'simple' // It could be bundle, or whatever. ] ); } } |
After that, remember to upgrade the version of your module in etc/module.xml and run the following commands:
|
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento cache:flush |
Then, make a try and the field should be updated.!