Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active October 30, 2023 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/87a24568a71ec185916bf2048d9d86a1 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/87a24568a71ec185916bf2048d9d86a1 to your computer and use it in GitHub Desktop.
<?php
/**
* Add the Confirm Email Address field to the Billing checkout form
*/
add_filter( 'woocommerce_checkout_fields' , 'wc_custom_add_email_confirm_field_checkout' );
function wc_custom_add_email_confirm_field_checkout( $fields ) {
$fields['billing']['billing_email_confirm'] = array(
'label' => __( 'Confirm Email Address', 'domain' ),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 120,
);
return $fields;
}
/**
* Validate the Confirm Email Address field making sure that it matches the billing email.
*/
add_action( 'woocommerce_after_checkout_validation', 'wc_custom_check_matching_email_address', 10, 2 );
function wc_custom_check_matching_email_address( $data, $errors ) {
$billing_email = sanitize_email( $data['billing_email'] );
$confirm_billing = sanitize_email( $data['billing_email_confirm'] );
if ( ! empty( $confirm_billing ) && $billing_email !== $confirm_billing ) {
$errors->add( 'billing', __( 'Your email address does not match.', 'domain' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment