jeudi 7 octobre 2021

Add a second checkbox in WooCommerce registration form

How could I add a second checkbox in the registration form for WooCommerce users?

On the 'my account' page of WooCommerce it gives you the option to log in or register (that's where I need help).

I already have a checkbox established to accept the privacy policy (just below the user, e-mail and password boxes).

This is the hook that I have achieved thanks to this article

add_action( 'woocommerce_register_form', function () {
  
  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'         => 'He leído y acepto la política de privacidad',
      )); 
}, 10);

add_filter( 'woocommerce_registration_errors', function ( $errores, $usuario, $email ) {
    if ( ! (int) isset( $_POST['politica_privacidad_registro'] ) ) {
        $errores->add( 'politica_privacidad_registro_error', 'Tienes que aceptar nuestra política de privacidad' );
    }
return $errores;
}, 10, 3);

But I need another checkbox so that they accept another necessary requirement on the web (age verification). That mandatory age verification checkbox to register, of course, would be accompanied by a small phrase.

I have tried with some hook in the functions.php, but it happens that if you accept the privacy checkbox you could already register even if you do not accept the second mandatory checkbox.

This is the code that best suited what I needed to this post

// To add the form
function add_age_verification() {
    ?>

    <p class="form-row form-row-first">
    <label for="reg_age_verification" class="woocommerce-form__label woocommerce-form__label-for-checkbox"><?php _e( 'age verification', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox" name="age_verification" id="reg_age_verification" value="<?php if ( ! empty( $_POST['age_verification'] ) ) esc_attr_e( $_POST['age_verification'] ); ?>" />
    </p>

    <div class="clear"></div>

    <?php
}
  // to validate the form
function validate_age_verification( $errors, $username, $email ) {
    if ( isset( $_POST['age_verification'] ) && empty( $_POST['age_verification'] ) ) {
        $errors->add( 'age_verification_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
    }
    return $errors;
}

 // to save the form into user meta age_verification
function age_verification_save( $customer_id ) {
    if ( isset( $_POST['age_verification'] ) ) {
        update_user_meta( $customer_id, 'age_verification', sanitize_text_field( $_POST['age_verification'] ) );
   }
}

add_action( 'woocommerce_register_form', 'add_age_verification' );
add_filter( 'woocommerce_registration_errors', 'validate_age_verification', 10, 3 );
add_action( 'woocommerce_created_customer', 'age_verification_save' );

Aside from the incompatibility of hooks, also this second checkbox with the tests that I have carried out appears the phrase in a line and the check box below. It must all go together on one line.

How could I do it and have both hooks work without creating incompatibilities?




Aucun commentaire:

Enregistrer un commentaire