0

I'm setting up a silent auction to use at a fundraiser. All items are being picked up at the event, so I'm trying to get around having Billing and Shipping addresses, but I don't think I can do that. Seems that Wordpress and WooCommerce are trying to force the user into filling out those boxes during checkout. If I could auto populate the fields that are required, that would probably work for what i need.

So, as a work around, I'm trying to take the users First, Last, Email and Phone from their registration and just copy them to both the Billing and Shipping fields in the user's account.

I tried this code that I found searching Google;

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

But, from what I can tell, this is only set to fire when a user updates their own profile. Running it as I created a user account didn't produce the results I was looking for.

I'm ok with PHP, but not with WordPress hooks.

Can someone take a quick peek at my code and point me in the right direction? Or, if there is a better way to accomplish what I am trying to do, that would be great too!

0

But, from what I can tell, this is only set to fire when a user updates their own profile.

Yes, that's correct.

Running it as I created a user account didn't produce the results I was looking for.

The hook you should use when the user is being created/registered, is user_register.

So you would use this, where you hook to both profile_update and user_register:

// Note that you should use add_action() and not add_filter()
add_action( 'profile_update', 'custom_update_checkout_fields' );
add_action( 'user_register', 'custom_update_checkout_fields' );

And since the second hook (user_register) only provides one parameter, then just omit the $old_user_data from the function; and secondly, you should use get_userdata() and not wp_get_current_user() to get the user being updated or created.

The full code I used:

add_action( 'profile_update', 'custom_update_checkout_fields' ); // Fires immediately after an existing user is updated.
add_action( 'user_register', 'custom_update_checkout_fields' );  // Fires immediately after a new user is registered.
function custom_update_checkout_fields( $user_id ) {
    $current_user = get_userdata( $user_id ); // Here we use get_userdata() and not 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 );
}

Note though, the user_register hook wouldn't be called if the customer is anonymous and not allowed to create a WordPress/user account.

Additional Code

You can use the woocommerce_checkout_get_value hook to auto-fill the form data, although WooCommerce actually does the auto-fill if the metadata exist.

add_filter( 'woocommerce_checkout_get_value', 'custom_autofill_customer_data', 10, 2 );
function custom_autofill_customer_data( $value, $input ) {
    $current_user = wp_get_current_user();
    if ( $current_user ) {
        switch ( $input ) {
            case 'billing_first_name':
            case 'shipping_first_name':
                return $current_user->user_firstname;

            case 'billing_last_name':
            case 'shipping_last_name':
                return $current_user->user_lastname;

            case 'billing_email':
            case 'shipping_email':
                return $current_user->user_email;
        }
    }
    return $value;
}
  • Thank you Sally! I will give your updated code a try. I appreciate your time. – Rick Jun 12 at 5:53
  • Sure, take your time. :) – Sally CJ Jun 12 at 6:02
  • Sally, seems to be working just as I need! Thank you very much! – Rick Jun 13 at 5:45
  • I'm glad to hear that, and you're welcome. :) – Sally CJ Jun 13 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.