简体   繁体   English

如何验证woocommerce注册表单的同意条款复选框?

[英]How do I validate agree terms checkbox for woocommerce registration form?

I have stmbled into a bit of a problem with the woocommerce registration form. 我发现woocommerce注册表单存在一些问题。 I wanted to add a agree to the terms checkbox that would actually work. 我想在实际适用的条款复选框中添加一个同意。

I added the code that shows the checkbox in form-login.php (from the templates/my account folder). 我在form-login.php中添加了显示复选框的代码(来自模板/我的帐户文件夹)。

I tried to modify the code in wc-user-functions.php and class-wc-form-handler.php from includes folder and now it does not recognize when the user checks the box. 我尝试从include文件夹修改wc-user-functions.php和class-wc-form-handler.php中的代码,现在当用户选中该框时它无法识别。 It gives me the Please accept terms error every time regardless if i check the box or not. 无论我是否选中此框,每次都给我“请接受条款”错误。

Does anyone know where is my mistake? 有人知道我的错误在哪里吗?

form-login.php added code form-login.php添加了代码

<p class="form-row form-row-wide">
            <label for="accept_terms" class="inline"><span class="required"></span></label>
            <input name="accept_terms" type="checkbox" id="accept_terms" value="empty";/> 
            <?php _e( 'Am citit si sunt de acord cu' ); ?> <a href="<?php echo "http://................." ?>"><?php _e( 'termenii si conditiile', 'woocommerce' ); ?></a>
        </p>

wc-user-functions.php added code wc-user-functions.php添加了代码

// Check the accept terms
if ( empty( $accept_terms ) )  {
    return new WP_Error( 'registration-error-accept-terms', __( 'Please accept terms', 'woocommerce' ) );
} 

wc-class-form-handler.php modified code wc-class-form-handler.php修改代码

public static function process_registration() {
    if ( ! empty( $_POST['register'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-register' ) ) {
        $username = 'no' === get_option( 'woocommerce_registration_generate_username' ) ? $_POST['username'] : '';
        $password = 'no' === get_option( 'woocommerce_registration_generate_password' ) ? $_POST['password'] : '';
        $email    = $_POST['email'];
    $accept_terms = $_POST['accept_terms'];

        try {
            $validation_error = new WP_Error();
            $validation_error = apply_filters( 'woocommerce_process_registration_errors', $validation_error, $username, $password, $email, $accept_terms);

            if ( $validation_error->get_error_code() ) {
                throw new Exception( $validation_error->get_error_message() );
            }

            // Anti-spam trap
            if ( ! empty( $_POST['email_2'] ) ) {
                throw new Exception( __( 'Anti-spam field was filled in.', 'woocommerce' ) );
            }

            $new_customer = wc_create_new_customer( sanitize_email( $email ), wc_clean( $username ), $password);

            if ( is_wp_error( $new_customer ) ) {
                throw new Exception( $new_customer->get_error_message() );
            }

            if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $new_customer ) ) {
                wc_set_customer_auth_cookie( $new_customer );
            }

            wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'myaccount' ) ) );
            exit;

        } catch ( Exception $e ) {
            wc_add_notice( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . $e->getMessage(), 'error' );
        }
    }
}

Any help would be greatly appreciated! 任何帮助将不胜感激!

First, you should not edit core WooCommerce files. 首先,您不应编辑核心WooCommerce文件。 You will lock yourself into the code and never be able to update, which is bad. 您将自己锁定在代码中,并且永远无法更新,这很糟糕。 And there's really no need and WooCommerce is littered with hooks and filters which permit you to make modifications from your own themes/plugins. 而且确实没有必要,WooCommerce充满了钩子和过滤器,使您可以根据自己的主题/插件进行修改。 The following should go in your theme's functions.php or preferably into its own custom plugin. 以下内容应该放在主题的functions.php或者最好放入其自己的自定义插件中。

This first part will add the terms checkbox. 第一部分将添加术语复选框。 If you wish to show the exact same terms field as on the checkout page, then you can just use the same template. 如果您希望显示与结帐页面上完全相同的术语字段,则可以使用相同的模板。

function so_33122634_add_field_to_registration(){
    wc_get_template( 'checkout/terms.php' );
}
add_action( 'woocommerce_register_form', 'so_33122634_add_field_to_registration' );

Then on the woocommerce_process_registration_errors we can check the $_POST and throw exceptions if it isn't there. 然后在woocommerce_process_registration_errors上,我们可以检查$_POST并抛出异常(如果不存在)。 WooCommerce will collect all the exceptions and show the appropriate error messages. WooCommerce将收集所有异常并显示适当的错误消息。

function so_33122634_validation_registration( $errors, $username, $password, $email ){
    if ( empty( $_POST['terms'] ) ) {
        throw new Exception( __( 'You must accept the terms and conditions in order to register.', 'text-domain' ) );
    }
    return $errors;
}
add_action( 'woocommerce_process_registration_errors', 'so_33122634_validation_registration', 10, 4 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM