This week one of our partners needed to implement a simple script for checking whether an user, in a Woocommerce website, had ever purchased anything. Searching the internet forums, he came up with the most bizarre solutions.

Some scripts used direct MySQL queries to get such data, while some others relied in a “foreach” to check all orders, before returning a simple boolean. So, taking the excellent and well-documented API that WooCommerce provides, can’t we find a better way of doing that?

A class named WC_Customer

Let’s think – if WooCommerce has a particular class specialized in managing customers, why should we look somewhere else? As long as this class includes a method that checks previous purchases, we can reach the same.

Let’s be fair: it doesn’t have any method that says it can “check whether a customer has purchased or not”. However, there a simple one to grab a customer’s last order.

Cause and consequence: if a customer has NEVER purchased anything before, wouldn’t his last order be NULL or FALSE? Well, that’s exactly what happens if you pass an user ID to that method, and this user never bought anything.

So, let’s bear that in mind and create a simple class that does the job.

Getting it done in two lines

Target: create a class that makes possible to check it in no more than two lines. First, we need to get the current customer ID or, alternatively, pass an user ID as argument while instantiating the class. Designing it, we’d have something like this:

class CustomerPurchases
	protected $customer_id;

	public function __construct(int $customer_id = null)
		if(!is_null($customer_id)) {
			$this->customer_id = $customer_id;
		} else {
			$current_id = get_current_user_id();
			$this->customer_id = $current_id;

So, every time we use this class, if we do not provide any user ID, it considers the current user’s ID number. Now, as a second part, the only thing we need to do is creating a method for checking existent purchases. The WC method we want to use is get_last_order().

This method returns, for a given user ID, the last WC_Order object for the respective customer. However, if the user has never bought anything, it returns false. So, here we have our conditional. Let’s create a method in our class:

class CustomerPurchases
  // ...
  public function hasPurchases()
		$customer = new \WC_Customer($this->customer_id);
		$check = $customer->get_last_order();
		if($check === false) {
			return false;

		return true;

Done! Now you can use this class for checking if an user has ever purchased wherever you want. Just copy inside your plugin or theme and use it like this:

$instance = new CustomerPurchases();
$check = $instance->hasPurchases();

if($check == false) {
  // Do something.
} else {
  // Do something else.

Anything else?

We could add much more to this class. However, the problem was solved for this situation. And you? Do you have any repetitive or troublesome routine while using WooCommerce, WordPress or any other plugin, and would like to see it solved rapidly and efficiently?

So drop us a message, telling what we could do for you. And, if you really loved our solution, and are loving our approach, you can tip us. Just 1 dollar – we do not intend to be rich, but could really use a cup of coffee while coding.

Leave a comment

Your email address will not be published. Required fields are marked *