Business Bloomer
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0

WooCommerce: Add First & Last Name to My Account Register Form

> Published: Mar 2017 - Revised: Mar 2020
> Blog Category: WooCommerce Tips
> Blog Tags: My Account
> Blog Comments: 91 Comments
Tweet

Join 17,000+ WooWeekly subscribers

Here’s another useful PHP snippet that adds the Billing First Name and Billing Last Name to the Registration Form on the WooCommerce My Account page.

This is a great first step. If you learn to add these simple text fields, you can then add any custom input field to the form such as dropdowns, radio buttons, checkboxes – and link these to the relevant WooCommerce/WordPress user fields. Enjoy!

Add name fields to the WooCommerce Registration form

PHP Snippet: Add First & Last Name to WooCommerce My Account Registration Form

/**
 * @snippet       Add First & Last Name to My Account Register Form - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WC 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
///////////////////////////////
// 1. ADD FIELDS
 
add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
 
function bbloomer_add_name_woo_account_registration() {
    ?>
 
    <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>
 
    <?php
}
 
///////////////////////////////
// 2. VALIDATE FIELDS
 
add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
 
function bbloomer_validate_name_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
        $errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
    }
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
        $errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
    }
    return $errors;
}
 
///////////////////////////////
// 3. SAVE FIELDS
 
add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
 
function bbloomer_save_name_fields( $customer_id ) {
    if ( isset( $_POST['billing_first_name'] ) ) {
        update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
        update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
    }
    if ( isset( $_POST['billing_last_name'] ) ) {
        update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
        update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) );
    }
 
}

Related posts:

  1. WooCommerce: Add New Tab @ My Account Page
  2. WooCommerce: Change User Role for New Customers
  3. WooCommerce: Add Select Field to “My Account” Register Form
  4. WooCommerce: Add Privacy Policy Consent @ My Account Registration
  5. WooCommerce: Separate Login, Registration, My Account Pages
  6. WooCommerce: Allow Users to Edit Processing Orders
  7. WooCommerce: Deny Automatic Login Upon Registration @ My Account
  8. WooCommerce: File Upload @ My Account Registration Form
  9. WooCommerce: Rename “My Account” If Logged Out @ Nav Menu
  10. WooCommerce: Add a Custom Download File @ My Account

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Follow @rmelogli
Category: WooCommerce Tips
Tag: My Account

Post navigation

Previous post: WooCommerce: Update User Meta After a Successful Order
Next post: WooCommerce: Change User Role for New Customers

91 thoughts on “WooCommerce: Add First & Last Name to My Account Register Form”

  1. Kelvin
    October 23, 2022

    I have a custom post to be created when a new user registers, I tested this snippet, and it works, but how can I fire the form update from Account Details? I want to update the custom post’s title with the new updated customer’s first and last name.

    function ebz_new_member( $user_id ) {
        $user_info = get_userdata($user_id);
        if (!post_exists($user_info->user_login,'','','member')) {
            $first_name = isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : $user_info->first_name;
            $last_name = (isset($_POST['billing_last_name'])) ? $_POST['billing_last_name'] : $user_info->last_name;
            $phone_contact = $_POST['phone_contact'];
            $gender = $_POST['gender'];
            $post = array(
                'post_title'    => sprintf("%s %s", $first_name, $last_name),
                'post_type'     => 'member',
                'post_status'   => 'publish',
                'post_author'   => $user_id, 
                'meta_input' => array( 
                    "wpcf-contact" => $phone_contact,
                    "wpcf-gender" => $gender)
            );
            $post_id = wp_insert_post($post);
        }
    }
    add_action( 'user_register', 'ebz_new_member', 10, 10 );
    Reply
    1. Rodolfo Melogli
      October 26, 2022

      Hi Kelvin, you can use the hook “woocommerce_customer_save_address”:

      do_action( 'woocommerce_customer_save_address', $user_id, $load_address );
      Reply
  2. Kelvin
    October 23, 2022

    How can I add my custom field between two default Woocommerce fields?

    Kelvin

    Reply
    1. Rodolfo Melogli
      October 26, 2022

      Not be possible with PHP alone – CSS or possibly JS may be needed to move them before/after the field you like

      Reply
  3. Tay
    June 8, 2022

    Yes, i have added it and it works just fine.. Thanks for the contribution

    Reply
    1. Rodolfo Melogli
      June 13, 2022

      Awesome

      Reply
  4. Brad
    March 9, 2022

    This works brilliantly, thank you. Does anyone know how to add alphabet validation (disallow special characters) to this code? Then it would be perfect! Thanks again for sharing.

    Reply
    1. Rodolfo Melogli
      March 25, 2022

      Hi Brad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  5. Jonathan Cheseaux
    December 7, 2020

    Awesome shortcode! Works great – just have a issue with one thing:
    Is it possible to redirect the user if logged in to the my account page? If yes, how shall I do it?

    Reply
    1. Rodolfo Melogli
      December 16, 2020

      Hi Jonathan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  6. Earth
    December 1, 2020

    Hi Rodolfo,

    This is the snippet I was looking for. It works great.
    But I was wondering how can I combine it with the Separate WooCommerce Customer Registration shortcode?(https://www.businessbloomer.com/woocommerce-separate-login-registration/) I’ve been having problem in combining the 2 together.

    Reply
    1. Rodolfo Melogli
      December 2, 2020

      Hello Earth, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  7. Debs
    November 16, 2020

    This is exactly what I wanted for the front end, but now I can not add users via the dashboard…I am getting the following error:

    Cannot create a user with an empty login name.

    How do I still add people via the Dashbaord?

    Reply
    1. Rodolfo Melogli
      November 25, 2020

      That’s weird. Sure if you remove the customization this goes back to normal?

      Reply
  8. Igor
    November 12, 2020

    Hello Rodolfo,

    Thx for your solution, I added the name, last name, pass , confirm pass.
    And now I have a structure like this: name, last name, pass , confirm pass, email.
    How can I do to be like this: name, last name, email, pass , confirm pass ?
    Thx!

    Reply
    1. Rodolfo Melogli
      November 25, 2020

      Hi Igor, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  9. Andrew Watson
    September 14, 2020

    Hi Rodolfo

    I used this on Kadence Ascend child theme – worked first time.
    It would be useful if Woocommerce would address the new account registration email(and others) to the users first/Last name rather then the username which would add a touch of professionalism – don’t you think?

    WP 5.5.1 running Kadence Ascend Premium with Ascend Child Theme.

    Reply
    1. Rodolfo Melogli
      September 21, 2020

      I agree

      Reply
  10. Stylianos Charalampous
    July 4, 2020

    Tried this with rehub theme but it didnt make any changes to the login popup.

    Reply
    1. Rodolfo Melogli
      July 7, 2020

      Stylianos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  11. Enrique Guardia
    June 7, 2020

    Just tried it out on WC 4.1 and still works like a charm! Thanks for the snippet

    Reply
    1. Rodolfo Melogli
      June 15, 2020

      Great!

      Reply
  12. Stylianos
    May 20, 2020

    I’ve tried to override the login_form from template (copied from plugins to themes) through cPanel but still does not work. Does my wp version 4.1.0 matters regarding this specific snippet?

    Thanks again.

    Reply
    1. Rodolfo Melogli
      May 26, 2020

      I don’t recommend to copy templates, sorry

      Reply
  13. Stylianos
    May 20, 2020

    Hey Rodolfo,

    Thanks for the free info you provide to us.

    I am not a developer, I paste this to the correct file using a child theme. All i get is this,

    ”Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”

    Do i need to change something on the code? Maybe replace some fields with my site name?

    Thanks again.

    Reply
    1. Rodolfo Melogli
      May 26, 2020

      Don’t use the WordPress editor – use FTP instead

      Reply
  14. Maram
    April 1, 2020

    Thanks Man, Work like a charm

    Reply
    1. Rodolfo Melogli
      April 2, 2020

      Cool!

      Reply
  15. Markos
    March 9, 2020

    Hi,
    Is it possible to set maximum field length for the first and last name?

    Reply
    1. Rodolfo Melogli
      March 10, 2020

      Hi Markos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  16. nara
    February 23, 2020

    Your code seems to work well except when I found that there is no First and Last name recorded in my users data. Please suggest.

    Reply
    1. Rodolfo Melogli
      February 27, 2020

      Hi Nara, did you change anything in my snippet?

      Reply
      1. nara
        February 28, 2020

        No. I copied them all. I noticed that you have updated both first_name and billing_first_name with the name acquired but my finding is that only billing_first_name is updated.

        Reply
        1. Rodolfo Melogli
          March 2, 2020

          Uhm, honestly it should work. I just tested it again and it worked indeed

          Reply
          1. nara
            March 23, 2020

            Update. I found an error in wordpress log as following:
            https://prnt.sc/rkwram
            and line 51 is
            https://prnt.sc/rkwrw4

            It’s exactly as you code. What do you think ?

            Reply
            1. Rodolfo Melogli
              March 27, 2020

              No idea – snippet is fine I’m afraid

              Reply
  17. Jose
    February 17, 2020

    Hello thank you for the code, just a question if I want to add another data for example the ID and show on the page my account. How could I do that?

    Reply
    1. Rodolfo Melogli
      February 22, 2020

      Hi Jose, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  18. Santiago
    December 26, 2019

    Excellent code! The script worked well, except for the validation. The fields validate, but does not return errors.

    Reply
    1. Rodolfo Melogli
      January 6, 2020

      Uhm, it should work. Try with no plugins but Woo, and 2020 theme – does it work?

      Reply
  19. Elkin
    December 2, 2019

    Hello, great code, thanks a lot. If I’d like to use those fields in the confirmation email to the customer as the salutation, instead of the user name that’s being used right now… I thought about just replacing with billing_first_name but sadly enough that’s not working.

    Can you help this noobie a bit with the correct line?

    Reply
    1. Rodolfo Melogli
      December 3, 2019

      Hi Elkin, did you mean this: https://businessbloomer.com/woocommerce-add-billing-first_name-to-email-receipt/

      Reply
  20. Ahmed Thahir
    October 14, 2019

    Is this working with Dokan? I have problem with that

    Reply
    1. Rodolfo Melogli
      October 22, 2019

      Not sure, ask Dokan

      Reply
  21. Firat
    September 29, 2019

    Hello Rodolfo,

    Thanks for all the codes you share! They are making my website coming along. After adding name and surname, is it possible to create the username automatically? Like the first letter of name and the surname.

    Reply
    1. Rodolfo Melogli
      September 30, 2019

      Hello Firat, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  22. Jon
    September 26, 2019

    Working on my site and thanks for the great tutorials!

    I have a feeling this is going to start a long road of learning WordPress customizing for myself!

    Most recent version of WordPress (as of 09-25-2019) using a third party parent theme, Soledad.

    Reply
    1. Rodolfo Melogli
      September 30, 2019

      Excellent!

      Reply
  23. stef
    August 28, 2019

    Worked like a charm! Great job Rodolfo! Thank you. Thoughts on making this its own file and calling to it from the functions file? Only because eventually our functions file is going to be a mess of hooks and snippets. Would love to know from a security stand point too.

    Thanks again

    Reply
    1. Rodolfo Melogli
      August 29, 2019

      Cool! If you really want, you can turn each snippet into a standalone plugin: https://codex.wordpress.org/Writing_a_Plugin#Creating_a_Plugin

      Reply
  24. Sonny
    June 26, 2019

    Hi Rodolfo,

    I just used your code and it works wonderfully. However, first and last name are on separate line, where with your example, it is on the same line. What could be the reason.

    Reply
    1. Rodolfo Melogli
      July 6, 2019

      Maybe just some CSS that needs to be customized 🙂

      Reply
  25. Nitin Shah
    May 29, 2019

    Thank you for this code. It is working on the latest wordpress and woocommerce versions

    Reply
    1. Rodolfo Melogli
      June 6, 2019

      Awesome!

      Reply
  26. Emiliano
    March 14, 2019

    Ciao e complimenti, ho inserito il codice e tutto funziona bene.
    Vorrei chiederti se fosse possibile, inserire anche il campo Telefono e se puoi darmi indicazioni in tal senso.
    Ti ringrazio anticipatamente e attendo un tuo gentile riscontro.
    Un saluto.
    Emi

    Reply
    1. Rodolfo Melogli
      March 15, 2019

      Ciao Emiliano, yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

      Reply
  27. John Chandler
    March 2, 2019

    Beautiful idea. I hate when the name is blank. Or when it is set to a part of the email address.

    I found that it’s good to also save the name into the user’s display_name too.

    wp_update_user (array( ‘ID’ => $customer_id, ‘display_name’ => $_POST[‘billing_first_name’] . ‘ ‘ . $_POST[‘billing_last_name’] ) );

    Reply
    1. Rodolfo Melogli
      March 7, 2019

      Thank you!

      Reply
    2. luan
      August 30, 2019

      Please help

      In my country it all the way around Last Name come first then “First name”. So can you guys help me with the code to

      1. Reoder first name and last name field all the way around.

      2. Put Last name and first name as the Display name automatically.

      Thank you so much for reading.

      Reply
  28. John
    February 8, 2019

    Hi,

    This is a great overview/snippet for woocommerce my account page. After reviewing the code, I wanted to ask if there isa way to add address information to the My Account form and also to the wcpv_registration form? if so, could you please explain?

    Thank you in advance.

    Reply
    1. Rodolfo Melogli
      February 14, 2019

      Hello John, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

      Reply
  29. Keerthan
    December 13, 2018

    Thanks, Rodolfo for the code,

    Is there any way to make the last name optional in this?

    Reply
    1. Rodolfo Melogli
      December 18, 2018

      Hey Keerthan, thanks for your comment! Yes, sure, just remove the validation for that field and also the “required” span 🙂

      Reply
  30. Brian
    November 27, 2018

    Hi Rodolfo, I am using this snippet and its working just great. However, I am also using another snippet of yours to add text to the registration screen (https://businessbloomer.com/woocommerce-show-extra-content-my-account-page/) but the first name and last name fields show above the extra text as opposed to below it.

    Any help would be very much appreciated.

    Reply
    1. Rodolfo Melogli
      December 5, 2018

      Hey Brian, thanks for your comment 🙂 Just add the priority e.g. “15”:

      add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration', 15 );
      
      Reply
  31. robin
    September 29, 2018

    I added the code provided by Luuk and the names still don’t stick in the user interface unless the customer inputs their name again. Any additional suggestions would be very appreciated.

    Reply
    1. Rodolfo Melogli
      October 2, 2018

      Robin, thanks a mill for your comment. Not sure about Luuk’s code, unfortunately I cannot troubleshoot it for you here via the blog comments. Thanks for your understanding, R

      Reply
  32. Thomas
    September 29, 2018

    Hey Rodolfo, It works! Thanks for the code. No plugin required and very easy to implement (if you know little bit of coding). Also name and surname is visible in WordPress database.

    Reply
    1. Rodolfo Melogli
      October 2, 2018

      Nice 🙂

      Reply
  33. ajay jain
    March 25, 2018

    but getting error when debug is true

    Undefined variable: options in C:\Program Files (x86)\Ampps\www\wp\wp-content\themes\themename\includes\woocommerce.php on line 270

    Reply
    1. Rodolfo Melogli
      March 30, 2018

      Hey there, thanks for your comment! I don’t think my snippet is causing that – either your theme needs updating or there is another PHP conflict. Try with another theme and let me know if the snippet works 🙂

      Reply
    2. ajay jain
      April 25, 2018

      Its working fine, but if we enable debug is true then showing error, using php 7.1

      Reply
      1. Rodolfo Melogli
        April 26, 2018

        Ok thank you! What PHP error do you get?

        Reply
    3. ajay jain
      April 30, 2018

      Notice: Undefined variable: options in C:\Program Files (x86)\Ampps\www\wp\wp-content\themes\xxx\includes\woocommerce.php on line 312

      Reply
      1. Rodolfo Melogli
        May 3, 2018

        I don’t see any $options variable in my code, so it must be something else 🙂

        Reply
  34. Daniel B
    January 30, 2018

    Thanks so much for the great snippet Rodolfo & Luuk! So helpfull! 🙂
    I am not a developer AT ALL so please forgive my lack in dev lingo and code structure. 🙂

    I was looking for a way to also add the “company name” to the registration form. (billing_company according to https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ )
    So first name, last name & Company name in top of the registration form.
    And also display this saved data when logged in as a customer and viewing my billing- and account info.

    I have tested the snipped and also added the company field & LUUK´s two extra lines to save first- and lastname. Code below.

    Ive tested it on a demopage with WC version 3.2.6 and WP-version 4.9.2 and it seems to be working….but again… Im no developer, so I probably missed something.

    Grateful for feedback. 🙂

    Thanks again all of you!

    /**
     * @snippet Add First, Last Name & Company name to My Account Register Form - WooCommerce
     */
    
    ///////////////////////////////
    // 1. ADD FIELDS
    
    add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
    
    function bbloomer_add_name_woo_account_registration() {
        ?>
    
        <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>
    
        <?php /*Company field*/    ?>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
        </p>
    
        <div class="clear"></div>
    
        <?php
    }
    
    ///////////////////////////////
    // 2. VALIDATE FIELDS
    
    add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
    
    function bbloomer_validate_name_fields( $errors, $username, $email ) {
        if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
            $errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: Fyll i förnamn!', 'woocommerce' ) );
        }
        if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
            $errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Fyll i efternamn!.', 'woocommerce' ) );
        }
        /*Validate company field*/
        if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
            $errors->add( 'billing_company_error', __( '<strong>Error</strong>: Fyll i företagsnamn!.', 'woocommerce' ) );
        }
    
        return $errors;
    }
    
    ///////////////////////////////
    // 3. SAVE FIELDS
    
    add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
    /*Adding LUUK code to save first and last name in WOO & WP*/
    function bbloomer_save_name_fields( $customer_id ) {
        if ( isset( $_POST['billing_first_name'] ) ) {
            update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
            update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
        }
        if ( isset( $_POST['billing_last_name'] ) ) {
            update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
            update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
        }
        /*Saving company field*/
        if ( isset( $_POST['billing_company'] ) ) {
            update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
        }
    
    }
    
    Reply
  35. Adnan
    November 11, 2017

    Hello Mike,

    Did you find the way that how to sync these two fields of First Name & Last Name on Billing Details & Account Details? Or is there anyway, if we hide the fields on Account Details tab and whatever information will be stored in the billing details the same goes to my account fields? Thanks

    Reply
  36. Ryan
    October 24, 2017

    Sorry couldn’t get it to work. In the admin under users first and last name are still blank. Fields only show on front end but the data input is not collected and stored.

    Reply
    1. Rodolfo Melogli
      October 25, 2017

      Hey Ryan, thanks for your comment! I just tested again and it works – please note “First” and “Last name” are the ones in the Customer Billing section (scroll down in the user page), and not the standard WordPress fields. Let me know 🙂

      Reply
      1. sebastian
        July 23, 2020

        Hi I have seen that there are other person with this problem. The first and last name is not show in the user page (in any fields). Maybe it’s about the language? What test can I do? I have +100 users and I don’t have any first and last name

        Thanks

        Reply
        1. Rodolfo Melogli
          July 28, 2020

          Already tried with 2020 theme and no other plugins but Woo?

          Reply
  37. Ravi
    July 4, 2017

    Hi there.

    I’ve tried to use the same code and the fields are getting displayed. However, the first name and last name are not saving and not displaying for customer until he/she filled the same again in /my-account/edit-account/ section. What could be the cause for this? WooCommerce version 3.10.

    Reply
    1. Rodolfo Melogli
      July 6, 2017

      Hey Ravi, thanks for your comment! Did you change anything in the snippet?

      Reply
    2. Sara
      August 9, 2017

      Same problem here… i do not make changes to the snippet. Can you help please?

      Reply
    3. Tom
      October 20, 2017

      You need to change the variables in template form-edit-account.php from “first_name” to “billing_first_name”

      Reply
    4. Mike
      November 2, 2017

      Same problem here. @tom are you saying the name fields in the account edit page are for the WordPress profile name and not the woocommerce billing firstname and lastname? Cheers!

      Reply
    5. Mike
      November 2, 2017

      Ah yes, Tom is correct.. So it is saving the name but you dont see it on the edit account page, only in the cart. I remember this issue from a while back but I’m fairly new to woocom again and assumed this wouldn’t still be an issue years and years later.. so there are two first name and last name fields per user.. what a strange way to do it.. anyways. I just need to figure out how to sync those fields now.

      Reply
      1. Rodolfo Melogli
        November 2, 2017

        🙂

        Reply
    6. Juan
      November 18, 2017

      @Tom if you do this “form-edit-account.php from “first_name” to “billing_first_name””, the first time you go to /my-account/edit-account/ page is ok but if you modify first_name or last_name, saves changes and refresh this page, you won’t see your modification (because this form modifies firt_name and last_name not billing_first_name and billing_last_name which are the fields what this form use to auto prefill) Can anybody help us to fix this???

      Reply
    7. Luuk
      January 4, 2018

      Hi all,

      Rodolfo, thank you for this snippet!

      To have the entered first and last names also show up in the My Account page, I added two lines in the saving section. Now the names are saved as billing names as well as ‘normal’ (WordPress) names. Below my edited version of the function used at //3. SAVE FIELDS. 🙂

      function bbloomer_save_name_fields( $customer_id ) {
      	if ( isset( $_POST['billing_first_name'] ) ) {
      		update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      		update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      	}
      	if ( isset( $_POST['billing_last_name'] ) ) {
      		update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      		update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      	}
      }
      
      Reply
      1. Rodolfo Melogli
        January 9, 2018

        Excellent 🙂

        Reply
Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
  • WooCommerce: Capitalize All Product Names
  • WooCommerce: Add a Third Description @ Single Product Page
