Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'register_form', 'adding_custom_registration_fields' );
- function adding_custom_registration_fields( ) {
- echo '<div class="form-row form-row-wide"><label for="reg_persontype">'.__('Person Type', 'woocommerce').' <span class="required">*</span></label>
- <select name="persontype" id="reg_persontype" class="select" value="'.esc_attr($_POST['persontype']).'" >
- <option value="0" >Seleziona</option>
- <option value="1" >Utente</option>
- <option value="2" >Studente</option>
- <option value="3" >Scuola</option>
- </select>';
- echo '<div class="form-row form-row-first" id="divfirst"><label for="reg_firstname">'.__('First Name', 'woocommerce').' <span class="required">*</span></label>
- <input type="text" class="input-text" name="firstname" id="reg_firstname" value="'.esc_attr($_POST['firstname']).'" /></div>';
- echo '<div class="form-row form-row-last" id="divlast"><label for="reg_lastname">'.__('Last Name', 'woocommerce').' <span class="required">*</span></label>
- <input type="text" class="input-text" name="lastname" id="reg_lastname" value="'.esc_attr($_POST['lastname']).'" /></div>';
- echo '<div class="form-row form-row-wide" id="billing_cpf_field"><label for="reg_codicefiscale">'.__('Codice Fiscale', 'woocommerce').' <span class="required">*</span></label>
- <input type="text" class="input-text" name="codicefiscale" id="reg_codicefiscale" value="'.esc_attr($_POST['codicefiscale']).'" /></div>';
- echo '<div class="form-row form-row-wide" id="billing_company_field"><label for="reg_azienda">'.__('Azienda', 'woocommerce').' <span class="required">*</span></label>
- <input type="text" class="input-text" name="azienda" id="reg_azienda" value="'.esc_attr($_POST['azienda']).'" /></div>';
- echo '<div class="form-row form-row-wide" id="billing_cnpj_field"><label for="reg_iva">'.__('P. Iva', 'woocommerce').' <span class="required">*</span></label>
- <input type="text" class="input-text" name="iva" id="reg_iva" value="'.esc_attr($_POST['iva']).'" /></div>';
- echo '</div>';
- ?>
- <script>
- jQuery(document).ready(function($) {
- // Required fields.
- $('#billing_company_field label, #billing_cpf_field label, #billing_cnpj_field label').append(' <abbr class="required" title="obrigatΓ³rio">*</abbr>');
- // Hide and show cpf and cnpj fields
- function personTypeFields(current) {
- $('#divfirst').hide();
- $('#divlast').hide();
- $('#billing_cpf_field').hide();
- $('#billing_company_field').hide();
- $('#billing_cnpj_field').hide();
- if (current == 1) {
- $('#divfirst').show();
- $('#divlast').show();
- $('#billing_cpf_field').show();
- }
- if (current == 2) {
- $('#divfirst').show();
- $('#divlast').show();
- $('#billing_cpf_field').show();
- }
- if (current == 3){
- $('#billing_company_field').show();
- $('#billing_cnpj_field').show();
- }
- }
- personTypeFields($('#reg_persontype').val());
- $('#reg_persontype').on('change', function() {
- var current = $(this).val();
- personTypeFields(current);
- });
- });
- </script>
- <?php
- }
- //Validation registration form after submission using the filter registration_errors
- add_filter('registration_errors', 'registration_errors_validation', 10,3);
- function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
- global $woocommerce;
- extract($_POST); // extracting $_POST into separate variables
- if($firstname == '' ) {
- $woocommerce->add_error( __( '<strong>Nome</strong> obbligatorio', 'woocommerce' ) );
- }
- if($lastname == '' ) {
- $woocommerce->add_error( __( '<strong>Cognome</strong> obbligatorio', 'woocommerce' ) );
- }
- if($persontype == 0 ) {
- $woocommerce->add_error( __( '<strong>Tipo di Persona</strong> obbligatorio', 'woocommerce' ) );
- }
- if ( $persontype == 1 && ! $codicefiscale ) {
- $woocommerce->add_error( __( '<strong>Codice Fiscale</strong> obbligatorio', 'woocommerce' ) );
- }
- /*
- if ( $persontype == 2 && ! $azienda ) {
- $woocommerce->add_error( __( '<strong>Azienda</strong> obbligatorio', 'woocommerce' ) );
- }
- if ( $persontype == 2 && ! $iva ) {
- $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
- }
- */
- if ( $persontype == 2 && ! $codicefiscale ) {
- $woocommerce->add_error( __( '<strong>Codice Fiscale</strong> obbligatorio', 'woocommerce' ) );
- }
- /*
- if ( $persontype == 3 && ! $iva ) {
- $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
- }
- */
- if ( $persontype == 3 && ! $azienda ) {
- $woocommerce->add_error( __( '<strong>Azienda</strong> obbligatorio', 'woocommerce' ) );
- }
- if ( $persontype == 3 && ! $iva ) {
- $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
- }
- return $reg_errors;
- }
- //Updating use meta after registration successful registration
- add_action('woocommerce_created_customer','adding_extra_reg_fields');
- function adding_extra_reg_fields($user_id) {
- extract($_POST);
- update_user_meta($user_id, 'first_name', $firstname);
- update_user_meta($user_id, 'last_name', $lastname);
- // can also do multiple fields like that
- update_user_meta($user_id, 'first_name', $firstname);
- update_user_meta($user_id, 'last_name', $lastname);
- update_user_meta($user_id, 'billing_first_name', $firstname);
- update_user_meta($user_id, 'billing_last_name', $lastname);
- update_user_meta($user_id, 'billing_cpf', $codicefiscale);
- update_user_meta($user_id, 'billing_company', $azienda);
- update_user_meta($user_id, 'billing_cnpj', $iva);
- //update_user_meta($user_id, 'shipping_first_name', $firstname);
- //update_user_meta($user_id, 'shipping_last_name', $lastname);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement