• 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://www.ads-software.com/plugins/woocommerce/

Viewing 9 replies - 16 through 24 (of 24 total)
  • Thanks Jenny!

    This has been terrific. I rarely edit code but this worked perfectly. I have another question. Is it possible to add a new field I created in the register process (Account #) as part of the Login process?

    My client wishes their customers to sign in using their account #.

    Thanks again!
    ~ Gary

    Thread Starter Jenny Beaumont

    (@jennybeaumont)

    Hi Gary,

    I’m not aware of a way to modify the login process (have never looked into it). I suppose you could reword the language around the username/login field so that it becomes the account number (tho beware that people are prone to mistyping if they are the ones to register it in the first place). But if you want to add this as a 3rd piece of information, so username, account name and password required, you might want to search elsewhere and/or start a new thread. ??

    Sorry I couldn’t be of more help!

    cheers,
    -jennyb

    https://support.woothemes.com/hc/en-us/articles/203182373-How-to-add-custom-fields-in-user-registration-on-the-My-Account-page

    Look this link, better.

    /**
     * Add new register fields for WooCommerce registration.
     *
     * @return string Register fields HTML.
     */
    function wooc_extra_register_fields() {
    	?>
    
    	<p class="form-row form-row-first">
    	<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
    	<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
    	</p>
    
    	<p class="form-row form-row-last">
    	<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
    	<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
    	</p>
    
    	<div class="clear"></div>
    
    	<p class="form-row form-row-wide">
    	<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
    	<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
    	</p>
    
    	<?php
    }
    
    add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

    if you dont want to mess with code , check out Cimy user extra fields plugin. worked perfectly for me

    Hello, thank you for the code. It really helps ??

    I’m customizing Woocommerce registration field with your code and I need to add country, state, and city to it (your code only explains how to add text field). How do I add the state, country, and city field (in the billing) to the registration page?

    Thread Starter Jenny Beaumont

    (@jennybeaumont)

    Hi there rpabowo,

    The same principal would apply for any new field you want to add. It’s starts with identifying the the name of the field as defined in WooCommerce – I don’t know these all off hand, you’ll have to go look them up. Once you know the name of the field you need, add a row with label & input for the form itself, then make sure to add the line to update_user_meta so that the info is saved. Also note that WooCommerce keeps separate info for billing & shipping…

    voilà, hope that helps!
    best,
    -jennyb

    ranjith multi_thoughts

    (@ranjith-multi_thoughts)

    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);
    }
    please tel me where to paste this code and in which file please tel me its urgent……..

    Thread Starter Jenny Beaumont

    (@jennybeaumont)

    Functions should be written/pasted into the functions.php file of your theme.

    I’m not a PHP programmer, but If there is anyone who can help me with the code (that I can just copy paste to my function.php or it’s okay to modify the core) to display the Select field of Billing country, city, and state to registration page, I appreciate it ??

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