Make A WooCommerce Guest Confirm Their Email Address During Checkout

By James LePage, CodeWP Founder
November 22, 2022

Many times, guests buying from a WooCommerce shop enter the wrong email, leading them to getting locked out of a newly created account and not receive order updates. A simple way to fix this is to modify the WooCommerce checkout to require guests (but not logged in customers) to confirm their email before checkout.

We used CodeWP to generate two snippets that work together to ensure that guest purchasers confirm their email during the checkout process:

  1. Adds a new billing email confirmation field
  2. Makes sure the fields both match before the user can checkout

This only shows up on the checkout if the user isn't logged in (ie. is a guest).

Add The New Input

This code leverages the woocommerce_form_field function to add a new field labeled "Confirm Email" to the checkout.

add_action( 'woocommerce_after_checkout_billing_form', 'add_billing_email_2_field' ); function add_billing_email_2_field( $checkout ) { if ( ! is_user_logged_in() ) { woocommerce_form_field( 'billing_email_2', array( 'type' => 'email', 'class' => array('form-row-wide'), 'label' => __('Confirm Email'), 'placeholder' => __('Confirm Email'), 'required' => true, ), $checkout->get_value( 'billing_email_2' )); } } Generation ID:cmpl-6FXMu9ickD8eC5gOdC17vpA8d2fKb

Here's what happens when the code is installed.

Confirm That The Emails Match

This code snippet takes the value from both input fields, and confirms that both values match. Keep in mind, the "real" email address input will also confirm that the entry is actually an email, so we don't need to do that for the confirmation input.

add_action( 'woocommerce_checkout_process', 'wc_confirm_billing_email_checkout' ); function wc_confirm_billing_email_checkout() { if ( ! is_user_logged_in() ) { $billing_email = filter_input( INPUT_POST, 'billing_email', FILTER_VALIDATE_EMAIL ); $billing_email_2 = filter_input( INPUT_POST, 'billing_email_2', FILTER_VALIDATE_EMAIL ); if ( $billing_email != $billing_email_2 ) { wc_add_notice( __( 'Please make sure your emails match.' ), 'error' ); } } } Generation ID:cmpl-6FXIDOUtMMUvmxU5D9TSIKTHQGBy8

If the emails don't match, we use the built in wc_add_notice function to trigger the message below.

The error notice also causes the checkout process to end, meaning no order is able to be processed until both email match.

You'll also note that ! is_user_logged_in() both are wrapped in this conditional, which means that both snippets will only run when the user isn't logged in. If we confirm all guest emails, we can safely assume that those with accounts have entered the right information, meaning that it can be disabled for customers who are already logged in.

Snippets In This Tutorial Were Generated With CodeWP
PHP, JS or jQuery
AI models trained for WordPress & popular plugins
Unlimited generations
100% free trial
Start For Free