3

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?

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Sep 22, 2017 at 6:46
2
  • 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. Commented Sep 19, 2018 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 Commented Sep 19, 2018 at 13:20

2 Answers 2

5

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>
    
answered Sep 22, 2017 at 7:43
3
  • 1
    I am getting fatal error : Fatal error: Uncaught Error: Call to a member function getValue() on null in Commented Feb 26, 2019 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 Commented Feb 27, 2019 at 2:16
  • I am also getting same error getValue(), while adding new address at frontend. Is there any solution for this? Commented Dec 18, 2019 at 11:49
0

Here is the updated solution which I have applied and worked fine for me. First you have to override vendor/magento/module-customer/view/frontend/templates/address/edit.phtml file in your theme then add below code in it.Please replace custom_attribute by your own attribute code.

<div class="field custome_attribute">
<label class="label" for="custome_attribute"><span><?php echo $block->getAttributeData()->getFrontendLabel('custome_attribute'); ?></span></label>
<div class="control">
 <input type="text" name="custome_attribute" value="<?= ($block->getAddress()->getCustomAttribute('custome_attribute') === null) ? "" : $block->getAddress()->getCustomAttribute('custome_attribute')->getValue(); ?>" title="<?php echo __('custome_attribute') ?>" class="input-text" id="custome_attribute">
</div>
</div>

Hope this will work for you. Thank you. :)

answered Dec 18, 2019 at 13:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.