Magento 2 - Adding Customer Attributes

We need extra attributes for a customer like we did for product, but there is no interface for that in CE of Magento

Posted on April 11, 2017 in Magento2

In the following steps, we will create a small module that adds a customer attribute: loyaltynumber

Create a module Jeff_CustomerAttribute by creating files module.xml and registraction.php:


#app/code/Jeff/CustomerAttribute/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Jeff_CustomerAttribute" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Registration.php:


#app/code/Jeff/CustomerAttribute/Registration.php

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

Create a data installtion script: InstallData.php


#app/code/Jeff/CustomerAttribute/Setup/InstallData.php

<?php
namespace Jeff\CustomerAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {
    private $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $customerSetup =$this->customerSetupFactory->create(['setup'=>$setup]);

        $setup->startSetup();

        $customerSetup->addAttribute('customer', 'loyaltynumber', [
            'label'=>'Loyaltynumber',
            'type'=>'static',
            'frontend_input'=>'text',
            'required'=>false,
            'visible'=>true,
            'position'=>105,
        ]);

        $loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'loyaltynumber');

        $loyaltyAttribute->setData('used_in_forms', ['adminhtml_customer']);

        $loyaltyAttribute->save();

        $setup->endSetup();
    }
}

Add the attribute to the backend, we have to create a ui_component XML file


#app/code/Jeff/CustomerAttribute/view/base/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="loyaltynumber">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">customer</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

To install the attribute in the database, run the command php bin/magento setup:upgrade in the Magento 2 root folder.

The required step is to add the attribute in the ui_component xml configuration. This configuration works like the layout XML files. When you look in the file app/code/Magento/Customer/view/adminhtml/layout/customer_index_edit.xml, you will set the following code that initializes the layout update of the ui_component:


<uiComponent name="customer_form"/>

In the backend, create a new customer. You will wee the Loyaltynumber attribute in the form like in the following screenshot:



comments powered by Disqus