How to check if WooCommerce customer has purchased product

Have you ever needed to know whether a customer has purchased a specific product before in your WooCommerce store? In this article will show you how you can create a conditional statement to check if the WooCommerce customer has purchased product or not. We are also going to look at an alternative way of checking which products a specific customer has already purchased.

There are many different situations where you might want to use this information. For example:

  • Marketing: You can display custom messages next to the specific product where you might want to show your customer has already purchased this product and to ask them if they want to repurchase it again (e.g. “You have purchased this in the past. Buy again?”).
  • Custom discounts: You can use the information of previous purchases of a customer to offer them a custom discount of they decide to buy again the same product (e.g. “Buy again and get 10% discount with this code!”)
  • Optimise product display: If you sell products that typically aren’t purchased multiple times by different customers (such as books), then an obvious optimisation of your product listings would be to move away or completely hide the product from the product page. If we take this approach and apply it to your website you might see some interesting patterns and some excellent examples.
  • Custom recommendations: You can use this information to optimise your product recommendations and offer related product that are very relevant to the already purchased products or to upsell a more premium product.

woocommerce customer has purchased product

How to check if customer has already purchased product programmatically

Now let us dive a little deeper into the actual code. In order to check if a customer has purchase specific product we will need to create a conditional statement. A conditional statement will execute one action based on another action. Fortunately for us, there is a useful WooCommerce function “wc_customer_bought_product” that allows us to check if our customers (by email or ID or both) have already bought a product before. This makes things a lot simpler as with this function we can create an conditional statement that we can use following PHP snippet to create our own customisation like this:

add_action( 'woocommerce_after_shop_loop_item', 'my_customer_already_bought_product', 30 ); 
function my_customer_already_bought_product() { 
 global $product; 
 if ( ! is_user_logged_in() ) return; 
 if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {  
   echo 'You have purchased this in the past. Buy again?'; 
 } 
}

 

The recommended way is to add the code into your child theme functions.php file (not your parent theme) alternatively you might use a dedicated snipped plugin like the code snippets plugin. When adding PHP code to WooCommerce and WordPress is important that done properly.

Please note that the above code works only for customers that already have user account on your WooCommerce store and are logged in. It will not work for guest checkout.

How to check if customer has purchased product with plugin

If you don’t need a custom feature for your store but you just want to simply check if a customer has purchased a specific WooCommerce product or if you want to list all the customers who have purchased a specific product you can do it with Users Insights. All you have to do is to use one of the dedicated filters like “Ordered products include”:

Filter Ordered WooCommerce digital products

This filter will list all WordPress users you have ordered the specific product form your WooCommerce store. If you combine two or more filter you can find customers who have purchased multiple products from your store.

You you can use the “Placed an order filter” for more advanced and detailed customer filtering.

WooCommerce Placed an Order filter

You can read more about this feature in the smart filters article.

Conclusion

As you can see even though there isn’t an out-of-the-box option, there are a few different ways that you can use to check if a WooCommerce customer has purchased specific product. Which method you’ll choose will depend on your preferences and needs. If you need a custom feature and are comfortable in dealing with code, you can use the PHP method. If however, coding is not your thing, you can use a dedicated WooCommerce plugin like Users Insights. We hope you find this guide useful.