In the era of online courses, subscriptions, custom-made products and product personalization, it may happen a scenario where a user can only purchase a product once in their lifetime.
In this short tutorial, we will see how this is done.Clearly, the user must be logged in in order for the code to trigger, so this applies to stores that require checkout login in order to proceed with the order.
Enjoy!
User “Rodolfo Melogli” is currently logged in and in the past he purchased this same product. Result: the add to cart button is not showing thanks to the snippet below.
PHP Snippet: One Product Purchase in a Lifetime @ WooCommerce Single Product / Cart / Checkout
it would be nice to add a notice to tell the user why the Add to Cart is not showing. The snippet above ONLY hides the add to cart
in case the already purchased product is in the Cart, and user logs in from there, item will be removed from the Cart and an error message will be displayed
same applies to the Checkout page, in case the user logs in from there
Please let me know in the comments if everything went 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.
If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 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.
Is it possible to add a new button after hiding the add to cart button on the individual product page?
Because my product is a digital downloadable product, I hope that after the customer purchases the product, the add to cart button of the purchased product will be hidden, and a new button will be added to redirect the download page.
Now each product can only be purchased once, and the add to cart button will be hidden when the user completes the purchase.
So can you add a new button after hiding the Add to Cart button, this button will point to the product order page purchased by the user, when the user clicks the newly added button, for example (view order) we will directly take the user to view The order page for this product he purchased
Hello Franster, 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!
After adding this function, the add to cart button is replaced with “Read more” in the grid view. it is worked. but I need to change the “read more ” button text to “Already purchased”. is this possible? thanks
I found ths a great snippet. Thanks very much for helping me understand PHP in the funtions file through code like this. Unfortunately for me, the “Product already Purchased” additional snippet causes layout problems, forcing the next item on to the next row in the catalogue layout.
Thank You Rodolfo
This is a code that I was looking for a long now, I building a coupon website for a client, but customs can get only one product/coupon
Tell me must I replace the bbloomer_ with my website name?
Kind Regards
Frans
What a great help! Thanks for sharing thisI would like to understand, how to make this apply only to one specific product (in my case it´s a specific Woocommerce subscription), so user can buy a specific subscription only one time AND only if he has no purchase history yet. Where can I donate etc for a profound answer? 🙂
Thank you for sharing the piece of code. Its great and wokrs perfect for me.
But I also use the piece of code to have a message displayed instead of the add to cart button on a simple product page but the text does not appear. Can you help please?
Hi Francois, 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!
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 :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Is it possible to add a new button after hiding the add to cart button on the individual product page?
Because my product is a digital downloadable product, I hope that after the customer purchases the product, the add to cart button of the purchased product will be hidden, and a new button will be added to redirect the download page.
Now each product can only be purchased once, and the add to cart button will be hidden when the user completes the purchase.
So can you add a new button after hiding the Add to Cart button, this button will point to the product order page purchased by the user, when the user clicks the newly added button, for example (view order) we will directly take the user to view The order page for this product he purchased
Hello Franster, 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,
I’ve added you code to my functions.php, but it gives an error:
Cannot re-declare function bbloomer_deny_purchase_if_already_purchased.
I’d like to send a screenshot, but I see that’s no option.
Hi Tim, possibly you added it twice, in the same file or maybe as a Code Snippet. You need to remove the second copy of the snippet
Hi,
How can I configure this code for a specific product?
Regards
I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy
Hi, did you find the answer?
I know nothing about coding and really need this function restricted to one product only.
Thank you.
After adding this function, the add to cart button is replaced with “Read more” in the grid view. it is worked. but I need to change the “read more ” button text to “Already purchased”. is this possible? thanks
You could reuse some of this code: https://www.businessbloomer.com/woocommerce-hide-add-to-cart-if-already-purchased/
I found ths a great snippet. Thanks very much for helping me understand PHP in the funtions file through code like this. Unfortunately for me, the “Product already Purchased” additional snippet causes layout problems, forcing the next item on to the next row in the catalogue layout.
Thank you! For the issue, it may just need some additional CSS
Thank You Rodolfo
This is a code that I was looking for a long now, I building a coupon website for a client, but customs can get only one product/coupon
Tell me must I replace the bbloomer_ with my website name?
Kind Regards
Frans
No need, no, that’s only a prefix I use, you can either remove it, or rename it. Basically you can give the PHP function whatever name you want
What a great help! Thanks for sharing thisI would like to understand, how to make this apply only to one specific product (in my case it´s a specific Woocommerce subscription), so user can buy a specific subscription only one time AND only if he has no purchase history yet. Where can I donate etc for a profound answer? 🙂
Thank you Robert! Here: https://www.businessbloomer.com/bloomer-armada/
Was this answered? I could also use a single product restriction. So IF this is product A, then only buy once.
I answered privately, yes. Basically, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂
How can I add a message like “Product already Purchased” ?
You could use something along the lines of this:
Hi Rodolfo
Thank you for sharing the piece of code. Its great and wokrs perfect for me.
But I also use the piece of code to have a message displayed instead of the add to cart button on a simple product page but the text does not appear. Can you help please?
Thanks
Hi Francois, 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!
That is good, but could be better if there is a time limit.
Like if customer bough it in last 24 hours, then can’t buy again.
Great idea!