skip to Main Content

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?

2

Answers


  1. Have you tried it without this?

    if( is_checkout() ) {
        return $errors;
    }
    

    Since the filter is a registration filter, it seems like that code is unnecessary, since it wouldn’t be called on checkout. Try commenting out that block then testing the registration again. If it works, then test the checkout process as well to make sure this didn’t break it.

    The return $errors; will end the function, and the error won’t be added. Of course, I would assume is_checkout() would return false if they’re registering, but maybe it’s returning true on the registration for some reason?

    Anyways, just something to try.

    Login or Signup to reply.
  2. I have tested your code on test website, and it works for me, the error notice is displayed when the checkbox is not checked…

    Now there are some missing things in the last function where you declare 3 arguments in the add_filter() part, so 2 are missing from it. Also I have simplified your code

    Here is just the revisited code for the last function:

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

    Now I am not sure that this will solve your problem, as the page reloads on your website when submitted, so the error message has no time to be displayed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search