简体   繁体   English

验证 WooCommerce 结帐页面中的附加复选框字段

[英]Validation for an additional checkbox field in WooCommerce checkout page

I have added in checkout page a acceptance field below the terms and conditions field.我在结帐页面的条款和条件字段下方添加了一个接受字段。 The new field is about the age validation.新字段是关于年龄验证的。

In frontend, I can see my checkbox but customer should not be able to order if checking this checkbox in not checked.在前端,我可以看到我的复选框,但如果未选中此复选框,客户将无法订购。 This is the generated html code:这是生成的 html 代码:

<?php if ( wc_terms_and_conditions_checkbox_enabled() ) : ?>
            <p class="form-row validate-required">
                <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); // WPCS: input var ok, csrf ok. ?> id="terms" />
                    <span class="woocommerce-terms-and-conditions-checkbox-text"><?php wc_terms_and_conditions_checkbox_text(); ?></span>&nbsp;<span class="required">*</span>
                </label>
                <input type="hidden" name="terms-field" value="1" />
            </p>
        <?php endif; ?>
                        <p class="form-row validate-required">
                <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" required name="validationage"  id="validationage" />
                    <span class="woocommerce-terms-and-conditions-checkbox-text"><?php _e( 'Je certifie avoir + de 18 ans', 'woocommerce'); ?></span>&nbsp;<span class="required">*</span>
                </label>
            </p>

How to enable the validation for this checkbox?如何启用此复选框的验证?

The following will handle your custom checkbox field validation on checkout page:以下将处理结帐页面上的自定义复选框字段验证:

add_action('woocommerce_checkout_process', 'custom_checkbox_checkout_validation');
function custom_checkbox_checkout_validation() {
    $field_id = 'validationage';

    if( ! isset( $_POST[$field_id] ) || empty( $_POST[$field_id] ) )
        wc_add_notice( __("validation text.","woocommerce"), "error" );
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的 functions.php 文件。 It should works.它应该有效。

It would be much better if you use woocommerce_after_checkout_validation hooks, this will let you to be sync with other WooCommerce validation error如果你使用woocommerce_after_checkout_validation钩子会好得多,这会让你与其他 WooCommerce 验证错误同步

add_action( 'woocommerce_after_checkout_validation', function( $data, &$errors ) {
    $field_id = 'validationage';
    if( ! isset( $data[$field_id] ) || empty( $data[$field_id] ) ) {
       $errors->add( 'terms', __( 'validation text.', 'theme-slug' ) );
    }
},10, 2  );

Note: You may need to use woocommerce_checkout_fields filter hook to add custom checkout field.注意:您可能需要使用woocommerce_checkout_fields过滤器钩子来添加自定义结帐字段。

Thanks谢谢

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

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