"Confirm Email" Field on Checkout
Adds a second email field to the classic checkout and blocks the order if the two addresses do not match, to cut down on typos.
Opens WPIDE on
Code
add_filter( 'woocommerce_billing_fields', function ( $fields ) {
$fields['billing_email_confirm'] = array(
'label' => 'Confirm email address',
'type' => 'email',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => isset( $fields['billing_email']['priority'] ) ? $fields['billing_email']['priority'] + 1 : 111,
'validate' => array( 'email' ),
);
return $fields;
}, 30 );
add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) {
$email = isset( $data['billing_email'] ) ? strtolower( trim( (string) $data['billing_email'] ) ) : '';
$confirm = isset( $data['billing_email_confirm'] ) ? strtolower( trim( (string) $data['billing_email_confirm'] ) ) : '';
if ( array_key_exists( 'billing_email_confirm', $data ) && $email !== $confirm ) {
$errors->add( 'validation', 'The email addresses do not match.' );
}
}, 10, 2 );
add_filter( 'woocommerce_checkout_get_value', function ( $value, $input ) {
if ( 'billing_email_confirm' === $input && null === $value && is_user_logged_in() ) {
return wp_get_current_user()->user_email;
}
return $value;
}, 10, 2 );
Notes
Logged-in customers with an email on file see the field pre-filled.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.