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

Leave a Reply

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