[ad_1]
Solved.
I had this code to add a Privacy policy checkbox to the register form (not the checkout register form):
add_action( 'woocommerce_register_form', function () {
$privacy_policy_url = get_privacy_policy_url();
woocommerce_form_field( 'politica_privacidad_registro', array(
'type' => 'checkbox',
'class' => array('form-row rgpd'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => sprintf( __( 'I have read and agree to the website %s.', 'generatepress-child' ), '<a href="' . esc_url( $privacy_policy_url ) . '" target="_blank">' . __( 'privacy policy', 'woocommerce' ) . '</a>' ),
));
}, 10);
And this to check if the field was selected:
add_filter( 'woocommerce_registration_errors', function ( $errores, $usuario, $email ) {
if ( ! (int) isset( $_POST['politica_privacidad_registro'] ) ) {
$errores->add( 'politica_privacidad_registro_error', __('-- Please read and accept the privacy policy.','generatepress-child') );
}
return $errores;
}, 10, 3);
Now I have changed to this and it’s working:
add_filter('woocommerce_registration_errors', 'custom_registration_errors', 10, 3);
function custom_registration_errors($errors, $username, $email) {
if (is_checkout()) {
return $errors;
}
if (!isset($_POST['politica_privacidad_registro']) || intval($_POST['politica_privacidad_registro']) !== 1) {
$errors->add('politica_privacidad_registro_error', __('Please read and accept the privacy policy.', 'generatepress-child'));
}
return $errors;
}