vendredi 8 octobre 2021

How to make compatible the two checkboxes on my account page in Woocommerce without getting error on payment page

I have two checkboxes working fine on my Woocommerce account page by using Add a second checkbox in WooCommerce registration form answer code

This works perfectly on the WooCommerce 'My Account' page, but I have encountered an error on the 'Checkout Page' and I don't know if it has something to do with the fact that for my payment page there is also a hook to accept the privacy policy.

This is the hook I have:

add_action( 'woocommerce_review_order_before_submit', 'add_privacy_checkbox', 9 );
function add_privacy_checkbox() {
woocommerce_form_field( 'privacy_policy', 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 have read and accept the privacy policy</a>',
));
}

add_action( 'woocommerce_checkout_process', 'privacy_checkbox_error_message' );
function privacy_checkbox_error_message() {
if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
wc_add_notice( __( ‘You should accept the privacy policy’ ), 'error' );
}
}

In the last check that I have made even removing this hook, the following error that I argue below is still detected:

When you make a purchase (without being registered as a customer) and you arrive at the payment page, you must complete the personal details of the order and just below these boxes there is a check box that gives you the option to create an account. When clicking, the username and password are displayed (the requested data is completed).

At the end of the purchase, even accepting the check boxes of the general conditions of purchase and the privacy policy, the following error appears: 'You have to accept the privacy policy'.

This error only appears when you sign up on the payment page. If you make the payment without registering, no error appears.

I have reviewed another purchase with the same procedure, but removing the hook from my account page and the purchase is made without errors (it does not ask you 'You have to accept the privacy policy') and you register as a user without problems.




Aucun commentaire:

Enregistrer un commentaire