Prevent WooCommerce Guest Checkout from Repeat Purchase of a Particular Product

Prevent guest checkout from repeat purchase of a particular product if using WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

//Hook into the after checkout validation action, add a callback function named 'validate_no_repeat_purchase' with a priority of 10 and pass two arguments.
add_action( 'woocommerce_after_checkout_validation', 'validate_no_repeat_purchase', 10, 2 );

//The callback function accepts the array of posted checkout data and an array of errors
function validate_no_repeat_purchase( $data, $errors ) {
	//extract the posted 'billing_email' and use that as an argument for querying orders.
	$args = array(
		'customer' => $data['billing_email'],
	);

	//loop through the cart items to see if one is the product that doesn't allow repeat purchases.
	foreach( WC()->cart->get_cart() as $cart_item ) {
		$product_id_no_repeat_purchases = 666;//substitute your product id.
  if ( $cart_item['product_id'] === $product_id_no_repeat_purchases ) {//note the identical operator.
			//use the WooCommerce helper function to query for orders with this email address:
			$orders = wc_get_orders( $args );

			//if an order with this email exists, don't allow another one.
			if ( $orders ) {
				$errors->add( 'validation', '<strong>Repeat order not allowed.</strong>' );
			}
		}
	}
}
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.