0

I have update woo-commerce in latest version 3.0.1 Because of which some of my functions have been closed like confirm password fields on check out page. my function is

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'             => __( 'Verify password', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( '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' );
        }
    }
}

I have added these code on function.php file in current theme. How can i correct my function.

3
+50

Please try the modified code below, working on WordPress 4.7.3 with WooCommerce 3.0.3.

Matt.

/**
 * Add a confirm password field to the checkout registration form.
 */
function o_woocommerce_confirm_password_checkout( $checkout ) {
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {

        $fields = $checkout->get_checkout_fields();

        $fields['account']['account_confirm_password'] = array(
            'type'              => 'password',
            'label'             => __( 'Confirm password', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
        );

        $checkout->__set( 'checkout_fields', $fields );
    }
}
add_action( 'woocommerce_checkout_init', 'o_woocommerce_confirm_password_checkout', 10, 1 );

/**
 * Validate that the two password fields match.
 */
function o_woocommerce_confirm_password_validation( $posted ) {
    $checkout = WC()->checkout;
    if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
        if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
            wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
        }
    }
}
add_action( 'woocommerce_after_checkout_validation', 'o_woocommerce_confirm_password_validation', 10, 2 );
  • Hello Matt Aitch your code is working fine. Thanks. – Husain Ahmed Apr 19 '17 at 5:30
1

I'm trying this on a couple different sites, both are running WordPress 4.9.2. One site is running WooCommerce 3.1.2 and the other is WooCommerce 3.2.1.

I tried using Matt Aitch's code, but for some reason or another, it wasn't working for me. Here is the solution I came up with (all of this code resides in your theme's functions.php file):

// Adds password-confirmation to user registration page (/my-account/)
add_filter( 'woocommerce_register_form', 'pixel_registration_password_repeat', 9 );
// Priority 10 will probably work for you, but in my situation, 
// a reCAPTCHA element loads between the password boxes, so I used 9.
function pixel_registration_password_repeat() {
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="password_confirm"><?php _e( 'Confirm Password', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="password" class="input-text" name="password_confirm" id="password_confirm">
    </p>
    <?php
}


// Adds password-confirmation to user registration during checkout phase
add_filter( 'woocommerce_checkout_fields', 'pixel_checkout_registration_password_repeat', 10, 1 );
function pixel_checkout_registration_password_repeat( $fields ) {
    if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) {
        $fields['account']['account_password_confirm'] = array(
            'type'        => 'password',
            'label'       => __( 'Confirm password', 'woocommerce' ),
            'required'    => true,
            'placeholder' => esc_attr__( 'Confirm password', 'woocommerce' )
        );
    }
    return $fields;
}


// Form-specific password validation step.
add_filter( 'woocommerce_registration_errors', 'pixel_validate_registration_passwords', 10, 3 );
function pixel_validate_registration_passwords( $errors, $username, $email ) {
    global $woocommerce;
    extract( $_POST );

    if ( isset( $password ) || ! empty( $password ) ) {
        // This code runs for new user registration on the /my-account/ page.
        if ( strcmp( $password, $password_confirm ) !== 0 ) {
            return new WP_error( 'registration-error', __( 'Passwords do not match', 'woocommerce' ) );
        }
    } else if ( isset( $account_password ) || ! empty( $account_password ) ) {
        // This code runs for new user registration during checkout process.
        if ( strcmp( $account_password, $account_password_confirm ) !== 0 ) {
            return new WP_error( 'registration-error', __( 'Passwords do not match', 'woocommerce' ) );
        }
    }

    return $errors;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.