Banner

Add Email Confirmation to Checkout

Add an email verification field on the checkout page.

PHP
/**
 * Snippet Name:     Add Email Confirmation to Checkout
 * Snippet Author:   coding-bunny.com
 * Description:      Add an email verification field on the checkout page.
 */
 
 /* Add "Confirm Email" field on Checkout */
add_filter( 'woocommerce_checkout_fields', 'cb_verifica_email' );

function cb_verifica_email( $fields ) {
    // Modify the billing email field to adjust its class for styling
    $fields['billing']['billing_email']['class'] = array( 'form-row-first' );

    // Add a new field for "Confirm Email" to the billing section
    $fields['billing']['billing_em_ver'] = array(
       'label'   => 'Confirm Email Address', // Change label to English
       'required' => true, // Make this field required
       'class'    => array( 'form-row-last' ), // Adjust styling for the second field
       'clear'    => true, // Clear any previous float or alignment issues
       'priority' => 110, // Set the field position
    );
    
    return $fields; // Return the modified fields
}

/* Validate that both email addresses match on Checkout */
add_action( 'woocommerce_checkout_process', 'cb_email_match' );

function cb_email_match() {
    // Sanitize the emails to ensure they are valid and safe
    $email1 = isset( $_POST['billing_email'] ) ? sanitize_email( $_POST['billing_email'] ) : '';
    $email2 = isset( $_POST['billing_em_ver'] ) ? sanitize_email( $_POST['billing_em_ver'] ) : '';

    // Check if the emails do not match
    if ( $email2 !== $email1 ) {
        // Add an error notice if the emails don't match
        wc_add_notice( 'The email addresses do not match.', 'error' );
    }
}

How To Implement This Solution?

Leave a Reply

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny