Home » WooCommerce: Sync Billing Name & WP User Name

WooCommerce: Sync Billing Name & WP User Name

by Tutor Aspire

When someone places an order via the WooCommerce checkout, there is a function (process_customer) that saves the checkout first & last name to the customer Billing address (WooCommerce). The same function also overwrites that same WP user first & last name (WordPress).

The same happens when someone updates the Billing address via the My Account edit address tab. There is a WooCommerce function (save_address) that copies Billing first & last name to the WP user first & last name.

You’d think that was sufficient to keep billing names and WP user names in sync – well, nope. You can also update billing first & last name from the user edit profile page (WP dashboard). In such case, WP user first & last name is NOT updated, and billing and user names are not in sync.

Today, we’ll study some code to make that happen, so that you never have to worry again about inconsistencies. Enjoy!

Let’s suppose my name is Lawrence Melogli. WP User and Billing User are in sync. But if I go edit the Billing address via my WP User Profile page, and enter “Tutoraspire” as first name, the WP User remains unchanged. In this article, we’ll fix that.

PHP Snippet: Sync Billing First & Last Name With User First & Last Name @ WP Edit User Profile

/**
 * @snippet       Copy Billing to User @ WP User Edit Page
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'pre_user_first_name', 'tutoraspire_sync_user_edit_profile_edit_billing_first_name' );

function tutoraspire_sync_user_edit_profile_edit_billing_first_name( $first_name ) {
    if ( isset( $_POST['billing_first_name'] ) ) {
        $first_name = $_POST['billing_first_name'];
    }
    return $first_name;
}

add_filter( 'pre_user_last_name', 'tutoraspire_sync_user_edit_profile_edit_billing_last_name' );

function tutoraspire_sync_user_edit_profile_edit_billing_last_name( $last_name ) {
    if ( isset( $_POST['billing_last_name'] ) ) {
        $last_name = $_POST['billing_last_name'];
    }
    return $last_name;
}

You may also like