Back to Top

Custom customer address attribute in Magento 2

Updated 13 August 2024

Magento 2 provides some default attributes to store the customer address information.
But, sometimes, when we develop our custom modules, we may need to add extra details to the customer’s address.

In this scenario, we can create a custom address attribute to store the additional or extra information of the address.
So, in this blog, we will learn how to create a custom customer address attribute in Magento 2.

Firstly, create the file InstallData.php inside the app/code/Vendor/Module/Setup/ directory. And write the code to create a custom address attribute ‘custom_address_attribute’.

<?php
/**
 * Webkul Software.
 *
 * @category  Vendor
 * @package   Vendor_Module
 * @author    Vendor
 * @copyright Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Vendor\Module\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
    /**
     * @var Config
     */
    private $eavConfig;

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

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param Config $eavConfig
     * @param EavSetupFactory $eavSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        Config $eavConfig,
        EavSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->eavConfig            = $eavConfig;
        $this->_eavSetupFactory     = $eavSetupFactory;
        $this->attributeSetFactory  = $attributeSetFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute('customer_address', 'custom_address_attribute', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'Custom Address Attribute',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'group'=> 'General',
            'global' => true,
            'visible_on_front' => true,
        ]);
       
        $customAttribute = $this->eavConfig->getAttribute('customer_address', 'custom_address_attribute');

		$customAttribute->setData(
			'used_in_forms',
			['adminhtml_customer_address','customer_address_edit','customer_register_address'], //list of forms where you want to display the custom attribute
		);
        $customAttribute->save();
        
        $setup->endSetup();
    }
}

InstallData conforms to InstallDataInterface, which requires the implementation of the install method that accepts two parameters of type ModuleDataSetupInterface and ModuleContextInterface.

Using the addAttribute method on the instance of Magento\Eav\Setup\EavSetupFactory, we are instructing Magento to add the attribute ‘custom_address_attribute’.

Searching for an experienced
Magento 2 Company ?
Find out More

Then, we will execute the setup:upgrade command from CLI.
After the upgrade command execution, a custom address attribute will be created and now you can see this attribute’s field in the customer edit form at the Magento admin end.

Refer to the below image to see the result.

customer address attribute in Magento 2

Hope, this will be helpful. Thanks 🙂

Next Blog: Inspecting developer tools in iPad Device

You can check out more blogs related to custom attribute creation in Magento 2.
Refer to the below links:

How to Create Customer Custom Attribute in Magento 2
Magento2- How to create product custom attribute from installer in custom module
Add custom product attributes for different product types in Magento2

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home