Support » Plugin: WooCommerce » Add fields to woocommerce registration form : follow up

  • Hey all,

    Had just read this post today after looking for this same thing, and even tho it’s closed for comments, I wanted to share the solution that a colleague helped me come up with (thanks @rarst).

    The following code pasted into your theme will add both a First Name and a Last Name field to the registration form, both of which will also be saved to the Billing and Shipping names fields as well. Additionally, there is a notice if the fields aren’t filled in.

    This has been tested and is working great for me. Hope it can help some of you. Enjoy 🙂

    //Adding Registration fields to the form 
    
    add_action( '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;
    	$firstname = empty( $_POST['firstname'] ) ? '' : $_POST['firstname'];
    	$lastname  = empty( $_POST['lastname'] ) ? '' : $_POST['lastname'];
    	?>
    	<div class="form-row form-row-wide">
    		<label for="reg_firstname"><?php _e( 'First Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="<?php echo esc_attr( $firstname ) ?>" />
    	</div>
    	<div class="form-row form-row-wide">
    		<label for="reg_lastname"><?php _e( 'Last Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="lastname" id="reg_lastname" size="30" value="<?php echo esc_attr( $lastname ) ?>" />
    	</div><?php
    }
    
    //Validation registration form  after submission using the filter registration_errors
    add_filter( 'woocommerce_registration_errors', 'registration_errors_validation' );
    
    /**
     * @param WP_Error $reg_errors
     *
     * @return WP_Error
     */
    function registration_errors_validation( $reg_errors ) {
    
    	if ( empty( $_POST['firstname'] ) || empty( $_POST['lastname'] ) ) {
    		$reg_errors->add( 'empty required fields', __( 'Please fill in 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);
    	update_user_meta($user_id, 'last_name', $lastname);
    	update_user_meta($user_id, 'billing_first_name', $firstname);
    	update_user_meta($user_id, 'shipping_first_name', $firstname);
    	update_user_meta($user_id, 'billing_last_name', $lastname);
    	update_user_meta($user_id, 'shipping_last_name', $lastname);
    }

    https://wordpress.org/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 24 total)
  • Thank you Jenny!!!!

    I have been banging my head against a brick wall on this for months with various projects, with no solution.

    Your code snippet works perfectly, although it puts the extra fields below the existing ones, so I just removed them from form-login.php (obviously in my theme folder, not the core WP plugin) and re-added using your method, but placing them below the first and last name.

    Thanks again 🙂

    Shilpa

    (@kshilpa1385)

    Thanks jennybeaumont…

    …searching lot for adding field to woocommerce registration form.
    finally got solution.
    It’s work for me and I add one more field in that form of mobile number:

    //Adding Registration fields to the form 
    
    add_action( '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;
    	$firstname = empty( $_POST['firstname'] ) ? '' : $_POST['firstname'];
    	$lastname  = empty( $_POST['lastname'] ) ? '' : $_POST['lastname'];
    	$phone = empty( $_POST['phone'] ) ? '' : $_POST['phone'];
    	?>
    	<div class="form-row form-row-wide">
    		<label for="reg_firstname"><?php _e( 'First Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="<?php echo esc_attr( $firstname ) ?>" />
    	</div>
    	<div class="form-row form-row-wide">
    		<label for="reg_lastname"><?php _e( 'Last Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="lastname" id="reg_lastname" size="30" value="<?php echo esc_attr( $lastname ) ?>" />
    	</div>
    	<div class="form-row form-row-wide">
    		<label for="reg_phone"><?php _e( 'Phone', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="phone" id="reg_phone" size="10" value="<?php echo esc_attr( $phone ) ?>" />
    	</div><?php
    }
    
    //Validation registration form  after submission using the filter registration_errors
    add_filter( 'woocommerce_registration_errors', 'registration_errors_validation' );
    
    /**
     * @param WP_Error $reg_errors
     *
     * @return WP_Error
     */
    function registration_errors_validation( $reg_errors ) {
    
    	if ( empty( $_POST['firstname'] ) || empty( $_POST['lastname'] ) || empty( $_POST['phone']) ) {
    		$reg_errors->add( 'empty required fields', __( 'Please fill in 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);
    	update_user_meta($user_id, 'last_name', $lastname);
    	update_user_meta($user_id, 'phone', $phone);
    	update_user_meta($user_id, 'billing_first_name', $firstname);
    	update_user_meta($user_id, 'shipping_first_name', $firstname);
    	update_user_meta($user_id, 'billing_last_name', $lastname);
    	update_user_meta($user_id, 'shipping_last_name', $lastname);
    	update_user_meta($user_id, 'billing_phone', $phone);
    }

    and it works for me….

    I want to ask one more things how to show mobile number on user dashboard?

    Hi guys,

    Glad this helped!

    @kshilpa1385 you might want to post a new thread for your question about displaying user data on the dashboard.

    best,
    -jennyb

    Roy Ho

    (@splashingpixelscom)

    Also be sure you’re sanitizing the user input.

    Something like this:

    update_user_meta( $user_id, 'first_name', sanitize_text_field( $firstname ) );
    Shilpa

    (@kshilpa1385)

    Thanks Roy Ho…

    you means i used like:

    //Adding Registration fields to the form 
    
    add_action( '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;
    	$firstname = empty( $_POST['firstname'] ) ? '' : $_POST['firstname'];
    	$lastname  = empty( $_POST['lastname'] ) ? '' : $_POST['lastname'];
    	$phone = empty( $_POST['phone'] ) ? '' : $_POST['phone'];
    	?>
    	<div class="form-row form-row-wide">
    		<label for="reg_firstname"><?php _e( 'First Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="<?php echo esc_attr( $firstname ) ?>" />
    	</div>
    	<div class="form-row form-row-wide">
    		<label for="reg_lastname"><?php _e( 'Last Name', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="lastname" id="reg_lastname" size="30" value="<?php echo esc_attr( $lastname ) ?>" />
    	</div>
    	<div class="form-row form-row-wide">
    		<label for="reg_phone"><?php _e( 'Phone', 'woocommerce' ) ?><span class="required">*</span></label>
    		<input type="text" class="input-text" name="phone" id="reg_phone" size="10" value="<?php echo esc_attr( $phone ) ?>" />
    	</div><?php
    }
    
    //Validation registration form  after submission using the filter registration_errors
    add_filter( 'woocommerce_registration_errors', 'registration_errors_validation' );
    
    /**
     * @param WP_Error $reg_errors
     *
     * @return WP_Error
     */
    function registration_errors_validation( $reg_errors ) {
    
    	if ( empty( $_POST['firstname'] ) || empty( $_POST['lastname'] ) || empty( $_POST['phone']) ) {
    		$reg_errors->add( 'empty required fields', __( 'Please fill in 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', sanitize_text_field( $firstname ));
    	update_user_meta($user_id, 'last_name', sanitize_text_field( $lastname ));
    	update_user_meta($user_id, 'phone', sanitize_text_field( $phone ));
    	update_user_meta($user_id, 'billing_first_name', sanitize_text_field( $firstname ));
    	update_user_meta($user_id, 'shipping_first_name', sanitize_text_field( $firstname ));
    	update_user_meta($user_id, 'billing_last_name', sanitize_text_field( $lastname ));
    	update_user_meta($user_id, 'shipping_last_name', sanitize_text_field( $lastname ));
    	update_user_meta($user_id, 'billing_phone', sanitize_text_field( $phone ));
    }

    Please tell me above syntax is correct or not?

    Hello,

    Thanks a lot @jenny, This save my lot of time.

    Can please explain me how to add select-box like country or city in thr registration form ???

    Roy Ho

    (@splashingpixelscom)

    @kshilpa1385 – yes that is correct. See this for more info on that function http://codex.wordpress.org/Function_Reference/sanitize_text_field

    Shilpa

    (@kshilpa1385)

    Thanks a lot Roy.

    I am seriously confused here. Please help… 🙂

    @jennybeaumont – You said: The following code pasted into your theme will add both a First Name and a Last Name field to the registration form…

    What do you mean by “pasted into your theme?” How do you paste something into your theme? Dashboard > Appearance > Themes > then what?

    @roothost – You said: I just removed them from form-login.php (obviously in my theme folder, not the core WP plugin)…

    What do you mean by “form-login.php (obviously in my theme folder)?

    I was under the impression that modifications like this would either be done on the files in your File Manager or under Dashboard > Plugins > Editor > Select plugin to edit: WooCommerce > then you have to find:

    woocommerce/templates/myaccount/form-login.php

    Sorry to sound like I’m clueless, but I’m totally lost here…

    Cheers!

    Hi Cmallon –

    For my bit, the function I posted should be pasted into the functions.php file of your theme. You should be able to access it via your admin editor. You can pretty much put it anywhere – at the end might be easiest.

    For further modifications to the login template (or other wc templates) you might start by studying the woocommerce template structure : http://docs.woothemes.com/document/template-structure/

    To override wc templates, you need to place them in your theme…the best way to get them their is via ftp…then you could eventually edit them with your admin editor (but easier via text/html editor).

    Hope this helps!
    best,
    -jennyb

    Jenny has pretty much covered everything above, but one thing I will say is that you should never edit a file within a plugin.

    Whilst it may work, and achieve what you intended, what happens when you update that plugin to the latest version? That’s right, it overwrites any custom edits you may have made.

    There are always various hooks you can use to override plugin functions, which can be called via functions.php and the link above gives you all you need to know to copy the templates files over in to your theme.

    +1 never modify plugin files!
    +1 there are always other ways (in case of doubt, hire someone :))

    WOW!! Holy response time! Amazing you guys and thank you!!!

    @jenny – When you said “To override wc templates, you need to place them in your theme…”

    Do you mean move them via ftp from the plugin into the theme or just copy the template file and paste it into the theme so that you’re editing a duplicate? I would imagine you mean copy and use a duplicate but I could be wrong…

    @roothost – Understood and thanks for the further clarification.

    You guys rock!!

    yes, make a copy of any templates you want to modify (located in the woocommerce plugin template folder) into your template in a folder you will name woocommerce (respecting of course any sub-folders). Go ahead and read that documentation link I posted, should be pretty clear. DO NOT delete the templates from the woocommerce plugin. and you only need to copy templates you want to override. if it exists in your theme, it will then override the wc defaults.

    clear? 🙂

    best,
    -jennyb

    Very. Thanks again!! 🙂

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Add fields to woocommerce registration form : follow up’ is closed to new replies.