I want to add a custom attribute on customer_address. I need to save the value in DB, trigger the value in frontend forms (new/edit address) and in backend also.
I used the following InstallData script in order to add a new attribute:
$installer = $setup;
$installer->startSetup();
/* @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute('customer_address', "chamber_of_commerce");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address'); //customer
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute('customer_address', 'chamber_of_commerce', [
'type' => 'varchar',
'label' => 'Chamber of Commerce',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
'sort_order' => 150
]);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'chamber_of_commerce')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer_address','customer_address_edit','customer_register_address']
]);
$attribute->save();
$installer->endSetup();
I used
'used_in_forms'=>['adminhtml_customer_address','customer_address_edit','customer_register_address']
but it seems that only in the backend form is added automatically and also saves a new value.
I manually added an input in order to show the attribute in frontend on new/edit address in CustomerAccount, but I cannot manage to get the value from DB or save a new value.
I think I am missing something and I would really appreciate if someone can take a look and give a hint.
I am using Magento 2.2.4
Update:
I used an observer for Magento\Customer\Model\Address in order to save the value for edit/new address from MyAccount page.
In order to get the value saved also from the checkout page, I used:
2.1. extension_attributes for Customer AddressInterface
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="chamber_of_commerce" type="string" />
</extension_attributes>
2.2. etc/fieldset:
<fieldset id="sales_convert_quote_address">
<field name="chamber_of_commerce">
<aspect name="to_customer_address" />
<aspect name="to_order_address" />
</field>
</fieldset>
2.3. plugin for Customer Address for beforeUpdateData function
<type name="Magento\Customer\Model\Address">
<plugin disabled="false" name="vendor_plugin_quote_model_address" sortOrder="10"
type="Vendor\Module\Plugin\Customer\Model\Address"/>
</type>