如何在 WooCommerce 注册页面中添加确认密码字段? [英] How to Add Confirm Password Field In WooCommerce Registration Page?
问题描述
我正在尝试添加代码
<?php
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}
?>
在 function.php 上但不起作用...任何关于如何在 woocommerce/myaccout/form-login.php 上添加密码构造字段的信息
on function.php but not working... Any on tell how to add the password conformation field on woocommerce/myaccout/form-login.php
推荐答案
作为 John 的回答的后续:
挂钩 woocommerce_checkout_init
在 Woocommerce 3.x 中不再有效,因为他们引入了一个魔法 getter,它只会返回原始的 $checkout->checkout_fields['account']
字段,没有 account2
.我不知道这是功能还是错误.
Hooking to woocommerce_checkout_init
no longer works in Woocommerce 3.x, as they introduced a magic getter that will only return the original $checkout->checkout_fields['account']
field, without account2
. I don't know if this is a feature or a bug.
对于 Woocommerce 3.x,正确的做法是使用过滤器.验证保持不变.
For Woocommerce 3.x, the correct way to do it is to use a filter. The validation stays the same.
// Add a second password field to the checkout page in WC 3.x.
add_filter( 'woocommerce_checkout_fields', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout_fields ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
return $checkout_fields;
}
// 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' );
}
}
}
这篇关于如何在 WooCommerce 注册页面中添加确认密码字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!