Hi,
I would like to add a confirm email and confirm password field to Woocommerce checkout.
I would like each of these field to be just after the email (for the confirm email) and password field (for the confirm password)
Thank you !
Kyle answers:
Here is the code for confirming email (goes in functions.php):
add_filter( 'woocommerce_checkout_fields' ,'add_checkout_field' );
add_filter( 'default_checkout_billing_email-2', 'default_field_value' , 10, 2 );
add_filter( 'woocommerce_process_checkout_field_billing_email-2' , 'validate_email_address' );
function add_checkout_field( $fields = array() ) {
$fields['billing']['billing_email-2'] = array(
'label' => __( 'Confirm Email Address', 'wc_emailvalidation' ),
'placeholder' => _x( 'Email Address', 'placeholder', 'wc_emailvalidation' ),
'required' => true,
'class' => apply_filters( 'woocommerce_confirm_email_field_class', array( 'form-row-first' ) ),
'clear' => true,
'validate' => array( 'email' ),
);
return $fields;
}
function default_field_value( $value = null, $field = 'billing_email-2' ) {
if ( is_user_logged_in() ) {
global $current_user;
$value = $current_user->user_email;
}
return $value;
}
function validate_email_address( $confirm_email = '' ) {
global $woocommerce;
$billing_email = $woocommerce->checkout->posted['billing_email'];
if( strtolower( $confirm_email ) != strtolower( $billing_email ) ) {
$notice = sprintf( __( '%1$sEmail addresses%2$s do not match.' , 'wc_emailvalidation' ) , '<strong>' , '</strong>' );
if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
$woocommerce->add_error( $notice );
} else {
wc_add_notice( $notice, 'error' );
}
}
return $confirm_email;
}
Kyle comments:
Here is the code for password:
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1 ); function wc_add_confirm_password_checkout( $checkout ) { if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) { $checkout->checkout_fields['account']['account_password2'] = array( 'type' => 'password', 'label' => __( 'Confirm password', 'woocommerce' ), 'required' => true, 'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' ) ); } } // Check the password and confirm password fields match before allow checkout to proceed. add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 ); function wc_check_confirm_password_matches_checkout( $posted ) { $checkout = WC()->checkout; if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) { if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) { wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' ); } } }
Kyle comments:
Sorry here is password again, formatted:
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
gorbatch comments:
Hi Kyle,
Thank you for your answer but it doesnt seems to work.
I send you a url via private message.
Thank you,
Bob answers:
[[LINK href="https://wordpress.org/plugins/woocommerce-email-validation/screenshots/"]]https://wordpress.org/plugins/woocommerce-email-validation/screenshots/[[/LINK]]
[[LINK href="http://gerhardpotgieter.com/2014/02/25/woocommerce-2-1-add-confirm-password-field-at-checkout/"]]http://gerhardpotgieter.com/2014/02/25/woocommerce-2-1-add-confirm-password-field-at-checkout/[[/LINK]]
[[LINK href="https://gist.github.com/kloon/9828306"]]https://gist.github.com/kloon/9828306[[/LINK]]
[[LINK href="https://wordpress.org/support/topic/password-confirmation-in-woocommerce-21x-release"]]https://wordpress.org/support/topic/password-confirmation-in-woocommerce-21x-release[[/LINK]]