In some cases, it’s essential to ensure that customers enter their email addresses accurately during the checkout process to avoid communication issues. One way to achieve this is by adding a confirm email address field to the WooCommerce checkout form. In this tutorial, we’ll walk through the steps to integrate this feature into your checkout page.
/**
* Snippet Name: Add confirm email address field to WooCommerce checkout form
* Snippet Author: wpsnippets.dev
*/
// Add confirm email address field to WooCommerce checkout form
function wps_custom_confirm_email_address_field( $checkout ) {
echo '<div id="wps_confirm_email_field">';
woocommerce_form_field( 'confirm_email', array(
'type' => 'email',
'class' => array('form-row-wide'),
'label' => __('Confirm Email Address'),
'placeholder' => __('Confirm Email Address'),
'required' => true,
), $checkout->get_value( 'confirm_email' ));
echo '</div>';
}
add_action( 'woocommerce_after_checkout_billing_form', 'wps_custom_confirm_email_address_field', 10 );
// Validate confirm email address field
function wps_custom_validate_confirm_email_address() {
if ( $_POST['email'] !== $_POST['confirm_email'] ) {
wc_add_notice( __('<b>Billing Confirm Email</b> don\'t match <b> Billing Email</b>'), 'error' );
}
}
add_action('woocommerce_checkout_process', 'wps_custom_validate_confirm_email_address');
Code Explanation
- The function wps_custom_confirm_email_address_field adds a confirm email address field to the WooCommerce checkout page.
- This function hooks with woocommerce_after_checkout_billing_form, which is triggered after the billing form in the WooCommerce checkout page.
- The function wps_custom_validate_confirm_email_address validates the confirm email address field during the checkout process. It checks if the confirmed email address matches the entered email address during the checkout process. If not, it adds an error notice.
- The hook woocommerce_checkout_process is triggered during the checkout process, specifically after the checkout form is submitted.