This snippet checks the billing email field upon the user clicking the place order button and if this email matches the email you have defined in this code, a custom error will appear, the cart is emptied, and the user will not be able to check out. In this example, we have let the user know their email is suspicious, their cart has been emptied, and to leave the website. This snippet is useful if you are finding the same email address is spamming your store with fake orders or maybe even a competitor trying to snoop around your store. If you’re finding you’re checkout is being spammed by bots, trying different cards for example, you should probably start with DNS. Start by rate limiting, country, and IP blocking.
/**
* Snippet Name: WooCommerce Prevent Email From Checking Out
* Snippet Author: ecommercehints.com
*/
add_action( 'woocommerce_after_checkout_validation', 'ecommercehints_validate_checkout_email_address', 10, 2);
function ecommercehints_validate_checkout_email_address($fields, $errors) {
if ( $fields[ 'billing_email' ] == 'spammer@spam-mail.com' ) { // The email address to block
$errors->add( 'validation', 'Your email address has been flagged as suspicious and your cart has been emptied. Please leave this website.' );
}
WC()->cart->empty_cart(); // Empty the cart
}