phpmagentomagento2magento2.2

Adding a new custom Customer attribute when updating a module


I'm having troubles at creating new customer attribute when upgrading one of my modules.

I've created the UpgradeData.php file under /app/code/vendor/modulename/Setup/UpgradeData.php with the current code:

namespace Ucs\CustomerAttribute\Setup;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
class UpgradeData implements UpgradeDataInterface{

private $customerSetupFactory;

public function __construct(
    CustomerSetupFactory $customerSetupFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
}

/**
 * {@inheritdoc}
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){

    $setup->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda', [
            'type' => 'varchar',
            'label' => 'Nome azienda',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda')
            ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]]);
        $attribute->save();


        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco', [
            'type' => 'varchar',
            'label' => 'Codice Univoco',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco')
            ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]]);
        $attribute->save();


    }
}
}

in short, it needs to create 2 new text (varchar) attributes. My module.xml has setup_version="1.0.5" schema_version="1.0.5" so it should enter the version_compare condition and create the attribute, but, after running php bin/magento setup:upgrade it doesn't work. If i check in the setup_module table, the setup_version and schema_version change correctly with the version in the module.xml. It looks like for some reason the UpgradeData.php does not get executed at all.


Solution

  • In Ucs\CustomerAttribute\etc\module.xml change version to 1.0.6

    then replace

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

    with

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

    Edit: Just to be sure.. by

    I've created the UpgradeData.php file under /app/code/vendor/modulename/Setup/UpgradeData.php

    You mean app/code/Ucs/CustomerAttribute/Setup/UpgradeData.php ?

    Edit2:

    I assumed Your agency is called Ucs. That's why I've asked about it, beacuse that's what suggest Your module namespace. This is not recomended practice but for purpose of installator verification, change namespace to: namespace vendor\modulename\Setup;

    What I recomend is:

    1. Create a new module or find app/code/[YOURCOMPANYNAME]/Customer - try to corespond Magento native modules. This way You can easier manage code, design and Magento doesn't need to load separated module for each functionallity.

    2. In UpgradeData.php try to call separate function for each version. Like:

    
         if (version_compare($context->getVersion(), '1.0.1', '<')) {
                    $this->addCustomerMyAttribute();
                }
         $setup->endSetup();
    

    and then below

     private function addCustomerMyAttribute($setup){
    // Your code goes here
    
    }
    

    If it's first version of Customer module in app/code/[YOURCOMPANYNAME] remember to create InstallData.php insted of UpgradeData.php (in that case no need to check version).

    1. After bin/magento setup:upgrade check eav_attribute table for new attribute.

    2. If it's there remember to bin/magento indexer:reindex so it goes to flat table. If it's not there. Put ```die(var_dump('I'm running'); at the beginning of upgrade function.