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

WooCommerce: check if logged in user has bought a productWooCommerce: check if logged in user has bought a product
WooCommerce: check if logged in user has bought a product

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 3.6.3
 * @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;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) echo '<div class="user-bought">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
}
Share:

Leave a Reply