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.

Update 2017: Since this article was written, WooCommerce added transients for this function.
Leave a Reply

Get your fix of
all cool things WordPress

DigitalOcean hosting coupon 10$ coupon

for DigitalOcean hosting.

Get this deal