Answer the question
In order to leave comments, you need to log in
How to replace in the admin panel in the order status of a guest with a registered user?
Hello! There is a woocommerce store .
Manually I add the user after the order made by it.
Everything would be fine, but in the order status the system defines him as a guest, but I would like this field to be displayed as a user known to her.
Как этого можно добиться? Вот мой код:
add_action( 'woocommerce_thankyou', 'create_user_callback' );
function create_user_callback($order_id){
$order = wc_get_order( $order_id );
$order_detail['customer_first_name'] = get_post_meta( $order_id, '_billing_first_name', true );
$order_detail['customer_phone'] = get_post_meta( $order_id, '_billing_phone', true );
$order_detail['customer_email'] = get_post_meta( $order_id, '_billing_email', true );
$order_detail['customer_address'] = get_post_meta( $order_id, '_billing_address_2', true );
$customers = get_users( array( 'role' => 'customer' ) );
foreach( $customers as $customer ) :
$customers_email = get_user_meta( $customer->ID, 'billing_phone', true );
if($customers_phone!=$order_detail['customer_phone'] && $customer->user_email!=$order_detail['billing_email']) {
$userdata = array(
'user_pass' => '123',
'user_login' => $order_detail['customer_email'],
'first_name' => $order_detail['customer_first_name'],
'user_email' => $order_detail['customer_email'],
'role' => 'customer',
);
$userid = wp_insert_user( $userdata );
if ( !is_wp_error( $userid ) ) {
add_user_meta( $userid, 'billing_phone', $order_detail['customer_phone'] );
add_user_meta( $userid, 'billing_address_2', $order_detail['customer_address'] );
}
}
endforeach;
}
Answer the question
In order to leave comments, you need to log in
Adding the feature
helped me link the current user to all previous accounts.
Grabbed from this article
But this function allows you to associate the current order with my user
update_post_meta($order_id, '_customer_user', get_current_user_id());
Well, firstly, taking all users, looping through the entire list and comparing emails to find the right user is hell of course. There is also get_user_by( 'email', $email ) .
And secondly, WooCommerce registers users in a different way, and binds the client, and there are their own hooks. Why here woocommerce_thankyou
?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question