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.

  1. When an Admin creates a new customer in the backend, the customer custom address attribute is saved.
  2. When an Admin updates an existing customer account/custom address attribute, then the attribute is saved.
  3. 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.

  1. 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.

  1. 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 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.

cta_image

LOOKING OUT FOR A MAGENTO AGENCY ?

  • Ensure smooth, fast & frictionless buying experience
  • Increase transaction success

Get In Touch

Leave a Reply

Be the First to Comment!