WooCommerce: Check If User Has Purchased Product

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>';
   }
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). 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.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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.

38 thoughts on “WooCommerce: Check If User Has Purchased Product

  1. Nice snippet! Any idea to make this happen: Show pagecontent only if the user has bought a specific product? (The specific product contains a url on the domain which they can visit after purchase).

    1. Thanks Jurgen!

      Yes, that’s totally doable.

      For example, on this same website, if you go to any course lesson, I’ve done just that! I only show a Gutenberg block (video) if the current user has purchased the current course e.g. https://www.businessbloomer.com/lesson/cuwm1l01/

  2. Thank you so much, this saved me so much time! I was in the process of writing a long query and a huge for loop to check through all previous orders.

  3. Great little snippet, thank you! I’d love to use this same snippet for HIDING products that have already been purchased by the user. Can that be done with this snippet?

    1. Hi Ry, 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!

  4. What if i want to add upto 3 products to know if the user has purchased any of the 3products, how can that be done?

    1. Hi Panda, 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!

  5. I am new in WordPress and when I paste this code in function.php and open shop page then this functionality does not work for me.

    1. Are you logged in with the user who purchased some products?

  6. Hello,
    thank you for the beautiful sharing.

    can you have a tutorial about something like Easy Digital Downloads โ€“ Additional Shortcodes‘s shortcodes? but for woocommerce.
    which is to make shortcodes like this:
    [edd_user_has_purchases]Content Here[/edd_user_has_purchases]
    [edd_user_has_no_purchases loggedout=true]Content Here[/edd_user_has_no_purchases]

    1. Hi Frangki, 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!

  7. hi

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

    thx

    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!

  8. 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 ๐Ÿ™‚

  9. 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

  10. Hello
    good day. Thanks for the tutorial. I’m just wondering how to apply this in the modal.?
    ex.
    – 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

  11. 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” ๐Ÿ™‚

  12. 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

  13. 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 ๐Ÿ™‚

  14. 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

  15. 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/

  16. 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?

    Thanks

    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 ๐Ÿ™‚

  17. 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 shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.