Check if a user has bought an item using WooCommerce built-in function

Check if a user has bought an item using WooCommerce built-in function

The built-in function woocommerce_customer_bought_product can be used to check if a user has bought an item. This function has 3 parameters:

$email – the email of the user
$user_id – the user_id of the user
$product_id – the ID of the item/product

Using woocommerce_customer_bought_product in a shortcode

If you require using this function in a shortcode (for example if you want to display private content only for customers who already purchased your product), you can easily do so.

First, place the following snippet in functions.php:

function wcr_check_user_bought($atts, $content = null) {
    extract(shortcode_atts(array('product_id' => false), $atts));

    if (!$product_id) {
        return false;

    $current_user = wp_get_current_user();
    $was_bought = !$current_user->exists() ? false : woocommerce_customer_bought_product($current_user->user_email, $current_user->ID, $product_id);

    if (!$was_bought) {
        return 'You must buy the product!';

    return do_shortcode($content);
add_shortcode('check_bought', 'wcr_check_user_bought');

Then call the shortcode in the post content like this:

[check_bought product_id="33"]
    <!-- content goes here -->

woocommerce_customer_bought_product function should be used sparingly, as it can be very resource-greedy on high traffic websites. In this case it helps if you save the result into a transient.

You can also use third-party tools like ClickFunnels, Google Analytics to track user behavior. Read this extensive ClickFunnels review or take Google Analytics course to improve your sales. Also check our Semrush review for competitor analysis and more.

Update 2017: Since this article was written, WooCommerce added transients for this function.
  • مدیران وردپرس

    Very good
    Thank you for this short code

  • Yasin

    this code not working

    i use this code and buy test product
    but this shortcode just show me You must buy the product! and dont show to me my text in shortcode

    can you help me pls ?

Leave a Reply

Get our Wordpress
freebies to your inbox