Reputation: 33
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?
Upvotes: 1
Views: 286
Reputation: 39
#Have you tried it
add_action( 'woocommerce_register_form', 'wtwh_add_registration_privacy_policy', 11 );
function wtwh_add_registration_privacy_policy() {
woocommerce_form_field( 'privacy_policy_reg', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'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' => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
));
}
// Show error if user does not tick
add_filter( 'woocommerce_registration_errors', 'wtwh_validate_privacy_registration', 10, 3 );
function wtwh_validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
$errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
}
}
return $errors;
}
Upvotes: 1
Reputation: 254448
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.
Upvotes: 2
Reputation: 729
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.
Upvotes: 1