cancel
Showing results for 
Search instead for 
Did you mean: 

Add new customer attribute

SOLVED

Add new customer attribute

Hello,

 

I want to add some new attributes and for the beginning just one new attribute.

The module is enabled, but did not display the new field in the admin view:

 

I add some files:

app/code/Company/CustomerAttribute/registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_CustomerAttribute',
    __DIR__
);

app/code/Company/CustomerAttribute/etc/module.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Company_CustomerAttribute" setup_version="1.0.0">
    <sequence>
      <module name="Magento_Customer"/>
    </sequence>
  </module>
</config>

app/code/Company/CustomerAttribute/etc/extension_attributes.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="customer_number" type="string"/>
    </extension_attributes>
</config>

app/code/Company/CustomerAttribute/Setup/InstallData.php:

<?php
 
namespace Company\CustomerAttribute\Setup;
 
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
 
class InstallData implements InstallDataInterface
{
 
    private $customerSetupFactory;
 
    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }
 
    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 
        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_number', [
            'type' => 'varchar', // type of attribute
            'label' => 'Customer Number',
            'input' => 'text', // input type
            'source' => '',
            'required' => false, // if you want to required need to set true
            'visible' => true,
            'position' => 500, // position of attribute
            'system' => false,
            'backend' => ''
        ]);
        
        /* Specify which place you want to display customer attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customer_number')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

Where is my mistake?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Add new customer attribute

@hanhoe 

Make sure that you have deleted installed module entry from the setup_module tables. 

DELETE FROM `setup_module` WHERE `module` = 'VP_CustomerAttribute'; // Module name


You have deleted from "eav_attribute" table. 

DELETE FROM `eav_attribute` WHERE `attribute_code` = 'YOUR_ATTRIBUTE_CODE';


BTW for change attribute label following query can be used:

UPDATE `eav_attribute` set `frontend_label` = 'YOUR ATTRIBUTE LABEL' WHERE `attribute_code` = 'YOUR_ATTRIBUTE_CODE';

View solution in original post

12 REPLIES 12

Re: Add new customer attribute

Hi @hanhoe 

I have attached a working module. You can check into it or you can use the same. 

Customer attribute module Download from here. 

If you have already installed your module then you uninstall in or use the attached after customer attribute name InstallData.php file.

I hope it will help you!

Re: Add new customer attribute

after updating the files ... which commands I should run?

  1. bin/magento setup:upgrade
  2. bin/magento indexer:reindex
  3. bin/magento cache:clean

 

And I expect the new file in admin at Customers => All Customers => Customer edit => Account Information

right?

Re: Add new customer attribute

@hanhoe 

Yes, it will add in admin customer form.

you need to run following command:

chmod -R 0777 var/ pub/ generated/
rm -rf var/cache/* var/view_preprocessed/* generated/* pub/static/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
php bin/magento indexer:reindex
chmod -R 0777 var/ pub/ generated/

If you are on developer mode then you need to run only following commands:

chmod -R 0777 var/ pub/ generated/
rm -rf var/cache/* var/view_preprocessed/* generated/* pub/static/*
php bin/magento setup:upgrade
php bin/magento cache:flush
php bin/magento indexer:reindex
chmod -R 0777 var/ pub/ generated/

Re: Add new customer attribute

@Vimal Kumar 

 

Thank you ... not sure where the problem is. I have now your field in the customer admin view, but not able to remove or change the label.

 

  1. When I delete the folder in apps/code/ ... and run your commands again ... is the attribute removed from the view? Or did I need first uninstall or disable?
  2. When I change just the label and run your update commands again ... should this work?

The attribute is still displayed.

 

Thank you for your help

Re: Add new customer attribute

Hey @hanhoe 
If you will uninstall/delete module again then you need to delete attribute the table as well. 

 

you can directly update attribute label from the database as well..

Following tables store the data.

eav_attribute
eav_attribute_label

use following query:

update eav_attribute_label set `value` = 'NEW LABEL TEXT' where attribute_id in (select attribute_id from eav_attribute where attribute_code ='YOUR_ATTRIBUTE_CODE'):

NOTE: I have not tested query. please check manually both tables once or manually edit the attribute label from the table.

 

Re: Add new customer attribute

Hi @Vimal Kumar 

 

oh, OK ... so I have to delete and update this directly by SQL query. OK: understand

 

But the new attribute is not included when I read the customer by API. Is it possible to add this by default?

 

Thank you for your support!

Re: Add new customer attribute

Hi @Vimal Kumar 

 

so weird:

I delete it from the database. I have only in eav_attribute one row ... in eav_attribute_label I did not have a row.

Then I remove all files from server, run your updatecode and copy your example files again on the server and re-run again your commands.

 

I tried several times, but the field would not be added again ... he don´t write a attribute in the database.

 

I tested it with:

SELECT * FROM `eav_attribute` WHERE attribute_code ='customer_number';

 

Anytime empty result!

Re: Add new customer attribute

@hanhoe 

Make sure that you have deleted installed module entry from the setup_module tables. 

DELETE FROM `setup_module` WHERE `module` = 'VP_CustomerAttribute'; // Module name


You have deleted from "eav_attribute" table. 

DELETE FROM `eav_attribute` WHERE `attribute_code` = 'YOUR_ATTRIBUTE_CODE';


BTW for change attribute label following query can be used:

UPDATE `eav_attribute` set `frontend_label` = 'YOUR ATTRIBUTE LABEL' WHERE `attribute_code` = 'YOUR_ATTRIBUTE_CODE';

Re: Add new customer attribute

well, now its working:

If I change something in my module, I have:

remove all files, run cli commands, clean database

then

restore all files, run cli commands

 

Not very user friendly :-)