1

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
genreeds48
  • 33
  • 7
  • Aztec - you very cool man! Thank you for correcting my posts. My English is made in China. Currently I: 1) I'm fixing a bug. On the shopping cart tab, product images disappear when you change the quantity of goods. 2) I'm looking for a way to display an error message (when the "Policies" checkbox is not selected - account registration). 3) Additionally (if it is difficult, then it is not necessary) - the ability to move the errors of the "checkboxes" to arbitrary places – genreeds48 Jun 27 '23 at 17:01
  • Dear Aztec! Hello. This is my last request (probably almost the very last). I almost completely debugged the site and fixed bugs. With your help. It remains quite a bit and you can fill it with content, goods. There is one unpleasant moment - I described it in a new topic. I'll never figure it out myself... Please help https://stackoverflow.com/questions/76583040/problem-with-deleting-an-item-from-the-cart-cross-it-kind-of-works-but-it-do – genreeds48 Jun 29 '23 at 16:52

3 Answers3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you! Yes, I need to figure out why I'm not getting a notification. Maybe I messed up something in the style.сss – genreeds48 Jun 27 '23 at 17:27
  • The page reloads on your website when submitted, so the error message has no time to be displayed. – LoicTheAztec Jun 27 '23 at 17:31
  • I changed and customized the theme quite a lot. Very little remains of the original version. I used the {display:none} function very often – genreeds48 Jun 27 '23 at 17:32
  • Please tell me how can I solve this problem? I have a similar problem on the page - "Cart". When you update the quantity of a product, the image of the product disappears. When you manually refresh the page - the picture appears ... – genreeds48 Jun 27 '23 at 17:35
  • 1
    Is not possible to guess… You are making too many customizations everywhere. – LoicTheAztec Jun 27 '23 at 17:37
  • Okay, I'll try to figure out the plugins and the file style.css. Regarding the "disappearance of the picture" in the basket - such a problem was originally. Before customization .css... – genreeds48 Jun 27 '23 at 17:43
  • If I manually refresh the page - the picture will appear. I wanted to implement with a script. Let the page reload itself. But it's a crutch – genreeds48 Jun 27 '23 at 17:46
  • 1
    Aztec! I found the reason. Understood. The problem was in the stylesheet - style.css. I disabled this block :D :))) I'll turn it back on right now. P.S. pictures also not showing in the cart. When you change the number of goods – genreeds48 Jun 27 '23 at 17:55
  • 1
    Now I will sort out why when you change the quantity of goods, the images disappear in cart page. It's strange that there are so many bugs :) But it's a good exercise for the brain. By the way, the hosting is completely mine. Web-server running on raspberry pi 4, ubuntu server )) nginx – genreeds48 Jun 27 '23 at 18:02
  • I also figured out the picture. The plugin "Jetpack Boost" turned out to be the culprit. Namely, the add-on - "Lazy Image Loading". I recommend not to turn it on, otherwise my CART will break – genreeds48 Jun 27 '23 at 18:18
  • Loic hi, bro. I solved those problems with "JQuery". Not just solved, but finalized the script. So that when you delete a cross from the basket, the mini-basket is automatically cleared and updated. It was necessary to configure the nginx server. It was incredibly difficult and long, but it worked out. By the way, I added a solution there and told what to do – genreeds48 Jul 05 '23 at 16:08
  • Why am I writing to you again? I need one more little help. Remember you helped me make the "policy" checkbox for placing an order? So, Can you help me make it so that the link is not just clicked. And so that without going to another page, a "slide" with a policy unfolds? The fact is that "Delivery and Condition" unfolds as a slide. And politicians - just go to a new page. I must either do the same, or remove the slider from the "delivery rules" – genreeds48 Jul 05 '23 at 16:10
  • Add the item to your shopping cart and checkout. And try clicking the "policies" checkbox, and then clicking the "delivery rules" checkbox. They open differently. And I would like politicians to also leave as a slide .... https://genreeds.ru – genreeds48 Jul 05 '23 at 16:13
  • Here is an old discussion thread https://stackoverflow.com/questions/76551160/add-checkbox-to-policy-line-woocommerce-botiga-theme-template – genreeds48 Jul 05 '23 at 16:14
  • On the "Registration account" page - let the "policy" checkbox open a new page. But when placing an order, I do not know how to make a slide. Or how to remove the slide from the "delivery rules". But ideally, you need to make a slide for both the "policy" and the "delivery rules" – genreeds48 Jul 05 '23 at 16:16
  • New thread - https://stackoverflow.com/questions/76622537/botiga-theme-woocommerce-checkout-page-need-to-add-a-slider-instead-of-follow – genreeds48 Jul 05 '23 at 17:04
  • Loic, hi friend! Can you help me pls? It is very easy issue https://stackoverflow.com/questions/76648198/refinement-of-the-javascript-jquery-mini-basket-update-script – genreeds48 Jul 10 '23 at 08:18
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.

Stevish
  • 734
  • 5
  • 17
  • Ok, I'll try. The thing is, the code itself works just fine. All I need is that when an error occurs, a message is also displayed. And the error is triggered, but the message is not ... – genreeds48 Jun 26 '23 at 17:15
  • No. Does not help. The error text is not displayed. P.S. here's this one"$errors->add( 'privacy_policy_reg_error', 'Вам нужно принять политику конфиденциальности.' );" – genreeds48 Jun 26 '23 at 17:40
1

#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;
}
Amit Gupta
  • 19
  • 3