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!
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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 5
* @community https://businessbloomer.com/club/
*/
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>';
}
}
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).
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/
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.
Awesome
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?
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!
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?
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!
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.
Are you logged in with the user who purchased some products?
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]
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!
hi
is there a way to add a date of when the product was purchased like amazon does?
thx
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!
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. 🙂
Thank you 🙂
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
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
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
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
Very useful snippet – thank you. Do you have a similar snippet that will display if the user has not purchased an item?
Hey Charlotte, thanks for your comment! Well, you can use the exact same snippet and add a “!” which in PHP means “not” 🙂
Thank you for the help, where in the snippet would you add “!” ?
In front of “wc_customer_bought_product” 🙂
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.
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
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.
Good point Jonny! Snippet has now been updated 🙂
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.
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
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?
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/
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
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 🙂
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!!
I totally agree Damien! I was actually pretty surprised when I found this out 🙂 Cheers for the feedback!
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.
Hey Curtis, I know, that function is a life saver! Looking forward to finding more of those 🙂