Link WooCommerce Guest Orders to Customer Account

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

5 thoughts on “Link WooCommerce Guest Orders to Customer Account

  1. blank
    Roger says:

    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?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.