Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
00 Days
00 Hours
00 Minutes
00 Seconds

How to Add Custom Validation on Email Domains in WooCommerce Checkout Blocks & Classic Checkout Page?

In WooCommerce checkout blocks, basic validation is set when an email is not entered or when an incorrect email address is entered. However, in some cases, store owners may want to restrict the purchase of certain products to specific email domains, such as email domains of a specific organization or company partners etc. Let’s consider an example where a company might offer exclusive discounts on software licenses to employees using their corporate email addresses.
In this guide, we’ll show you how to automatically block purchases on the checkout blocks page unless the customer uses an email address from a predefined domain.

Solution: Add Custom Validation on Email Domains in WooCommerce Checkout Blocks Page

The code ensures that if a specific product (ID 2269) is in the cart, the customer must use an email address with a required domain (tychesoftwares.com). If not, the checkout process is blocked with an error.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action( 'woocommerce_store_api_checkout_update_order_from_request', function( $order, $request ) {
$cart = WC()->cart->get_cart();
$required_product_id = 2269; // Replace with your required product ID
$email_domain = 'tychesoftwares.com'; // Replace with the required email domain
$product_found = false;
// Check if the required product is in the cart
foreach ( $cart as $cart_item ) {
if ( $cart_item['product_id'] == $required_product_id ) {
$product_found = true;
break;
}
}
// Get the customer email from the request
$billing_email = isset( $request['billing_address']['email'] ) ? sanitize_email( $request['billing_address']['email'] ) : '';
// Extract domain from email
$email_parts = explode( '@', $billing_email );
$customer_domain = isset( $email_parts[1] ) ? $email_parts[1] : '';
// Validation: If the required product is in the cart, enforce the email check
if ( $product_found && $customer_domain !== $email_domain ) {
throw new Exception( __( 'To purchase this product, you must use an email address ending in @' . $email_domain, 'woocommerce' ) );
}
}, 10, 2 );
add_action( 'woocommerce_store_api_checkout_update_order_from_request', function( $order, $request ) { $cart = WC()->cart->get_cart(); $required_product_id = 2269; // Replace with your required product ID $email_domain = 'tychesoftwares.com'; // Replace with the required email domain $product_found = false; // Check if the required product is in the cart foreach ( $cart as $cart_item ) { if ( $cart_item['product_id'] == $required_product_id ) { $product_found = true; break; } } // Get the customer email from the request $billing_email = isset( $request['billing_address']['email'] ) ? sanitize_email( $request['billing_address']['email'] ) : ''; // Extract domain from email $email_parts = explode( '@', $billing_email ); $customer_domain = isset( $email_parts[1] ) ? $email_parts[1] : ''; // Validation: If the required product is in the cart, enforce the email check if ( $product_found && $customer_domain !== $email_domain ) { throw new Exception( __( 'To purchase this product, you must use an email address ending in @' . $email_domain, 'woocommerce' ) ); } }, 10, 2 );
add_action( 'woocommerce_store_api_checkout_update_order_from_request', function( $order, $request ) {
    $cart = WC()->cart->get_cart();
    $required_product_id = 2269; // Replace with your required product ID
    $email_domain = 'tychesoftwares.com'; // Replace with the required email domain
    $product_found = false;

    // Check if the required product is in the cart
    foreach ( $cart as $cart_item ) {
        if ( $cart_item['product_id'] == $required_product_id ) {
            $product_found = true;
            break;
        }
    }

    // Get the customer email from the request
    $billing_email = isset( $request['billing_address']['email'] ) ? sanitize_email( $request['billing_address']['email'] ) : '';

    // Extract domain from email
    $email_parts = explode( '@', $billing_email );
    $customer_domain = isset( $email_parts[1] ) ? $email_parts[1] : '';

    // Validation: If the required product is in the cart, enforce the email check
    if ( $product_found && $customer_domain !== $email_domain ) {
        throw new Exception( __( 'To purchase this product, you must use an email address ending in @' . $email_domain, 'woocommerce' ) );
    }
}, 10, 2 );

Output

When a customer attempts to place an order, the code checks whether a specific product (e.g., ID 2269) is in the cart. If the product is found, the system verifies the customer’s email domain.
Since we have defined the domain name as ‘tychesoftwares.com’ in the code, the checkout will only be allowed if the customer enters an email address ending with @tychesoftwares.com.

Add Custom Validation on Email Domains in WooCommerce Checkout Blocks Page

Solution: Add Custom Validation on Email Domains in WooCommerce Classic Checkout Page

This code applies the same logic as discussed above but performs the validation in classic checkout page.

By implementing this restriction, store owners can ensure that only eligible customers with specific email domains can proceed with their purchases. If you’re looking to customize your checkout experience further, you can also add additional custom fields in the checkout blocks page to collect extra information from customers.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.