Create customer address attribute Programmatically using Patch data in Magento 2.3

Create a customer address attribute programmatically in Magento 2 using the simple module by Best Coding Standard Approach using Setup patch data.

In our demo, I have created a simple text field attribute for a customer address called Nickname.

You can see a customer attribute in backend using Customer -> All Customer -> Click on Edit Customer Link.
Go to Addresses Tab and edit or add new Address.

Create a customer address attribute we need to create a PHP file for defining our custom, customer address attribute name and attribute_code.

We need to create a PHP file inside the Setup\Patch\Data folder.

Let’s start with a simple custom module to create custom customer address attribute,

You need to create the first registration.php and module.xml file for defining our module to Magento. Here I have used Rbj as Packagename where AddressAttribute is module name.

Path: app/code/Rbj/AddressAttribute/registration.php

Create module.xml file, Path: app/code/Rbj/AddressAttribute/etc/module.xml

Create AddressAttribute.php file to define our custom customer address attribute,
Path: app/code/Rbj/AddressAttribute/Setup/Patch/Data/AddressAttribute.php

In the above file, You can set used_in_forms value as per your requirement to display attribute in a different section of customer page.

Now Run the command from the Magento root instance,

php bin/magento setup:upgrade

You can see the new Customer address attribute is generated in a defined scope above.

One new entry generated in the eav_attribute table.


2 Replies to “Create customer address attribute Programmatically using Patch data in Magento 2.3”

    1. Hi Mohan, You can display this new field in frontend but for that you have to override vendor/magento/module-customer/view/frontend/templates/address/edit.phtml file in your module or theme and add below code in it.

      getAttributeData()->getFrontendLabel('nickname'); ?>

      <input type="text" name="nickname" value="getAddress()->getCustomAttribute('nickname') === null) ? "" : $block->getAddress()->getCustomAttribute('nickname')->getValue(); ?>" title="" class="input-text" id="nickname">

      This is working for me, hope this will work for you. Thank you

Leave a Reply