Skip to content

Instantly share code, notes, and snippets.

@iMazed
Last active April 7, 2021 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMazed/af4fb06875da0477045a to your computer and use it in GitHub Desktop.
Save iMazed/af4fb06875da0477045a to your computer and use it in GitHub Desktop.
WooCommerce: add password check field to checkout page
// Add snippet to your functions.php file to add a second password field to the WooCommerce checkout page.
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' );
}
}
}
@caysoasesores
Copy link

Hello IMazed, I added this snippet to functions.php and it does not do anything. Is there some other feature I do have to consider?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment