I'm trying to figure out how to achieve a simple task that in magento 1 was very simple, adding an attribute to an entity. In my case an attribute to customer_address, so in magento tutorial only appears how to add it to the form in checkout using extension attributes (which is not very clear). So far I have added the attribute by installData, using the config:
$MyAttribute->setData(
'used_in_forms',
['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
);
But it only appears in admin, and it works fine saving/editing address, it creates the linked attribute in customer_address_entity_varchar, but cannot figure out how to save the addresses created from checkout and from edit address in customer dashboard, also, if the user does not save the address to address book, it only gets saved in sales_order_address, and even there is not EAV use for attributes, so, how do you have to use the EAV attributes and extension attribute for this? An explanation would be very appreciated.
Best Answer
I had to do something similar in a Twilio module I wrote, https://github.com/pmclain/module-twilio. I've included the relevant code for adding the custom address attribute
sms_alert
to the frontend customer address edit forms and the checkout address edit forms. I omitted the setup scripts, since your original question included them.If you haven't already you will want to add an
extension_attributes.xml
The customer address template is a configuration value located in:
If you haven't set a custom value for this setting you can define the default value with your module's
config.xml
.You'll need to add your attribute to the checkout javascript layout. I did this with a plugin, but I imagine it could be done with xml layouts.
Next there are a handful of mixins you will need for capturing and displaying the information in the knockout templates used during checkout.
Now we'll add the custom attribute to the customer address form contained in the customer account section. This is an override of the customer address edit form. This doesn't seem like a clean solution, but since the form fields are all predefined in the template I did not see another option.