I added a customer custom attribute as customer_address type and it runs correctly in the admin and in onepagecheckout as well as in shipping & billing address.

I created: my_namespace/my_module/etc/module.xml and registration.php composer.json files in the module base directory.


namespace Namespace\Module\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

 * @codeCoverageIgnore
class InstallData implements InstallDataInterface
     * Customer setup factory
     * @var CustomerSetupFactory
    private $customerSetupFactory;

     * Init
     * @param CustomerSetupFactory $customerSetupFactory
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
        $this->customerSetupFactory = $customerSetupFactory;

     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);


        // insert attribute
        $customerSetup->addAttribute('customer_address', 'attr_code',  [
            'label' => 'My attribute',
            'type' => 'varchar',
            'input' => 'text',
            'position' => 45,
            'visible' => true,
            'required' => false,
            'system' => 0

        $MyAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'attr_code');
            ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']


Now I need to add attribute field in the customer add and edit address form that are related to the file magento_customer/view/frontend/templates/address/edit.phtml

I added the field but I'm not able to get and save the value of that attribute.

  • which magento version?
    – Sohel Rana
    Commented Jul 12, 2016 at 7:01
  • magento CE 2.1.0
    – Ale
    Commented Aug 11, 2016 at 9:00
  • Hi Ale, Can you please share your working code for custom customer address attribute. I also need to add same functionality.
    – Rahul
    Commented May 13, 2017 at 9:20

2 Answers 2


Custom customer attributes will never just 'appear' on the frontend like they do in the backend. The code that displays them on the frontend is located in a custom phtml file.

Magento EE has this functionality built in. I'm not suggesting you NEED to spend that money, I'm just saying it has it. If you want to go ahead and attempt to add custom attributes, it's somewhat complex.

First of all, you must do all of this in a module or it just isn't going to work right, and it's going to be hard to debug/upgrade later.

You have to do these things:

  • Create the attribute (you've done this if it shows up in admin)
  • override the frontend layout for referenceContainer form.additional.info
  • add a template phtml file to show the additional attribute(s)
  • add a block PHP file to load the new attributes and create the HTML
  • other things like learn how to automate the process and load multiples instead of hard coding it to load just the name of the one you created.

You can load your custom attributes in the Block PHP. Then just add your layout to the customer_account_create.xml like this:

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="customer_form_template_handle"/>
        <referenceContainer name="form.additional.info">
            <block class="Company\Customformattributes\Block\FormCustomer" template="Company_Customformattributes::customattributes.phtml" name="customer_form_user_attributes" cacheable="false">
                <action method="setFormCode">
                    <argument name="code" xsi:type="string">customer_account_edit</argument>
                <action method="setEntityModelClass">
                    <argument name="code" xsi:type="string">Magento\Customer\Model\Customer</argument>

This is the magic sauce to get your block PHP to load, to get your phtml to load, and to get it into the correct page.

This isn't a full answer honestly, there is a lot more to it, but you get the basic idea.

  • 1
    can you please complete your answer? what you have entered in block and template file? Commented Feb 10, 2017 at 5:20
  • setEntityModelClass action can automatically save our attribute or we need to write code for saving attribute also?
    – siddhesh
    Commented Jul 19, 2017 at 15:59

Cannot answer your question, because there is not enough code provided, but have a little advice. Did you check this tutorial Adding customer attirbute tutorial ?

Since Magento 2.1 there is a change and methods ->save() are deprecated. You should start using Repositories instead. For example for customer EAV you should use


In your case, second part of script should be changed to

/** Magento\Eav\Model\AttributeRepository $attributeRepository */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.