I would like to create a WooCommerce plugin to add some offers for customers (that have a purchase History).
How can I check a user bought something before?
Thanks.
I would like to create a WooCommerce plugin to add some offers for customers (that have a purchase History).
How can I check a user bought something before?
Thanks.
Here is the code for this conditional function:
This code is tested and works.
This code goes in function.php file of your active child theme or theme, or in a plugin php file.
USAGE (as a condition):
References
If Guest Checking is enabled the this function might help
Just send $customer_email as argument and function will check in all the orders for that email and return true or false.
Enhanced version with paid state or just status agnostic query, also improved a bit code typing, for PHP 5.4+ :
Here is a much light and faster conditional function that will return true if a customer has already maid a purchase.
There is an optional argument
$user_id
, that will allow you to specify a defined user ID:Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works (It should work on previous versions too).
Usage example 1 (logged in customer)
Usage example 2 (setting the $user_id)
If the
$user_id
is not defined and the current user is not logged in, this function will returnfalse
.Simplified version: