在Woocommerce结账表单中启用确认密码

3
我已经在Woocommerce设置中启用了“在“结账”页面上启用客户注册”选项。新客户在下订单时会在网站上注册。没有其他的注册方式。
下单时,有一个密码字段。我想要“确认密码”,我尝试在主题的functions.php文件中使用以下代码,但是确认密码字段没有显示出来。
链接:https://dev.clipcertification.com/checkout/
<?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' );
    }
}
}
?>

问题出在哪里?对我来说,两个代码都可以运行... - LoicTheAztec
1个回答

4
假设在“结账”页面上启用了“客户注册”选项,您可以尝试以下类似的操作:
add_filter( 'woocommerce_checkout_fields' , 'add_confirm_password_checkout_field', 10, 1 );
function add_confirm_password_checkout_field( $fields ) {
    if ( get_option( 'woocommerce_registration_generate_password' ) != 'no' )
        return $fields;

    $fields['account']['account_password']['class'] = array('form-row-first');
    $fields['account']['account_password-2'] = array(
        'type' => 'password',
        'label' => __( 'Password confirmation', 'woocommerce' ),
        'required'          => true,
        'placeholder' => _x('Confirmation', 'placeholder', 'woocommerce'),
        'class' => array('form-row-last'),
        'label_class' => array('hidden')
    );
    return $fields;
}

add_action( 'woocommerce_checkout_process', 'confirm_password_checkout_validation' );
function confirm_password_checkout_validation() {
    if ( ! is_user_logged_in() && ( WC()->checkout->must_create_account || ! empty( $_POST['createaccount'] ) ) ) {
        if ( strcmp( $_POST['account_password'], $_POST['account_password-2'] ) !== 0 )
            wc_add_notice( __( "Passwords doesn’t match.", "woocommerce" ), 'error' );
    }
}

代码放在你的活动子主题(或活动主题)的function.php文件中。

已测试并可用。


你会得到类似于这样的东西:

在此输入图像描述

Now if you don't get the field try to remove or to comment this lines in the first hooked function:

 if ( get_option( 'woocommerce_registration_generate_password' ) != 'no' )
        return $fields;

This issue could be related to your theme or others plugin customizations.


快速建议以帮助他人 - 在验证函数的第二个if语句中,添加非空条件链以获得更清晰/更简洁的WooCommerce错误消息:codeif ( ! empty( $_POST['account_password'] ) && ! empty( $_POST['account_password-2'] ) && strcmp( $_POST['account_password'], $_POST['account_password-2'] ) !== 0 )code - user1138

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接