Woocommerce: Checking if successful purchase was f

2019-06-07 07:55发布

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!

2条回答
狗以群分
2楼-- · 2019-06-07 08:03

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!

查看更多
Viruses.
3楼-- · 2019-06-07 08:22

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
        }
    }
}
查看更多
登录 后发表回答