cancel
Showing results for 
Search instead for 
Did you mean: 

Proper way to add product attribute on module upgrade

Highlighted

Proper way to add product attribute on module upgrade

Hi to all,

first let me tell I read the examples in official Magento 2 documentation, check lot of examples here and in stackoverflow, but nothing help me, and decide to ask.

I have a Magento 2 plugin, and in the next version I want to add a new attribute to all products. At first look this looks simple, but in my case it not work. Most of the examples suggest to do this in the InstallData class, but I am not sure will this class be called on upgrade, so I want to add the attribute in the UpgradeData class.

Here is what I am doing in my class:

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

class UpgradeData implements UpgradeDataInterface
{
        private $eavSetupFactory;

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

        public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context
    ) {
             $setup->startSetup();
             $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
             $eavSetup->addAttribute(
				\Magento\Catalog\Model\Product::ENTITY,
				'sample_attribute_on_update',
				[
					'type' => 'text',
					'backend' => '',
					'frontend' => '',
					'label' => 'Sample Upgrade Atrribute',
					'input' => 'text',
					'class' => '',
					'source' => '',
					'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
					'visible' => true,
					'required' => false,
					'user_defined' => true,
					'default' => '',
					'searchable' => true,
					'filterable' => true,
					'comparable' => false,
					'visible_on_front' => false,
					'used_in_product_listing' => true,
					'unique' => false,
					'apply_to' => '',
					'group' => 'General',
				]
			);

               $setup->endSetup();
       }

And nothing happen, there is no new record in eav_attribute table, there is no the attribute when try to add new product.

Because I do not check the version of the plugin in the UpgradeData, I do not change the version in etc/module.xml and composer.json. I also try to change the version, but again no success.

Can someone tell me what I am doing wrong?

Thanks and regards!

 

PS: at the end I execute: setup:upgrade, setup:di:compile, setup:static-content:deploy, cache:flush

8 REPLIES 8
Highlighted

Re: Proper way to add product attribute on module upgrade

@miroslav_stoev 

 

You can try by creating new module as shared in below link:

https://www.rakeshjesadiya.com/create-product-attribute-programmatically-in-magento-2/

 

I am not sure about your Magento version are you using as Magento 2.3 releases already DB schema to create attributes and install and update already deprecated. 

Manish Mittal
https://www.manishmittal.com/
Highlighted

Re: Proper way to add product attribute on module upgrade

Hi, my version is 2.3.3, soon will upgrade to 2.3.5.

I saw lot of examples, with InstallData class, but can I do this in UpgradeData class?

 

Regards!

Highlighted

Re: Proper way to add product attribute on module upgrade

@miroslav_stoev 

 

Yes, you can. Just follow below shared article:

https://webkul.com/blog/update-product-custom-attribute-scope-in-magento-2/

you have to update the version of the module from the module.xml

After updating these files, update your custom product attribute’s scope in your custom module in your Magento 2 instance by terminal run following command in your magento2 root directory.

 

php bin/magento setup:upgrade

Manish Mittal
https://www.manishmittal.com/
Highlighted

Re: Proper way to add product attribute on module upgrade

In this example I upgrade existing attribute, I want to add new one in UpgradeData, is this possible?

 

Regards!

Highlighted

Re: Proper way to add product attribute on module upgrade

Hello @miroslav_stoev 

 

you can do below thing

 

go to phpmyadmin or mysql and remove your module entry from setup_module

 

And change upgrade data to InstallData and then try to setup upgrade.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
Highlighted

Re: Proper way to add product attribute on module upgrade

Highlighted

Re: Proper way to add product attribute on module upgrade

I will check the links!

Regards!

Highlighted

Re: Proper way to add product attribute on module upgrade

@miroslav_stoev 

 

Sure, let me know if you need any other help!

Manish Mittal
https://www.manishmittal.com/