How to create Customer Address Attribute in Magento 2

Before we start the Implementation, lets understand the few terms that we are going to use in further blog.
– Eav (entity attribute value)
– Entity
– Attribute or Property
– Forms
EAV stands for Entity Attribute and Value, Eav is a data model which describes the Entities where attributes are the properties or Arguments or parameters. Basically EAV is a design pattern where each component is a separate table that is why it is also know as the Open Schema.

Entity : It is in Magento known as the Data Item, such as Customer, Product, Category, Address, order Quote etc. Each Entity have its own records in DB (DataBase).

Attribute : These are the data Items owned by Entity (Just explained above) Ex : Name, Price, etc.

Forms : When ever we create any customer attribute then we need to specify in which form you need to show it, for that in Magento we have the table named as customer_form_attribute, that contains the forms for the attribute. Ex : adminhtml_customer , customer_account_create, customer_account_edit etc. used_in_forms is a property of attribute that stores the forms value for attribute.

Now lets see how to create customer attributes :
Step 1 : Create a new Module, first of all create registration.php , app/code/Blogento/CustomerAddressAttribute/registration.php

<?php
/**
* Module registration
*
* @author Blogento
* @package Blogento_CustomerAddressAttribute
*/
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Blogento_CustomerAddressAttribute',
    __DIR__
);

Step 2 : Create module.xml file, app/code/Blogento/CustomerAddressAttribute/etc/module.xml

<?xml version="1.0" ?>
<!--
/**
* Definition of module name, version and dependencies
*
* @author Blogento
* @package Blogento_CustomerAddressAttribute
*/ -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Blogento_CustomerAddressAttribute" setup_version="2.0.0">
    </module>
</config>

Step 3 : Now create the patch file here : app/code/Blogento/CustomerAddressAttribute/Setup/Patch/Data/CustomerAddressAttribute.php

<?php
/**
* Patch to create Customer Address Attribute
*
* Creates nickname custom address attribute
*
* @author Blogento
* @package Blogento_CustomerAddressAttribute
*/
namespace Blogento\CustomerAddressAttribute\Setup\Patch\Data;

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

/**
* Class Customer Address Attribute
*/
class CustomerAddressAttribute 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', 'alternative_email', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'Alternative Email',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system' => false,
            'group' => 'General',
            'global' => true,
            'visible_on_front' => false,
        ]);
        $customAttribute = $this->eavConfig->getAttribute('customer_address', 'alternative_email');
        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address']
        );
        $customAttribute->save();
    }

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

Now run the following commands :
php bin/magento module:enable Blogento_CustomerAddressAttribute
php bin/magento setup:upgrade

Happy Coding 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top