WooCommerce : Disable Repeat Purchase Of Products.


Want to sell one product only once per user?

Restrict User to buy a specific product only once in a lifetime from your WooCommerce site?

Some times in WooCommerce products we need to stop user from buying product multiple time, that means user can buy that product only once.

Depending on what kind of products you selling from your WooCommerce online store, you may want to restrict your customers from purchasing a product more than once from your online store.
May be want to disable repeat purchases for all products, or may be want to prevent repeat purchase of a specific product.

For applying any restriction first we need to check that user has already bought that product or not after that we need to disable buying ability of user for that product.

WooCommerce provide woocommerce_is_purchasable and woocommerce_variation_is_purchasable filter for setting restrictions to products.
we can apply our conditional statements in it.Both woocommerce_is_purchasable and woocommerce_variation_is_purchasable returns true and false.

And for checking user has already bought product or not we can use ‘wc_customer_bought_product’ function.

SO it’s a combination of two methods

  1. woocommerce_variation_is_purchasable filter
  2. woocommerce_is_purchasable filter
  3. ‘wc_customer_bought_product’ function
// Disable repeat purchase of WooCommerce  products / WooCommerce variations
function DisableRepeatPurchase( $purchasable, $product ) {  
  // Get ID for the current product
  $product_id = $product->get_id();  
  // return false if the customer has already bought the WooCommerce product / WooCommerce variation
  if ( wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
    $purchasable = false;
  } 
  //check for variations: if parent is not purchasable, then variation is not
  if ( $purchasable && $product->is_type( 'variation' ) ) {
    $parent = wc_get_product( $product->get_parent_id() );
    $purchasable = $parent->is_purchasable();
  }  
  return $purchasable;
}
add_filter('woocommerce_variation_is_purchasable','DisableRepeatPurchase',99,2 );
add_filter('woocommerce_is_purchasable','DisableRepeatPurchase',99,2 );

Paste this code to your theme’s functions.php.
After this if product in already bought by user the add to cart button is disabled.
Note: If This product is already in cart then too no need to worry about it, it will automatically removed from cart page and checkout page.
Note: This will add restriction for all product. for specific product we need to need conditional statement to check product id before applying restriction.
Example:

// Disable repeat purchase of WooCommerce  products / WooCommerce variations
function DisableRepeatPurchase( $purchasable, $product ) {
  // Enter ID of the product which must be restricted for purchasing again
  $restricted = 1234;  
  // Get ID for the current product
  $product_id = $product->get_id(); 
  //Check product id with restricted product if true then restrict else return parent $purchasable value
  if ( $restricted != $product_id ) {
    return $purchasable;
  }
  // return false if the customer has already bought the WooCommerce product / WooCommerce variation
  if ( wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
    $purchasable = false;
  } 
  //check for variations: if parent is not purchasable, then variation is not
  if ( $purchasable && $product->is_type( 'variation' ) ) {
    $parent = wc_get_product( $product->get_parent_id() );
    $purchasable = $parent->is_purchasable();
  }  
  return $purchasable;
}
add_filter('woocommerce_variation_is_purchasable','DisableRepeatPurchase',99,2 );
add_filter('woocommerce_is_purchasable','DisableRepeatPurchase',99,2 );
, ,

Leave a Reply

Your email address will not be published. Required fields are marked *