1

I am wondering if there is any way to check if a successful purchase was from a new or returning customer.

I have a script which needs added to the Order Success page.

I've got this so far, which doesn't really work as I need it to as it is only checking for guest or logged-in checkout:

$order = wc_get_order($order->id);
$user = get_user_by('email', $order->billing_email);

if (isset($user->ID)) {
    echo 'User is logged in.';
} else {
    echo 'User is a guest.';
}

Thanks!

stevie-c91
  • 98
  • 9

2 Answers2

0

You can simply use wordpress is_user_logged_in() function with hook woocommerce_thankyou to check the order status and user is logged in or not.

add_action('woocommerce_thankyou', 'my_custom_tracking', 10, 1);

function my_custom_tracking($order_id) {
    if (!$order_id) {
        return;
    }
    // Lets grab the order
    $order = wc_get_order($order_id);

    $_billing_email = get_post_meta($order_id, '_billing_email', true);
    $user = get_user_by('email', $_billing_email);

    //for successful order
    if (in_array($order->status, ['processing', 'completed'])) {
        if (is_user_logged_in() || $user) {
            //it is a returning user
        } else {
            //user is a guest
        }
    }
    //unsuccessful order
    else {

    }
}

Please Note: if you want to check ONLY user is Logged In or not then replace if (is_user_logged_in() || $user) by if (is_user_logged_in())

Related Question: woocommerce php snippets for proceeded to checkout to know user is login or not


UPDATED v2
add_action('woocommerce_thankyou', 'wh_isReturningCustomer', 10, 1);

function wh_isReturningCustomer($order_id) {
    if (!$order_id) {
        return;
    }
    // Lets grab the order
    //$order = wc_get_order($order_id);

    $_billing_email = get_post_meta($order_id, '_billing_email', true);

    $args = [
        'post_type' => 'shop_order',
        'post__not_in' => [$order_id], //exclude current Order ID from order count
        'post_status' => ['wc-processing', 'wc-completed'],
        'posts_per_page' => -1,
        'meta_query' => [
            'relation' => 'AND',
            [
                'key' => '_billing_email',
                'value' => $_billing_email,
                'compare' => '=',
            ]
        ]
    ];
    $posts = new WP_Query($args);
    if ($posts->post_count) {
        //it is a returning user
    } else {
        //user is a guest
    }
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Hope this helps!

Community
  • 1
  • 1
Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
  • Thanks for your help! Only issue I can find is that if the user registers during checkout and purchases a product successfully, they will always be seen as a 'returning' customer as they will be logged in and their billing email will be found. Any way around this? Thanks again. – stevie-c91 Feb 14 '17 at 15:02
  • @stevie-c91: I have updated my answer please check it. use `wh_isReturningCustomer()` function. – Raunak Gupta Feb 14 '17 at 18:44
  • Thanks very much! You have been a great help! – stevie-c91 Feb 16 '17 at 14:58
  • you are welcome and, I'm glad that it help you. just one point if user is a guest and it still have 4-5 order then you wont get `$user_id` – Raunak Gupta Feb 16 '17 at 15:08
  • I've customised it slightly, so in my case guests will have an ID, new customers have a different ID and returning customers have another ID. Cheers :) – stevie-c91 Feb 16 '17 at 15:10
0

The following code should work for returning customer as well as new customer irrespective of a change in billing email address. This should also work for a new customer registering while checking out.

add_action('woocommerce_thankyou', 'is_returning_customer', 10, 1);

function is_returning_customer($order_id) 
{
    if (!$order_id) {
        return;
    }
    if(is_user_logged_in()) {
        $order_status = array('wc-on-hold', 'wc-processing', 'wc-completed');
        $customer_id = get_current_user_id(); 
            $customer_orders=get_posts( array(
                'meta_key' => '_customer_user',
                'meta_value' => $customer_id,
                'post_type' => 'shop_order', 
                'post_status' => $order_status,
                'numberposts' => -1
            )
        );
        if(count($customer_orders)>1) {
            //returning customer
        } else {
            //new customer
        }
    }
}
LearnWoo
  • 71
  • 3