Magento – Customer Address custom attribute not showing front end in Magento 2

custom-attributescustomer-addressmagento2

I am trying to create and save the Customer Address custom attribute for these I have created the module and it's successfully created and showing backend (adminhtml), But frontend not showing.

My code is:

<?php

namespace Learning\CA\Setup;


use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;



/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $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)
    {
        $setup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        if (version_compare($context->getVersion(), '2.1.0', '<')) {

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $customerSetup->addAttribute('customer_address', 'dummy_nav_customer_id', [
                'type' => 'varchar',
                'label' => 'Nav Customer ID',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'dummy_nav_customer_id')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
                ]);
            $attribute->save();
        }

        $setup->endSetup();
    }

}

enter image description here

And it's showing while debugging my code, after saving customer address see below pic.

enter image description here

Why it's not showing frontend? My code is wrong?

Best Answer

Customer Address edit form won't automatically generate custom attribute. You have to override template file vendor/magento/module-customer/view/frontend/templates/address/edit.phtml to show it in frontend.

E.g., override template in theme:

  • Copy vendor/magento/module-customer/view/frontend/templates/address/edit.phtml to app/design/frontend/[Vendor] /[theme]/Magento_Customer/templates/address/edit.phtml

  • Add this to new template:

    <div class="field dummy_nav_customer_id">
            <label class="label" for="dummy_nav_customer_id"><span><?php echo $block->escapeHtml(__('Nav Customer ID')) ?></span></label>
            <div class="control">
                <input type="text" name="dummy_nav_customer_id" value="<?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeHtmlAttr((!is_null($block->getAddress()->getCustomAttribute('dummy_nav_customer_id')) ? $block->getAddress()->getCustomAttribute('dummy_nav_customer_id')->getValue() : '')) ?>" title="<?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeHtmlAttr(__('Nav Customer ID')) ?>" class="input-text <?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('dummy_nav_customer_id')) ?>" id="dummy_nav_customer_id">
            </div>
        </div>
    
Related Topic