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.