Home » WooCommerce: Only Allow to Buy a Product Once

WooCommerce: Only Allow to Buy a Product Once

by Tutor Aspire

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 “Tutor Aspire” 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 tutoraspire.com FREE
* @author        Tutor Aspire
* @testedwith    WooCommerce 6
* @donate $9     https://tutoraspire.com
*/

add_filter( 'woocommerce_is_purchasable', 'tutoraspire_deny_purchase_if_already_purchased', 9999, 2 );
 
function tutoraspire_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

You may also like