Home » WooCommerce: Check If User Has Purchased Product

WooCommerce: Check If User Has Purchased Product

by Tutor Aspire

If you need to detect if a logged in user has purchased a certain product ID, this snippet will do the trick. You can use this for marketing (e.g. “Buy More of This!”) or for displaying special notices on the loop or the single product page. Enjoy!

WooCommerce: check if logged in user has bought a product
WooCommerce: check if logged in user has bought a product and display a message under such item in the shop page

PHP Snippet: Check if Logged In User Has Already Purchased a Product

Thanks to Woo (woo-hoo!) there is a handy little function called “wc_customer_bought_product”. So, no need to code that from scratch, that function already does the check for us.

/**
 * @snippet       WooCommerce Check if User Has Purchased Product
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */
 
add_action( 'woocommerce_after_shop_loop_item', 'tutoraspire_user_logged_in_product_already_bought', 30 );
 
function tutoraspire_user_logged_in_product_already_bought() {
   global $product;
   if ( ! is_user_logged_in() ) return;
   if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
      echo '
You purchased this in the past. Buy again?
'; } }

You may also like