Reputation: 31
I'm using WooCommerce and have trouble in checking if the order is a guest customer.
Tried using $order = new WC_Order($orderid);
found nothing.
Any help is appreciated.
Thanks!
Upvotes: 1
Views: 5653
Reputation: 77
It looks like the best way to do this (at least as of WC 3.3.3) is to use WC_Order->get_user(): "Get the user associated with the order. False for guests."
It will return a WP_User for logged in customers, or false
for guest checkout.
Note that if you need to reliably determine the actual checkout method used, you should not rely on WordPress's get_user_by()
function; this will tell you whether a user account exists matching the email on the order, but not whether it was actually used to place the order. Someone who has an account can still order as a guest if they don't login; whether this distinction is important may depend on what you're trying to accomplish.
Upvotes: 2
Reputation: 357
You can try to get billing email and check if user is registered:
$order = new WC_Order($order_id);
$user = get_user_by('email', $order->billing_email);
if(isset($user->ID)){
//Registered
}else{
//Guest
}
Upvotes: 1