mardi 27 juin 2023

Custom checkbox validation error notice issue in Woocommerce registration

In WooCommerce, I am using the following code to add a custom "privacy" checkbox to the customer registration form:

// Addind a checkbox to registration form
add_action( 'woocommerce_register_form', 'add_privacy_checkbox_registration' );
function add_privacy_checkbox_registration() {
    $checkbox_text = sprintf( '%s <a href="%s"><strong>%s</strong></a>', __( 'Я прочитал и согласен с' ), 
    esc_url( site_url('/politic-conf/') ), __( 'политикой конфиденциальности' ) );
    ?>
    <div class="woocommerce-privacy-policy-wrapper">
        <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="privacy_policy" <?php checked( false, true ); ?> id="privacy-policy" />
                <span class="woocommerce-privacy-policy-checkbox-text"><?php echo $checkbox_text; ?></span> <abbr class="required" title="<?php esc_attr_e( 'required', 'woocommerce' ); ?>">*</abbr>
            </label>
            <input type="hidden" name="policy-field" value="1" />
        </p>
    </div>
    <?php
}

// The validation
add_filter( 'woocommerce_registration_errors', 'privacy_checkbox_registration_validation', 10, 3 );
function privacy_checkbox_registration_validation( $errors ) {
 
    if( is_checkout() ) {
        return $errors;
    }
 
    if ( empty( $_POST[ 'privacy_policy' ] ) ) {
        $errors->add( 'privacy_policy_reg_error', 'Вам нужно принять политику конфиденциальности.' );
    }
 
    return $errors;
 
}

The code works. The checkbox has been added. The checkbox logic works. That is, if you do not check the box, there will be no user registration.

But there is a problem. The error text is not displayed on the screen if the checkbox is not clicked...

Here is the page of my site with the problem - page with issue

Any ideas?




Aucun commentaire:

Enregistrer un commentaire