Hướng dẫn thêm Confirm Password trong Registration Page và Checkout Page

25 Tháng Sáu, 2024

Tình trạng spam tài khoản, hay khách hàng không nghiêm túc trong việc tạo tài khoản để spam đơn hàng. Mình khuyến khích các bạn thêm một field nhập nữa là “Nhập lại mật khẩu”.

Khá là đơn giản, bạn chỉ cần việc copy các đoạn sau đây vào file function.php là được. Ở đây có 2 trường hợp, mình sẽ tách ra làm 2 giúp các bạn dễ hiểu.

Thêm Confirm Password trong Registration Page

<?php
// ----- Kiểm tra password ở trang đăng ký Woocommerce
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', __( 'Mật khẩu không trùng nhau.', 'woocommerce' ) );
    }
    return $reg_errors;
}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
// ----- Thêm field Nhập lại mật khẩu ở trang đăng ký Woocommerce
function wc_register_form_password_repeat() {
    ?>
    <p class="form-row form-row-wide">
        <label for="reg_password2"><?php _e( 'Nhập lại mật khẩu', '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
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );

Thêm Confirm Password trong Checkout Page

// ----- Kiểm tra password ở trang thanh toán Woocommerce
function lit_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( __( 'Mật khẩu không trùng nhau.', 'woocommerce' ), 'error' ); 
        }
    }
}
add_action( 'woocommerce_after_checkout_validation', 'lit_woocommerce_confirm_password_validation', 10, 2 );
// ----- Thêm field Nhập lại mật khẩu ở trang thanh toán Woocommerce
function lit_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'             => __( 'Nhập lại mật khẩu', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( 'Nhập lại mật khẩu', 'placeholder', 'woocommerce' )
        );
        $checkout->__set( 'checkout_fields', $fields );
    }
}
add_action( 'woocommerce_checkout_init', 'lit_woocommerce_confirm_password_checkout', 10, 1 );

Đây là kết quả, có gì không hiểu hay không được, bạn hãy bình luận ở dưới nhé!

Kết quả sau khi thêm Code
Kết quả sau khi thêm Code
guest
0 Góp ý
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận
Tin tức liên quan
Save/Load dữ liệu trong Unity
Save/Load dữ liệu có lẽ là điều quá quen thuộc với những người đã từng chơi game. Bài viết này mình sẽ hướng dẫn các bạn cách dựng một...
Các hàm cơ bản của Monobehaviour Unity: Update, LateUpdate, FixedUpdate
Với nhiều bạn mới làm quen với Unity, thì việc khi nào dùng Update, LateUpdate, FixedUpdate thường là khá mơ hồ. Thêm vào đó, ba hàm này ảnh hưởng...
C# Extension trong Unity: Delay Action Coroutine
Việc chờ một khoảng thời gian rồi gọi một (hoặc nhiều) hàm trong một project thì cách đơn giản nhất là sử dụng Coroutine. Cách thông thường nhất là...