我能够非常轻松地在Woocommerce结帐和注册新帐户时添加确认密码字段。然而,试图通过电子邮件地址实现这一目标几乎是不可能的。
我使用相同的编码方式来确认密码,但这会在网站上产生错误。我尝试了多种方法但没有成功。
// ----- validate email match on the registration page
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $email, $email2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Email address do not match.', 'woocommerce' ) );
}
return $reg_errors;}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
// ----- add a confirm email fields match on the registration page
function wc_register_form_email_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_email2"><?php _e( 'Confirm Email', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="input-text" name="email2" id="reg_email2" value="<?php if ( ! empty( $_POST['email2'] ) ) echo esc_attr( $_POST['email2'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form', 'wc_register_form_email_repeat' );
// ----- Validate confirm email field match to the checkout page
function lit_woocommerce_confirm_email_validation( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_email'], $posted['account_confirm_email'] ) !== 0 ) {
wc_add_notice( __( 'Emails do not match.', 'woocommerce' ), 'error' );
}
}
}
add_action( 'woocommerce_after_checkout_validation', 'lit_woocommerce_confirm_email_validation', 10, 2 );
// ----- Add a confirm email field to the checkout page
function lit_woocommerce_confirm_email_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_email' ) == 'no' )
{
$fields = $checkout->get_checkout_fields();
$fields['account']['account_confirm_email'] = array(
'type' => 'email',
'label' => __( 'Confirm email', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Email', 'placeholder', 'woocommerce' )
);
$checkout->__set( 'checkout_fields', $fields );
}
}
add_action( 'woocommerce_checkout_init', 'lit_woocommerce_confirm_email_checkout', 10, 1 );}