Halloween Sale 15% off Ends in See the plans
PHP

Block Checkout for Listed Emails and Phone Numbers

Stops orders from customers on your block list, such as repeat chargeback offenders, matched by email address, email domain or phone number.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_block_list = array(
    'emails'  => array( '[email protected]' ),     // Full email addresses.
    'domains' => array( 'bad-domain.example' ),    // Email domains.
    'phones'  => array( '+44 7700 900123' ),       // Phone numbers.
);

add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_block_list ) {
    $email  = strtolower( trim( (string) ( $data['billing_email'] ?? '' ) ) );
    $domain = false !== strpos( $email, '@' ) ? substr( strrchr( $email, '@' ), 1 ) : '';
    $phone  = preg_replace( '/\D/', '', (string) ( $data['billing_phone'] ?? '' ) );
    $phones = array_map( function ( $number ) {
        return preg_replace( '/\D/', '', $number );
    }, $wpide_block_list['phones'] );

    $blocked = in_array( $email, array_map( 'strtolower', $wpide_block_list['emails'] ), true )
        || ( $domain && in_array( $domain, array_map( 'strtolower', $wpide_block_list['domains'] ), true ) )
        || ( strlen( $phone ) > 5 && in_array( $phone, $phones, true ) );

    if ( $blocked ) {
        $errors->add( 'validation', 'Sorry, we are unable to process this order. Please contact us for help.' );
    }
}, 10, 2 );

Notes

Phone numbers are compared by digits only, and the message is deliberately neutral so blocked customers are not told why.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.