We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

Magento provides so many default fields but If we need custom information in customer address then we can create a new customer_address type attribute.

In this article, I will share how we can add text type attribute in Customer Address,

You need to create InstallData.php file in your existing module or you can create a new module as well as,

If you are creating in your existing module: app/code/[Vendor Name]/[Module Name]/Setup/InstallData.php

<?php
namespace [Vendor Name]/[Module Name]Setup;
 
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
 
class InstallData implements InstallDataInterface
{	
	/**
     * @var CustomerSetupFactory
     */
    private $customersetupFactory;
 	
 	/**
     * @var AttributeSetFactory
     */
    private $attributesetFactory;
 	
 	/**
     * @param CustomerSetupFactory $customersetupFactory
     * @param AttributeSetFactory $attributesetFactory
     */
    public function __construct(
        CustomerSetupFactory $customersetupFactory,
        AttributeSetFactory $attributesetFactory
    ) {
        $this->customersetupFactory = $customersetupFactory;
        $this->attributesetFactory = $attributesetFactory;
    }
 
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
 
        $customerSetup = $this->customersetupFactory->create(['setup' => $setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        $attributeSet = $this->attributesetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
 
        $customerSetup->addAttribute('customer_address', 'job_title', [
            'type' => 'varchar',
            'label' => 'Job Title',
            'input' => 'text', //You can change your input type as per your requirement
            'required' => false, //If You need this field is required in customer address area then you can just set "true" in stand of "false"
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
 
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'job_title')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer_address'], //Other list of forms: customer_address_edit, customer_register_address where you want to display the custom attribute 
            ]);
 
        $attribute->save();
    }
}

After creating the above file you have to run below commands:

php bin/magento setup:upgrade
php bin/magento cache:clean

Now you can check in the customer section in the admin area.
Our newly created attribute shown in the customer address form.

field_5bfb909c5ccae

    Recent Articles
    Get a Free Quote

      Full Name

      Email Address

      Mobile Number

      Message

      Let’s initiate a discussion!!

      With Only Agency that provides a 24/7 emergency support.

        Full Name

        Email Address

        Mobile Number

        Message