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

9 thoughts on “Link WooCommerce Guest Orders to Customer Account

      • blank
        Dave says:

        Love this functionality…thank you for sharing!! I’ve just been doing some testing and this only appears to be working for new orders placed. If a customer has an account and checkouts as a guest using the same email, the order automatically gets the user’s account attached. However, if the user creates an account after placing the guest order, or if the user has an account but placed a guest order prior to this functionality being added, the orders do not automatically attach to the account.

  1. blank
    jean says:

    I just tried it and it don’t work if the customer make an order as guest and then create an account.
    There is no orders in his account then…

  2. blank
    Zeddrich says:

    This doesn’t work, I added this to my functions.php and then made a fake guest order and a fake customer order and the orderinfo from the guest order wasn’t added to the customer.

  3. 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?

  4. blank
    matin says:

    Our scenario is: The guest user enters the site and places one or more orders without the need to register. And after a while, he decides to register on the site. Now how to Link Guest Orders to Customer Account after registeration?

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.