Recently while working with one of our clients to improve their checkout funnel, we noticed that WooCommerce does not link order made as a guest (non logged-in) with email address which has existing account in WooCommerce.
These orders will be marked as guest orders and will not be linked to the customer account and will not show in their order history on “My Account” section.
Add this snippet to your theme’s functions.php file In order to link guest orders made by registered customers email the the customer account:
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
Will this work if the guest checkout order is the only order they’ve made and then they register for an account afterwards?
Yes, it should
Great Article!
How can I run this on current orders and customers/guests ?
Hi, this will work only for new orders.. you will need to create a custom code to run on all orders to do it for old orders
This is great. As Nitsan is saying, I wish to also add this functionality the other way around.
1. Guest purchases
2. Guest registeres and purchases
And now, both orders are connected to the registered email address.
Do you know what to change in the code to be able to also achieve this?