At times, a merchant may need more information about a customer during the registration process apart from the address fields provided in the default registration form.
Magento 2 provides a feature in which the admin/merchant can add extra address fields in the registration form.
This feature works in the following scenarios…
Given below are the scenarios in which it works as intended.
- When an Admin creates a new customer in the backend, the customer custom address attribute is saved.
- When an Admin updates an existing customer account/custom address attribute, then the attribute is saved.
- In the frontend, if an existing customer edits and updates the custom address attribute, it is saved successfully.
But, when a new customer registers for the first time and if he provides address information in the custom field, it is not saved successfully in the database.
This article provides a solution to fix this issue and to save the information entered in the custom attribute in the database.
The following changes have been done in below mentioned files. Please check the line numbers.
- magento-enterprise-package/vendor/magento/module-customer-custom-attributes/view/frontend/templates/customer/form/register.phtml
Before
Line No 130:
After
The coding of the lines from 132 to 134 has been modified.
- magento-enterprise-package/vendor/magento/module-customer/Controller/Account/CreatePost.php
Remove the extractAddress() method code from line 194 – 237 in the Createpost.php file
And add the following code to that file
Include RegionInterface and RegionFactory class to this file.
Create a RegionFactory variable
Assign RegionFactory class as a variable in the construct method
Add extractAddress method
Here you have to add one new method called updateRegionData($attributeValues) to get the region_id.
That’s it. Clear the Magento cache and now the Customer Custom Address Attribute value will be saved in the database.
Hope this helps you to fix this issue. If you have any queries regarding the code, please let us know in the comment section below. We will be more than happy to help you.
You can focus on achieving your business goals.
Wondering who will take care of Magento Design Support then?
The answer is “we” – Get in touch! Let’s talk how!