Magento – How to add a customer custom-attribute in the customer address edit form

attributescustom-attributescustomer-addressmagento2

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.

my_namespace/my_module/Setup/InstallData.php

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]);

        $setup->startSetup();

        // 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');
        $MyAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        );
        $MyAttribute->save();

        $setup->endSetup();
    }
}

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.

Best Answer

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"/>
    <body>
        <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>
                <action method="setEntityModelClass">
                    <argument name="code" xsi:type="string">Magento\Customer\Model\Customer</argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>

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.

Related Topic