lundi 3 août 2020

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:

<?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?




Aucun commentaire:

Enregistrer un commentaire