2

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?

  • Are you have this code in github? I have the same problem but I can not solve. Sorry about my english, I don't write very well. – Lucas Quintela Sep 19 '18 at 12:51
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Rama Chandran M Sep 19 '18 at 13:20
4

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($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>
    
  • I am getting fatal error : Fatal error: Uncaught Error: Call to a member function getValue() on null in – Sheshgiri Anvekar Feb 26 at 19:21
  • @SheshgiriAnvekar If your custom attribute is not set, getCustomAttribute will return null, you should check it first then getValue() if not null – Quan Le Feb 27 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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