WooCommerce 2.1 Confirm Password Field on CheckoutThe last couple of weeks I have spent a lot of time working on some tutorials to reverse some of the changes introduced in WooCommerce 2.1 to the ways it was in WooCommerce 2.0.

This tutorial is another one of this cases, WooCommerce 2.1 removed the password confirm field and functionality from the checkout page as it was thought that should a customer make a typo in the password field they can easily just reset it via the password reset functionality in WooCommerce.

However if you would still like add this password confirm field to your WooCommerce 2.1 checkout page, good news is this is still possible.

The code below will add an additional field underneath your password field on the checkout page called Confirm Password and when the customer places the order it will check the two password field against each other and give an error message and prohibit checkout if they do not match.

Place the code below in your theme’s functions.php file


<?php
// place the following code in your theme's functions.php file
// Add a second password field to the 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' );
}
}
}
?>

view raw

functions.php

hosted with ❤ by GitHub