Advertisement
salvatorifabio

registration form

Mar 13th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.95 KB | None | 0 0
  1. add_filter( 'register_form', 'adding_custom_registration_fields' );
  2. function adding_custom_registration_fields( ) {
  3.  
  4. echo '<div class="form-row form-row-wide"><label for="reg_persontype">'.__('Person Type', 'woocommerce').' <span class="required">*</span></label>
  5. <select name="persontype" id="reg_persontype" class="select" value="'.esc_attr($_POST['persontype']).'" >
  6.                         <option value="0" >Seleziona</option>
  7.                         <option value="1" >Utente</option>
  8.                         <option value="2" >Studente</option>
  9.                         <option value="3" >Scuola</option>
  10.                     </select>';                    
  11. echo '<div class="form-row form-row-first" id="divfirst"><label for="reg_firstname">'.__('First Name', 'woocommerce').' <span class="required">*</span></label>
  12. <input type="text" class="input-text" name="firstname" id="reg_firstname" value="'.esc_attr($_POST['firstname']).'" /></div>';
  13. echo '<div class="form-row form-row-last" id="divlast"><label for="reg_lastname">'.__('Last Name', 'woocommerce').' <span class="required">*</span></label>
  14. <input type="text" class="input-text" name="lastname" id="reg_lastname" value="'.esc_attr($_POST['lastname']).'" /></div>';
  15.  
  16. echo '<div class="form-row form-row-wide" id="billing_cpf_field"><label for="reg_codicefiscale">'.__('Codice Fiscale', 'woocommerce').' <span class="required">*</span></label>
  17. <input type="text" class="input-text" name="codicefiscale" id="reg_codicefiscale" value="'.esc_attr($_POST['codicefiscale']).'" /></div>';
  18. echo '<div class="form-row form-row-wide" id="billing_company_field"><label for="reg_azienda">'.__('Azienda', 'woocommerce').' <span class="required">*</span></label>
  19. <input type="text" class="input-text" name="azienda" id="reg_azienda" value="'.esc_attr($_POST['azienda']).'" /></div>';                   
  20. echo '<div class="form-row form-row-wide" id="billing_cnpj_field"><label for="reg_iva">'.__('P. Iva', 'woocommerce').' <span class="required">*</span></label>
  21. <input type="text" class="input-text" name="iva" id="reg_iva" value="'.esc_attr($_POST['iva']).'" /></div>';                   
  22.                    
  23.                     echo '</div>';
  24.                     ?>
  25.                    
  26.                     <script>
  27.             jQuery(document).ready(function($) {
  28.     // Required fields.
  29.     $('#billing_company_field label, #billing_cpf_field label, #billing_cnpj_field label').append(' <abbr class="required" title="obrigatΓ³rio">*</abbr>');
  30.  
  31.     // Hide and show cpf and cnpj fields
  32.     function personTypeFields(current) {
  33.         $('#divfirst').hide();
  34.         $('#divlast').hide();
  35.         $('#billing_cpf_field').hide();
  36.         $('#billing_company_field').hide();
  37.         $('#billing_cnpj_field').hide();
  38.  
  39.         if (current == 1) {
  40.             $('#divfirst').show();
  41.             $('#divlast').show();
  42.             $('#billing_cpf_field').show();
  43.         }
  44.  
  45.         if (current == 2) {
  46.             $('#divfirst').show();
  47.             $('#divlast').show();
  48.             $('#billing_cpf_field').show();          
  49.         }
  50.        
  51.         if (current == 3){
  52.             $('#billing_company_field').show();
  53.             $('#billing_cnpj_field').show();
  54.         }
  55.     }
  56.     personTypeFields($('#reg_persontype').val());
  57.  
  58.     $('#reg_persontype').on('change', function() {
  59.         var current = $(this).val();
  60.  
  61.         personTypeFields(current);
  62.     });
  63. });
  64. </script>
  65. <?php
  66. }
  67.  
  68. //Validation registration form  after submission using the filter registration_errors
  69. add_filter('registration_errors', 'registration_errors_validation', 10,3);
  70. function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
  71.         global $woocommerce;
  72.         extract($_POST); // extracting $_POST into separate variables
  73.         if($firstname == '' ) {
  74.             $woocommerce->add_error( __( '<strong>Nome</strong> obbligatorio', 'woocommerce' ) );
  75.         }
  76.         if($lastname == '' ) {
  77.             $woocommerce->add_error( __( '<strong>Cognome</strong> obbligatorio', 'woocommerce' ) );
  78.         }
  79.         if($persontype == 0 ) {
  80.             $woocommerce->add_error( __( '<strong>Tipo di Persona</strong> obbligatorio', 'woocommerce' ) );
  81.         }
  82.         if ( $persontype == 1 && ! $codicefiscale ) {
  83.                 $woocommerce->add_error( __( '<strong>Codice Fiscale</strong> obbligatorio', 'woocommerce' ) );
  84.             }
  85.         /*
  86.         if ( $persontype == 2 && ! $azienda ) {
  87.                 $woocommerce->add_error( __( '<strong>Azienda</strong> obbligatorio', 'woocommerce' ) );
  88.             }
  89.            
  90.         if ( $persontype == 2 && ! $iva ) {
  91.                 $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
  92.             }
  93.         */
  94.         if ( $persontype == 2 && ! $codicefiscale ) {
  95.                 $woocommerce->add_error( __( '<strong>Codice Fiscale</strong> obbligatorio', 'woocommerce' ) );
  96.             }
  97.         /*
  98.         if ( $persontype == 3 && ! $iva ) {
  99.                 $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
  100.             }
  101.         */
  102.         if ( $persontype == 3 && ! $azienda ) {
  103.                 $woocommerce->add_error( __( '<strong>Azienda</strong> obbligatorio', 'woocommerce' ) );
  104.             }
  105.            
  106.         if ( $persontype == 3 && ! $iva ) {
  107.                 $woocommerce->add_error( __( '<strong>P. IVA</strong> obbligatorio', 'woocommerce' ) );
  108.             }
  109.         return $reg_errors;
  110. }
  111.  
  112. //Updating use meta after registration successful registration
  113. add_action('woocommerce_created_customer','adding_extra_reg_fields');
  114.  
  115. function adding_extra_reg_fields($user_id) {
  116.     extract($_POST);
  117.     update_user_meta($user_id, 'first_name', $firstname);
  118.     update_user_meta($user_id, 'last_name', $lastname);
  119.  
  120.         // can also do multiple fields like that
  121.         update_user_meta($user_id, 'first_name', $firstname);
  122.         update_user_meta($user_id, 'last_name', $lastname);
  123.     update_user_meta($user_id, 'billing_first_name', $firstname);
  124.     update_user_meta($user_id, 'billing_last_name', $lastname);
  125.     update_user_meta($user_id, 'billing_cpf', $codicefiscale);
  126.     update_user_meta($user_id, 'billing_company', $azienda);
  127.     update_user_meta($user_id, 'billing_cnpj', $iva);
  128.     //update_user_meta($user_id, 'shipping_first_name', $firstname);
  129.     //update_user_meta($user_id, 'shipping_last_name', $lastname);
  130.    
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement