Woocommerce确认密码未显示

时间:2018-03-19 14:37:46

标签: php wordpress forms woocommerce

我已将以下代码添加到我的functions.php文件中,以便在结帐页面上确认密码。

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' );
        }
    }
}

以下是网站结帐页面的链接。您必须将产品添加到购物车,然后返回结帐页面。一旦产品进入购物车并且您在Checkout页面上,您将注意到密码字段已突出显示为红色且没有密码确认字段。这已破了。

http://staging.vawk.ca/checkout/

但是,如果您转到以下网址并执行相同的操作,密码确认就在那里,一切正常。就我而言,所有代码都是相同的,数据库是相同的。

http://jolangreen.com/other/clients/vawk/checkout/

如何获取密码确认以使用http://staging.vawk.ca/checkout/

1 个答案:

答案 0 :(得分:0)

请尝试下面修改后的代码,使用WooCommerce 3.0.3 +处理WordPress 4.7.3+。

logicdigger

**
 * 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 );