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!
You can simply use wordpress
is_user_logged_in()
function with hookwoocommerce_thankyou
to check the order status and user is logged in or not.Please Note: if you want to check ONLY user is Logged In or not then replace
if (is_user_logged_in() || $user)
byif (is_user_logged_in())
Related Question: woocommerce php snippets for proceeded to checkout to know user is login or not
UPDATED
v2
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!
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.