WooCommerce: Only Allow to Buy a Product Once

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

/**
* @snippet       Deny second purchase
* @how-to        Get CustomizeWoo.com FREE
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 6
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/

add_filter( 'woocommerce_is_purchasable', 'bbloomer_deny_purchase_if_already_purchased', 9999, 2 );
 
function bbloomer_deny_purchase_if_already_purchased( $is_purchasable, $product ) {
   if ( is_user_logged_in() && wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
      $is_purchasable = false;
   }
   return $is_purchasable;
}

Notes:

  • 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

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.

4 thoughts on “WooCommerce: Only Allow to Buy a Product Once

  1. How can I add a message like “Product already Purchased” ?

    1. You could use something along the lines of this:

      add_action( 'woocommerce_single_product_summary', 'bbloomer_message_if_not_purchasable', 29 );
      
      function bbloomer_message_if_not_purchasable() {
         global $product;
         if ( is_user_logged_in() && wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
            echo 'You already bought this product!';
         }
      }
      
  2. 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.

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.