I have seen similar questions but couldn't find a solution for me.
I am trying to add custom fields to WooCommerce registration form, specifically first and last name field. I have managed to create these fields but the information entered does not pass over to the Account Details page when the user has logged in. Other tutorials have mentioned validating the fields but I am not sure is it relevant to me or not. I am working on a Wordpress child theme.
Please visit to codepad .org to view the code. I tried to paste the code here by using the code sample option but it doesn't work properly.
Hope I have explained myself clearly. If not please let me know and I will clarify.
I think you have overwrite
woocommerce/templates/myaccount/form-login.php
template, and by doing that you have managed to show thebilling_first_name
andbilling_last_name
but you forget to usewoocommerce_created_customer
hook which is needed to save those data into your database.What I'll suggest is that you keep the template as it is and add those field through
function.php
Here is the code to add custom fields in WooCommerce registration form:
Coming to your second part of your question on validation it is totally optional and depends on your business logic that what you want, in general most of the site has First Name and the Last Name required but again it totally depends upon you, If you don't want to validate this then remove
<span class="required">*</span>
from above code and skip this section.Now this is a main part and this what you was had missed, below code is needed to save the custom data:
All the above code goes in
function.php
file of your active child theme (or theme). Or also in any plugin php files.The code is tested and fully functional.
Hope this helps!