Advertisement
marciomrb

function extra registro

Oct 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.23 KB | None | 0 0
  1. function cs_wc_extra_register_fields() {
  2.     ?>
  3. <p class="form-row form-row-first">
  4.         <label for="reg_billing_first_name"><?php _e( 'Nome', 'textdomain' ); ?> <span class="required">*</span></label>
  5.         <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
  6.     </p>
  7.     <p class="form-row form-row-last">
  8.         <label for="reg_billing_last_name"><?php _e( 'Sobrenome', 'textdomain' ); ?> <span class="required">*</span></label>
  9.         <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
  10.     </p>
  11. <p class="form-row form-row-wide">
  12.         <label for="billing_company"><?php _e( 'Razão Social', 'textdomain' ); ?> <span class="required">*</span></label>
  13.         <input type="text" class="input-text" name="billing_company" id="billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
  14.     </p>
  15.  
  16.     <p class="form-row form-row-first">
  17.         <label for="billing_cnpj"><?php _e( 'CNPJ', 'textdomain' ); ?> <span class="required">*</span></label>
  18.         <input type="text" class="input-text cnpj" name="billing_cnpj" id="billing_cnpj" value="<?php if ( ! empty( $_POST['billing_cnpj'] ) ) esc_attr_e( $_POST['billing_cnpj'] ); ?>" />
  19.     </p>
  20.     <p class="form-row form-row-last">
  21.         <label for="reg_billing_ie"><?php _e( 'Inscrição Estadual', 'textdomain' ); ?> <span class="required">*</span></label>
  22.         <input type="text" class="input-text ie" name="billing_ie" id="billing_ie" value="<?php if ( ! empty( $_POST['billing_ie'] ) ) esc_attr_e( $_POST['billing_ie'] ); ?>" />
  23.  
  24.  
  25.  
  26.     <?php
  27. }
  28. add_action( 'woocommerce_register_form_start', 'cs_wc_extra_register_fields' );
  29. /**
  30.  * Validate the extra register fields.
  31.  *
  32.  * @param  string $username          Current username.
  33.  * @param  string $email             Current email.
  34.  * @param  object $validation_errors WP_Error object.
  35.  *
  36.  * @return void
  37.  */
  38. function cs_wc_validate_extra_register_fields( $username, $email, $validation_errors ) {
  39.     if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
  40.         $validation_errors->add( 'billing_first_name_error', __( '<strong>Erro</strong>: Digite o seu nome.', 'textdomain' ) );
  41.     }
  42.     if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
  43.         $validation_errors->add( 'billing_last_name_error', __( '<strong>Erro</strong>: Digite o seu sobrenome.', 'textdomain' ) );
  44.     }
  45.     if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
  46.         $validation_errors->add( 'billing_company', __( '<strong>Erro</strong>: Digite a Razão Social da sua empresa.', 'textdomain' ) );
  47.     }
  48.     if ( isset( $_POST['billing_cnpj'] ) && empty( $_POST['billing_cnpj'] ) ) {
  49.         $validation_errors->add( 'billing_cnpj', __( '<strong>Erro</strong>: Digite o seu CNPJ.', 'textdomain' ) );
  50.     }
  51.     if ( isset( $_POST['billing_ie'] ) && empty( $_POST['billing_ie'] ) ) {
  52.         $validation_errors->add( 'billing_ie', __( '<strong>Erro</strong>: Digite o seu Inscrição Estadual.', 'textdomain' ) );
  53.     }
  54. }
  55. add_action( 'woocommerce_register_post', 'cs_wc_validate_extra_register_fields', 10, 3 );
  56. /**
  57.  * Save the extra register fields.
  58.  *
  59.  * @param  int  $customer_id Current customer ID.
  60.  *
  61.  * @return void
  62.  */
  63.  
  64.  
  65. function cs_wc_save_extra_register_fields( $customer_id ) {
  66.     if ( isset( $_POST['billing_first_name'] ) ) {
  67.         // WordPress default first name field.
  68.         update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  69.         // WooCommerce billing first name.
  70.         update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  71.     }
  72.  
  73.     if ( isset( $_POST['billing_last_name'] ) ) {
  74.         // WordPress default last name field.
  75.         update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
  76.         // WooCommerce billing last name.
  77.         update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
  78.     }
  79.         if ( isset( $_POST['billing_company'] ) ) {
  80.         // WordPress default first cnpj field.
  81.         update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
  82.         // WooCommerce billing cnpj name.
  83.         update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
  84.     }
  85.     if ( isset( $_POST['billing_cnpj'] ) ) {
  86.         // WordPress default first cnpj field.
  87.         update_user_meta( $customer_id, 'billing_cnpj', sanitize_text_field( $_POST['billing_cnpj'] ) );
  88.         // WooCommerce billing cnpj name.
  89.         update_user_meta( $customer_id, 'billing_cnpj', sanitize_text_field( $_POST['billing_cnpj'] ) );
  90.     }
  91.         if ( isset( $_POST['billing_ie'] ) ) {
  92.         // WordPress default first inscrição estadual field.
  93.         update_user_meta( $customer_id, 'billing_ie', sanitize_text_field( $_POST['billing_ie'] ) );
  94.         // WooCommerce billing inscrição estadual name.
  95.         update_user_meta( $customer_id, 'billing_ie', sanitize_text_field( $_POST['billing_ie'] ) );
  96.     }
  97. }
  98. add_action( 'woocommerce_created_customer', 'cs_wc_save_extra_register_fields' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement