P粉4640884372023-09-04 11:32:09
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粉2311124372023-09-04 10:04:20
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.