Linz Darlington
Linz Darlington

Reputation: 671

Updating the WooCommerce 'User Firstname Field' with 'Billing First Name' on check out

I'm currently using WooCommerce.

To make life easy for my customers I've got rid of the 'First Name' and 'Last Name' fields on the Account Registration. I've just got the email address.

However, when the customer checks out I'd like to populate the 'user_firstname' with the 'billing_first_name' value in their user meta.

I'm trying the following code but doesn't seem to work - any ideas?

// Auto Update User Details

add_filter( 'process_checkout' , '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->billing_first_name != $current_user->user_firstname)
    update_user_meta($user_id, 'user_firstname', $current_user->billing_first_name);
}

Cheers,

Linz

Upvotes: 0

Views: 6197

Answers (1)

Linz Darlington
Linz Darlington

Reputation: 671

Ok, so I found a good solution:

add_action('woocommerce_checkout_order_processed', 'custom_process_order1', 10, 1);
function custom_process_order1($order_id) {
    $current_user = wp_get_current_user();
    $current_user_id = get_current_user_id();

    update_user_meta($current_user_id, "first_name", $current_user->billing_first_name);
    update_user_meta($current_user_id, "last_name", $current_user->billing_last_name);
}

I'm a bit of a newb, so help others out.

  • The 'add_action' bit hooks this code into the check out process
  • The $current_user defines the term
  • You'll notice the wp_get_current_user() and get_current_user_id() are a bit different but sort of the same. I think this is because one is Wordpress and the other WooCommerce
  • Then I simply say replace the "first name" field with the "billing_first_name" field.

Hope that helps any readers.

Upvotes: 1

Related Questions