The rewritten title of the custom checkbox validation error prompt in Woocommerce registration is: Solve the problem of custom checkbox validation error prompt in Woocommerce registration
P粉318928159
P粉318928159 2023-09-03 12:06:38
0
2
540
<p>In WooCommerce, I added a custom "Privacy" checkbox to the customer registration form using the following code: </p> <pre class="brush:php;toolbar:false;">//Add 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 } // verify 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; } </pre> <p>The code is valid. Checkbox added. The checkbox logic works too. This means that if you do not check the box, user registration will not be possible. </p> <p>But there is a problem. If the checkbox is not clicked, the error text will not appear on the screen...</p> <p>This is the page in question on my site - Problem Page</p> <p>Any ideas? </p>
P粉318928159
P粉318928159

reply all(2)
P粉464088437

Have you tried it without this?

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

Since the filter is a registered filter, it seems like that piece of code is unnecessary since it won't be called at checkout. Try commenting out that code block and test registration again. If it works, test the checkout process as well to make sure it doesn't go wrong.

return $errors;will end the function and errors will not be added. Of course, I would assume that is_checkout() would return false on registration, but maybe on registration it returns true for some reason?

In short, it’s just an attempt.

P粉231112437

I have tested your code on a test site and for me it works fine, it shows an error when the checkbox is unchecked...

Now there is something missing in the last function, you declared 3 parameters in the add_filter() part, so 2 are missing. I also simplified your code

Here is just the modified code of the last function:

// 验证
add_filter( 'woocommerce_registration_errors', 'privacy_checkbox_registration_validation', 999, 3 );
function privacy_checkbox_registration_validation( $errors, $username, $email ) {
    // 不在结账页面
    if( ! is_checkout() && empty( $_POST[ 'privacy_policy' ] ) ) {
        $errors->add( 'privacy_policy_error', 'Вам нужно принять политику конфиденциальности.' );
    }
    return $errors;
}

Now I'm not sure if this solves your problem since on your site the page reloads after submission so the error message doesn't have time to show up.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template