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

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

Create module.xml file, Path: app/code/Rbj/AddressAttribute/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="Rbj_AddressAttribute" setup_version="1.0.0">
	</module>
</config>

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

<?php declare(strict_types=1);

/**
 * Patch to create Customer Address Attribute
 *
 * Creates nickname custom address attribute
 *
 * @author Rakesh Jesadiya
 * @package Rbj_CustomerAddress
 */

namespace Rbj\CustomerAddress\Setup\Patch\Data;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * Class AddressAttribute
 */
class AddressAttribute implements DataPatchInterface
{
    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * AddressAttribute constructor.
     *
     * @param Config              $eavConfig
     * @param EavSetupFactory     $eavSetupFactory
     */
    public function __construct(
        Config $eavConfig,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create();

        $eavSetup->addAttribute('customer_address', 'nickname', [
            'type'             => 'varchar',
            'input'            => 'text',
            'label'            => 'Nickname',
            'visible'          => true,
            'required'         => false,
            'user_defined'     => true,
            'system'           => false,
            'group'            => 'General',
            'global'           => true,
            'visible_on_front' => false,
        ]);

        $customAttribute = $this->eavConfig->getAttribute('customer_address', 'nickname');

        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address',
             'customer_address_edit',
             'customer_register_address']
        );
        $customAttribute->save();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases(): array
    {
        return [];
    }
}

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.