问题
If a user registers on the site (enter username and email) and THEN go to purchase, filling in the details of invoicing and shipping, the field name in the WordPress user profile is empty!
If the customer does not register before, but makes recording the time of purchase, then the name and surname of the user profile fields are populated.
Any solution?
Thanks.
See this screenshot of wordpress users list:
回答1:
What you are pointing is the empty display name in user list. The user first name and last name exist.
So is not very important, but it is certainly useful for administrator or shop_manager user roles to have this information in user list for searching purposes, retrieving a user by this "display name".
Here is a function that is fired when a user register first time. Inside it, we check if that "display name" exist:
- If the "display name" exist, we exit from the function.
- If the "display name" doesn't exist, we setting this display name from user first name and last name, then we update this in database.
Use this code pasting it in function.php file of your active child theme or theme:
add_action( 'user_register', 'wc_new_user_display_name_update', 10, 1 );
function wc_new_user_display_name_update( $user_id ) {
// Check if user display name exist.
// If yes, we stop the process getting out of this function.
if ( !empty( get_the_author_meta( 'display_name', $user_id ) ) {
exit;
}
$user_info = get_user_meta( $user_id );
if ( !empty($user_info->first_name ) ) {
$user_first_name = $user_info->first_name;
} else {
$user_first_name = $user_info->billing_first_name;
}
if ( !empty( $user_info->last_name ) ) {
$user_last_name = $user_info->last_name;
} else {
$user_last_name = $user_info->billing_last_name;
}
// setting up user display name
$display_name = $user_first_name . ' ' . $user_last_name;
// setting up user display name
wp_update_user( array( 'ID' => $user_id, 'display_name' => $display_name ) );
}
Base on this thread (thanks to helgatheviking): After woocommerce/wordpress registration
回答2:
My issue was that due to custom woocommerce checkout and little messed up coding, it wasn't updating the user's WordPress first name & last name fields at the time of placing a new order.
I tried the above code with little tweaks based on my scenario but that didn't work for me as the user was getting created and then the order was placed - so there was no information about the order till that time.
I tried hooking other solutions like this - Updating the WooCommerce 'User Firstname Field' with 'Billing First Name' on check out but that didn't work for me either.
or tried to hook process_checkout but nothing worked.
I solved the issue by making the changes in email-order-details.php (woocommerce's email template)
I added the line below after: <?php if ( ! $sent_to_admin ) : ?>
on line 24.
<?php wp_update_user( array( 'ID' => (int)$order->user_id, 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name) ); ?>
Reason: I wanted to change the values after the order is placed and since email is sent after the order is placed, it made sense to add this
来源:https://stackoverflow.com/questions/37879112/woocommerce-filling-the-first-and-last-name-fields-in-the-user-profile-bug