Add fields to woocommerce registration form
-
Hi
Woocommerce allows user registration. I would like to add two woocommerce fields to that registration form (billing_first_name) and (billing_last_name). Is it possible to add these two files with editin the “Mytheme”/woocommerce/myaccount/form-login.php template file?
I can’t seem to get the template right. For example there’s this kind of code which is email field how could I modify it to make it add billing_first_name ?
<p class="form-row form-row-wide"> <label for="reg_email"><?php _e('Email', 'woocommerce'); ?> <span class="required">*</span></label> <input type="email" class="input-text" name="email" id="reg_email" value="<?php if (isset($_POST['email'])) echo esc_attr($_POST['email']); ?>" /> </p>
I’m quite new to wordpress so I might be totally off here.
Thank you in advance for your help!
-J
-
Please read this tutorial on how to customise the checkout fields. This clearly explains what code needs to be added in order to add or modify checkout fields. Yes, this does require some coding experience, we are working on an extension to make this configurable via the administration panel.
Thank you for the response!
Yes I had a look at that tutorial before, but If I understand it correctly it’s about modifying or customizing checkout fields.
What I’m trying to do is ADD billing_first_name and billing_last_name fields to woocommerce registration form. (In woocommerce account page, so that during registration first and last name would be collected)
https://dl.dropbox.com/u/2655364/wc-add.JPG
I’m trying to add the two checkout fields (billing_first_name and billing_last_name) in the registration form where the arrows are.
am I missing something very obvious here? ??
We do not have a tutorial on that, but there are hooks available in that form as well. You will have to dig in the code to find the right one, but
register_form
is a start to add your new form elements.Hi Coen
Sorry to dig up old posts but I found this in a google search.
I’ve been attempting to insert a custom field into the user registration process (a select for ‘how did you hear about us?’ ) and have read up on both the register_form and register_post actions, and their use in WC during form-login.php and woocommerce_process_registration().
My issue at the moment is that I can’t find a suitable hook to update the user meta once processed, as register_post is only called before the user is created.
Would it be possible for future updates to include an “add_user_meta” action hook around line 665 of woocommerce-functions.php or a filter for the wp_update_user used to set the role to customer so we can add out own meta in here?
If there’s something I’ve missed, making all this unnecessary please let me know
Thanks
Aj
Hi!
I find that the default checkout process is kinda lengthy. I’m trying to cut down some on some steps by requiring the info during the login/registration page.
I’ve managed to add the extra fields to the login/registration page, but the info doesn’t save to their wordpress user profile.
I’m not php proficient, so I may be asking the same thing as Aj…
Here’s an example of what I added to woocommerce/templates/myaccount/form-login.php
<p class=”form-row form-row-first”>
<label for=”first_name”><?php _e( ‘First Name’, ‘woocommerce’ ); ?> <span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”first_name” id=”reg_first_name” value=”<?php if (isset($_POST[‘first_name’])) echo esc_attr($_POST[‘first_name’]); ?>” />
</p>What else would I have to do to capture the info in their wordpress user profile? I’d love to be able to add what Aj is trying to do too… the ‘How did you hear about us?’ thing.
Many Thanks!
Hi,
I managed to map the WP default profile fields, first name and last name in woocommerce by modifying templates/myaccount/form-login.php and woocommerce-functions.php using the following tutorial as a base:
https://tommcfarlin.com/add-custom-user-meta-during-registration/As per the instructions, I made the following edits:
1. Added the following to form-login.php
<div class="form-row form-row-first"> <label for="reg_firstname"><span class="required">*</span> <strong><?php _e('Firstname', 'woocommerce'); ?></strong></label> <input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="<?php if (isset($_POST['firstname'])) echo esc_attr($_POST['firstname']); ?>" /> </div> <div class="form-row form-row-last"> <label for="reg_lastname"><span class="required">*</span> <strong><?php _e('Lastname', 'woocommerce'); ?></strong></label> <input type="text" class="input-text" name="lastname" id="reg_lastname" size="30" value="<?php if (isset($_POST['lastname'])) echo esc_attr($_POST['lastname']); ?>" /> </div>
2. Added the following to woocommerce-functions.php in the function woocommerce_process_registration()
$user_firstname = isset( $_POST['firstname'] ) ? trim( $_POST['firstname'] ) : ''; $user_lastname = isset( $_POST['lastname'] ) ? trim( $_POST['lastname'] ) : '';
and
$new_customer_data = array( 'user_login' => $sanitized_user_login, 'user_pass' => $password, 'user_email' => $user_email, 'role' => 'customer', 'first_name' => $user_firstname, 'last_name' => $user_lastname );
The thing is that while #1 can be achieved by overriding the woo templates through your theme but I am not sure how to modify / override a specific function. The only problem is that if the plugin is updated, you will lose the changes in #2 and will have to make the changes again.
As far as adding custom fields like “Where did you hear about us” might be possible using the same method above.
Hope it helps!
As a follow up to rahul.sahni0105’s answer:
I also added in this at around link 757 (where it checks the e-mail address) to male these required fields. If you DON’T use an email address as a username, you’ll need to put it in again at about line 740 after where it checks the username. :
elseif ( $user_firstname == '' ) { $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter a first name.', 'woocommerce' ) ); } elseif ( $user_lastname == '' ) { $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter a last name.', 'woocommerce' ) ); }
Hope this helps someone!
I just referred to this: Hooks
And the reference above: Custom Fields
To modify the fields on the Edit My Addresses page.Another way to do it is to modify directly this file in your directory: woocommerce/classes/class-wc-countries.php
Thanks for all the help folks! Wish this was easier to implement within WooCommerce, but you’ve helped a ton.
Any chance you know of a way to edit the user profile data such as first and last name from with /my-account?
I wish I could get an extension for this or a plugin… $$ :p
Hello i was looking for this too but since could not find anything decided to write something myself.
Since i dont like (and would not recommend) editing woocommerce plugin files.
I did everything through my themes functions.php file. Everything seems to be working well and all the proper hooks and filters are triggering.
here we go://Adding Registration fields to the form add_filter( 'register_form', 'adding_custom_registration_fields' ); function adding_custom_registration_fields( ) { //lets make the field required so that i can show you how to validate it later; echo '<div class="form-row form-row-wide"><label for="reg_firstname">'.__('First Name', 'woocommerce').' <span class="required">*</span></label> <input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="'.esc_attr($_POST['firstname']).'" /></div>'; } //Validation registration form after submission using the filter registration_errors add_filter('registration_errors', 'registration_errors_validation', 10,3); function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) { global $woocommerce; extract($_POST); // extracting $_POST into separate variables if($firstname == '' ) { $woocommerce->add_error( __( 'Please, fill in all the required fields.', 'woocommerce' ) ); } return $reg_errors; } //Updating use meta after registration successful registration add_action('woocommerce_created_customer','adding_extra_reg_fields'); function adding_extra_reg_fields($user_id) { extract($_POST); update_user_meta($user_id, 'first_name', $firstname); // can also do multiple fields like that update_user_meta($user_id, 'first_name', $firstname); update_user_meta($user_id, 'billing_first_name', $firstname); update_user_meta($user_id, 'shipping_first_name', $firstname); }
Thats about it! Would advice wordpress developers learn more about hooks and filters! they make everything so much more easier!
Have a good day!
Well are all these capability to add fields to user/account going to be available with wooCom 2.1 which is going to be released very soon?
I would hate to start on this kind of implementation (this would be my first foray into php…!) only to discover that wooCom 2.1 includes all it….
Thanks
I tried this above code to add a field to wooCom registration form. It works – kinda… it adds the code BELOW the Captcha.
Any ways around this?
I had that issue! – but create a easy solution. I posted a video with the solution, that you can find here: Create new fields on the woocommerce registration page
Hope it might helps you??
/Flemming
Hi Flemming117, Thanks for the link. I notice that the code above from karbanovich has:
add_filter( 'register_form', 'adding_custom_registration_fields' );
from my experience it should have been add_action.In any case, good news, I solved my problem with the CAPTCHA showing before my added fields. What is did…. make sure my action is before the Captcha
remove_action( 'register_form', 'cptch_register_form' ); add_action( 'register_form', 'adding_custom_registration_fields' ); add_action( 'register_form', 'cptch_register_form' );
Hi Flemming,
thanks so much for your video! It seems to be exactly what I′ve been looking for the whole day and so far it looks like as if it works for me too ??
Nicole
- The topic ‘Add fields to woocommerce registration form’ is closed to new replies.