Sync WordPress and Woocommerce user profile

I never understood why woocommerce developers decided not to sync name and email address for billing and shipping to the wordpress user meta. Also, ‘The profile owner and the person who is placing the order may not be the same’ should not be considered as a feature. Retaining outdated contact details on the checkout page will also lead to user’s confusion and frustration.

So here’s the solution!

Following code snippet will update First Name, Last Name and Email address for billing and shipping contact details when any user updates his/her profile. Tested with WordPress 3.8 and Woocommerce 2.1.

Just append this code in ‘Functions.php’ file in your theme’s directory.

<?php
// Auto Update Woocommerce billing and shipping name,email on profile update
add_filter( 'profile_update' , 'custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
  $current_user = wp_get_current_user();

  // Updating Billing info
  if($current_user->user_firstname != $current_user->billing_first_name)
    update_user_meta($user_id, 'billing_first_name', $current_user->user_firstname);
  if($current_user->user_lastname != $current_user->billing_last_name)
    update_user_meta($user_id, 'billing_last_name', $current_user->user_lastname);
  if($current_user->user_email != $current_user->billing_email)
    update_user_meta($user_id, 'billing_email', $current_user->user_email);
 
  // Updating Shipping info
  if($current_user->user_firstname != $current_user->shipping_first_name)
    update_user_meta($user_id, 'shipping_first_name', $current_user->user_firstname);
  if($current_user->user_lastname != $current_user->shipping_last_name)
    update_user_meta($user_id, 'shipping_last_name', $current_user->user_lastname);
  if($current_user->user_email != $current_user->shipping_email)
    update_user_meta($user_id, 'shipping_email', $current_user->user_email);
}
?>
Advertisements

14 thoughts on “Sync WordPress and Woocommerce user profile

  1. That’s great man! Thank you for this.
    Other than first name, second name and email, can we update the phone, address as well (assuming I have custom user field address)? Is it possible to make the data sync if user update their billing/shipping details (from woocommerce edit account) as well?

  2. I have recently implemented this on WP 4.3.1 with Woo 2.4.10.

    Excellent work on putting this together. My client’s store has shared WP user accounts so we don’t want the customer order data to overwrite the WP User info. This snippet was the ticket.

    btw Sean….The company would be referenced with: ” update_user_meta($user_id, ‘billing_company’ , ‘ ‘);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s