最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Confirm Password on checkout page not working in woocommerce 3.0.1

programmeradmin0浏览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.

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.

Share Improve this question edited Jan 11, 2019 at 11:21 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 12, 2017 at 7:04 Husain AhmedHusain Ahmed 731 silver badge13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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 );

I had an issue on latest WordPress 5.6 and Woocommerce 4.8. The call in the line below is deprecated.

$fields = $checkout->get_checkout_fields();

This is by now a working version. But the $_POST access is not the best solution. A more clean way would be to access the password_confirm value from the $posted variable in the last function sn_woocommerce_confirm_password_validation.

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

        woocommerce_form_field(
        'account_password_confirm',
            array(
                'type'              => 'password',
                'label'             => __( 'Passwort confirmation', 'woocommerce' ),
                'required'          => true,
                'placeholder'       => _x( 'Passwort repeat', 'placeholder', 'woocommerce' )
            ),
             $checkout->get_value( 'account_password_confirm' )
        );

    }
}
//add_action( 'woocommerce_checkout_init', 'sn_woocommerce_confirm_password_checkout', 10, 1 );
add_action( 'woocommerce_after_order_notes', 'sn_woocommerce_confirm_password_checkout', 10, 1 );

function sn_woocommerce_save_password_confirm( $order_id ) {
    if ( ! empty( $_POST['account_password_confirm'] ) ) {
            update_post_meta( $order_id, 'account_password_confirm', sanitize_text_field( $_POST['account_password_confirm'] ) );
        }
}
add_action( 'woocommerce_checkout_update_order_meta', 'sn_woocommerce_save_password_confirm');

/**
 * Validate that the two password fields match.
 */
function sn_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'], $_POST['account_password_confirm'] ) !== 0 ) {
            wc_add_notice( __( 'Passwords are not the same.', 'woocommerce' ), 'error' );
        }
    }
}
add_action( 'woocommerce_after_checkout_validation', 'sn_woocommerce_confirm_password_validation', 10, 2 );

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;
}
发布评论

评论列表(0)

  1. 暂无评论