WooCommerce : Disable Repeat Purchase Of Products.

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

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

we can simply use 'woocommerce_is_purchasable' filter and 'wc_customer_bought_product' WooCommerce function For this.
Example:

// 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_is_purchasable', 'DisableRepeatPurchase', 10, 2 );

After writing this code to your theme’s functions.php, if product in already bought by user the add to cart button is disabled. and this product is already in cart it will automatically removed.

Leave a Comment

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

Scroll to Top