About Business Bloomer

With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.

Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.

Join 75,000+ Monthly Readers & 16,500+ Subscribers.

Become a Business Bloomer Supporter.

Join BloomerArmada and become an Official Business Bloomer Supporter:
easy-peasy, and lots of perks for you.
See your Benefits →
Popular Searches: Visual Hook Guides - Checkout Page - Cart Page - Single Product Page - Add to Cart - Emails - Shipping - Prices - Hosting
Latest Articles
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
  • WooCommerce: Capitalize All Product Names
  • WooCommerce: Add a Third Description @ Single Product Page
Latest Comments
  • Rodolfo Melogli on WooCommerce: Separate Login, Registration, My Account Pages
  • Rodolfo Melogli on WooCommerce: Separate Login, Registration, My Account Pages
  • Rodolfo Melogli on WooCommerce: Related Products Custom Heading & Subheading
  • Rodolfo Melogli on WooCommerce: Switch Shop Columns Responsively
Find Out More
  • Become a WooCommerce Expert
  • WooCommerce Blog
  • WooCommerce Online Courses
  • WooCommerce Weekly
  • Bloomer Armada
  • Affiliate Program
  • Contact
Contact Info

Ciao! I’m Rodolfo Melogli, an Italian Civil Engineer who has turned into an international WooCommerce expert. You can contact me here:

Email: [email protected]

Twitter: @rmelogli

Hire me by the hour: Get Quote »

VisaMastercardAmexPayPal Acceptance Mark
Business Bloomer © 2011-2023 - Terms of Use - Privacy Policy