如果在 Woocommerce 帐户注册时未选中复选框,则会出现错误

Give error if checkbox isn't selected on Woocommerce Account registration


我正在尝试在 Woocommerce 用户帐户注册表中添加一些自定义字段。
我已经成功添加了名字和姓氏,但我正在尝试添加 T&C 协议复选框,如果未选中它,则不会让用户继续注册。
我已经设法正确显示它,但我无法进行验证。 我做错了什么?

PS。只是想知道,是否可以在不需要重新加载页面的情况下启用这些检查?
谢谢
我使用的代码:

add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );

function cbi_custom_woo_account_registration() {
    ?>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
    <label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
    </p>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
    <label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
    </p>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
    <label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" value="<?php if ( ! empty( $_POST['accept_terms'] ) ) esc_attr_e( $_POST['accept_terms'] ); ?>" />
    </p>

    <div class="clear"></div>

    <?php
}

// 2. VALIDATE FIELDS

add_filter( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );

function cbi_validate_custom_reg_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
        $errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
    }
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
        $errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
    }
    if ( isset($_POST['accept_terms']) && $_POST['accept_terms'] == 0 ) {
        $errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
    }
    return $errors;
}

我已经修复了你的代码。

add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );

function cbi_custom_woo_account_registration() {
    ?>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
    <label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
    </p>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
    <label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
    </p>

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
    <label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" <?php if( isset($_POST['accept_terms'])){ echo "checked"; } ?> />
    </p>

    <div class="clear"></div>

    <?php
}

// 2. VALIDATE FIELDS

add_action( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );

function cbi_validate_custom_reg_fields( $errors, $username, $email ) {

    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
        $errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
    }
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
        $errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
    }
    if ( !isset($_POST['accept_terms']) ) {
        $errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
    }
    return $errors;
}