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);
}
?>