WooCommerce: Check if User Has Already Purchased Product

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 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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @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">&hearts; Hey ' . $current_user->first_name . ', you\'ve purchased this in the past. Buy again?</div>';

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

27 thoughts on “WooCommerce: Check if User Has Already Purchased Product

  1. hi

    is there a way to add a date of when the product was purchased like amazon does?


    1. Hi Ed, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Thank you for the snippet! I only wish there was a code for the product in the shopping cart. I’m afraid they’d miss it on the thumbnail. I’m using it nonetheless. It’s cool. ๐Ÿ™‚

    1. Thank you ๐Ÿ™‚

  3. hi Dear
    this is a fine tutorial

    and could you help me in my issue?
    my problem is:
    how can I improve the code in this way: discover if a person purchased a product and paid not in “cash delivery method” .
    this code couldn’t discover ,that user selected which payment methods. because even a customer purchase a product by “cash on delivery” way the code couldn’t distinguish between.

    I want to use this code to build a shortcode.

    thank you

    1. Hey Mamood, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  4. Hello
    good day. Thanks for the tutorial. I’m just wondering how to apply this in the modal.?
    – Click product thumbnail
    – pop up window appear
    – with message “you already purchased item”.

    I’m still learning WP and i’m exploring things. ๐Ÿ™‚
    Thank you

    1. Hey Rai, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  5. Very useful snippet – thank you. Do you have a similar snippet that will display if the user has not purchased an item?

    1. Hey Charlotte, thanks for your comment! Well, you can use the exact same snippet and add a “!” which in PHP means “not” ๐Ÿ™‚

    2. Thank you for the help, where in the snippet would you add “!” ?

      1. In front of “wc_customer_bought_product” ๐Ÿ™‚

  6. Is there a way to create a shortcode that checks if a logged in user has every purchased a product?

    I would like to wrap some text in a shortcode, that only displays to users who have purchased any product.

    1. Jon, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  7. Since the Woocommerce update, I got this notify:
    Notice: id was called incorrectly. Product properties should not be accessed directly. Backtrace: require.
    Would you like to update this snippet? Thank you.

    1. Good point Jonny! Snippet has now been updated ๐Ÿ™‚

  8. […] few snippets ago we introduced the magic WooCommerce inbuilt function “wc_customer_bought_product” – automatically, with a single line of PHP, you can find out if the user has already […]

  9. Is there any way to limit the function to the last 6 months?
    Basically, I’m trying to restrict any product so that if it’s been purchased once, it can’t be purchased again for 6 months.

    1. Hey Juliah, thanks so much for your comment! I’m sure there is a PHP way, but unfortunately it’s custom code and cannot provide it here in the blog comments. Thanks for your understanding! R

  10. Is there a way to do an if statement to make this so you can target the text for a specified product or array of products instead of all of them?

    1. Valerie, thanks for your comment! Yes, clearly there is a way or two – maybe take a look at these conditional logic examples to see if you find a suitable one: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

  11. What if the customer bought the item but didn’t create an account, or is not logged in?

    Is there an effective way to check if the customer (that is about to place an order) is new or not? Keep in mind that NOT all customers will create an account when they place an order. This is purely practicality.

    This would be helpful for when you want to allow a coupon to be used only by NEW customers on their first order.
    Maybe the most straightforward way is to check the billing email address against the orders database in WC to see if there’s a match. (Of course this wouldn’t prevent people from using a different email address)

    Rodolfo, maybe you could develop a snippet or plugin for this?


    1. Hey Vuster, thanks for your comment ๐Ÿ™‚ However – how do you know what user it is, if they’re not logged in? Unfortunately there is no other way around in my opinion ๐Ÿ™‚

  12. Wow, I was expecting a big long function that did a WP_Query on all orders and searched by customer and then searched each order for product ID. And there was this nice and simple WC core function – there must be a few more gems like this in the code. It pays to read source code!!

    1. I totally agree Damien! I was actually pretty surprised when I found this out ๐Ÿ™‚ Cheers for the feedback!

      1. This code snippets was very timely, I was just about to code exactly this. Was unaware of the Woocommerce function.
        Thanks again.
        You’re the best.

        1. Hey Curtis, I know, that function is a life saver! Looking forward to finding more of those ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.