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();
}
}
And it's showing while debugging my code, after saving customer address see below pic.
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– Lucas Quintela2018年09月19日 12:51:52 +00:00Commented 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 ReviewRama Chandran M– Rama Chandran M2018年09月19日 13:20:41 +00:00Commented Sep 19, 2018 at 13:20
2 Answers 2
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.phtmltoapp/design/frontend/[Vendor] /[theme]/Magento_Customer/templates/address/edit.phtmlAdd 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>
-
1I am getting fatal error : Fatal error: Uncaught Error: Call to a member function getValue() on null inSheshgiri Anvekar– Sheshgiri Anvekar2019年02月26日 19:21:59 +00:00Commented Feb 26, 2019 at 19:21
-
@SheshgiriAnvekar If your custom attribute is not set,
getCustomAttributewill returnnull, you should check it first thengetValue()if not nullQuan Le– Quan Le2019年02月27日 02:16:01 +00:00Commented 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?Ronak Parmar– Ronak Parmar2019年12月18日 11:49:43 +00:00Commented Dec 18, 2019 at 11:49
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. :)
Explore related questions
See similar questions with these tags.