在WooCommerce 3.0.1中确认结账页面上的密码不起作用

时间:2017-04-12 作者:Husain Ahmed

我已经在最新版本3.0.1中更新了woo commerce,因为我的一些功能已经关闭,如签出页面上的确认密码字段。我的职能是

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\' );
        }
    }
}
我在函数中添加了这些代码。当前主题中的php文件。如何更正功能。

3 个回复
最合适的回答,由SO网友:Matt Aitch 整理而成

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

马特。

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

SO网友:James Miller

我在最新的WordPress 5.6和Woocommerce 4.8上有一个问题。下面一行中的调用已弃用。

$fields = $checkout->get_checkout_fields();
现在这是一个工作版本。但是$_POST 访问不是最佳解决方案。更干净的方法是访问password_confirm 来自的值$posted 最后一个函数中的变量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 );

SO网友:Canowyrms

我在两个不同的网站上尝试了这一点,它们都运行WordPress 4.9.2。一个站点运行WooCommerce 3.1.2,另一个站点运行WooCommerce 3.2.1。

我尝试使用Matt Aitch的代码,但出于某种原因,它对我不起作用。下面是我提出的解决方案(所有这些代码都驻留在主题的functions.php文件中):

// 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;
}

相关推荐