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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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. Required fields are marked *