Menu Close

WooCommerce: Check If User Has Purchased Product

default

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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_user_logged_in_product_already_bought', 30 );
 
function bbloomer_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 '<div>You purchased this in the past. Buy again?</div>';
   }
}
View Source
Posted in WooCommerce Tips