Magento 2 – Save Custom Field in Shipping Address at Checkout

checkoutmagento-2.1magento2

There is any simple way in Magento2 to add custom field to Checkout? I have displayed my custom field in shipping address following by StackOverFlow tips and Magento docs. Unfortunally, Magento docs doesn't have info how to save this attribute to database.

I have moved my attributes from custom_attributes to extension_attributes, but values still is not saved in customer shipping address.

Can You give me tip how I should save my new field in custom attributes in shipping address in Customer details?

Best Answer

I solved my problem. Extra field in Shipping Address is saved to Quote first. So if you want to save new field in custom attributes in Customer details you have to do follow steps:

  1. Extend Extension Attributes for Magento\Quote\Api\Data\AddressInterface (in Magento docs)
  2. Extend Quote table in DB about new field Create

  3. set-shipping-information-mixin.js and move attributes to extension attributes (in Magento docs)

  4. Create plugin in ShippingInformationManagement before SaveAddressInformation method. Read variable from extension attributes and save it to Quote

  5. Overwrite "populateCustomerInfo" method in "Magento\Quote\Model\CustomerManagment and read new value from Quote and save it to custom attributes in customer object.