Advertisement
eappereira

Campos Extras Woocommerce - CNPJ, IE e Razão Social

Jan 6th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.52 KB | None | 0 0
  1. /**
  2.  *
  3.  * Elton Pereira - eltondeveloper@gmail.com
  4.  * Adicionar novos campos personalizados no Cadastro Woocommerce
  5.  *
  6.  * @retrun void HTML.
  7.  */
  8.  
  9.  function wooc_campos_extra_registro() {
  10.         ?>
  11.         <p class="form-row form-row-first">
  12.         <label for="reg_billing_first_name"><?php _e( 'Nome do responsável', 'woocommerce' ); ?> <span class="required">*</span></label>
  13.         <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'] ); ?>" />
  14.         </p>
  15.        
  16.         <p class="form-row form-row-last">
  17.         <label for="reg_billing_company"><?php _e( 'Razão sosial', 'woocommerce' ); ?> <span class="required">*</span></label>
  18.         <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
  19.         </p>
  20.  
  21.         <div class="clear"></div>
  22.  
  23.         <p class="form-row form-row-wide">
  24.         <label for="reg_billing_cnpj"><?php _e( 'CNPJ', 'woocommerce' ); ?> <span class="required">*</span></label>
  25.         <input type="text" placeholder="000.000.000/0000-00" class="input-text" name="billing_cnpj" id="reg_billing_cnpj" value="<?php if ( ! empty( $_POST['billing_cnpj'] ) ) esc_attr_e( $_POST['billing_cnpj'] ); ?>" />
  26.         </p>
  27.  
  28.         <p class="form-row form-row-wide">
  29.         <label for="reg_billing_ie"><?php _e( 'Inscrição estadual,', 'woocommerce' ); ?> <span></span></label>
  30.         <input type="text" class="input-text" name="billing_ie" id="reg_billing_ie" value="<?php if ( ! empty( $_POST['billing_ie'] ) ) esc_attr_e( $_POST['billing_ie'] ); ?>" />
  31.         </p>
  32.  
  33.         <?php
  34. }
  35. add_action( 'woocommerce_register_form_start', 'wooc_campos_extra_registro' );
  36.  
  37. /**
  38.  * Validar os campos de registro extra.
  39.  *
  40.  * @param  string $username          Current username.
  41.  * @param  string $email             Current email.
  42.  * @param  object $validation_errors WP_Error object.
  43.  *
  44.  * @return void
  45.  */
  46. function wooc_validar_campos_extra_registro( $username, $email, $validation_errors ) {
  47.         if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
  48.                 $validation_errors->add( 'billing_first_name_error', __( '<strong>Erro!</strong>: Informe o nome do responsável!', 'woocommerce' ) );
  49.         }
  50.         if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
  51.                 $validation_errors->add( 'billing_company', __( '<strong>Erro!</strong>: Informe a razão social!.', 'woocommerce' ) );
  52.         }      
  53.         if ( isset( $_POST['billing_ie'] ) && empty( $_POST['billing_ie'] ) ) {
  54.                 $validation_errors->add( 'billing_ie', __( '<strong>Erro!</strong>: Informe a inscrição estadual!', 'woocommerce' ) );
  55.         }
  56.         if ( isset( $_POST['billing_cnpj'] ) && empty( $_POST['billing_cnpj'] ) ) {
  57.                 $validation_errors->add( 'billing_cnpj_error', __( '<strong>Erro!</strong>: Informe o CNPJ!.', 'woocommerce' ) );              
  58.         }
  59. }
  60. add_action( 'woocommerce_register_post', 'wooc_validar_campos_extra_registro', 10, 3 );
  61.  
  62. /**
  63.  * Salvar campos extras.
  64.  * @param  int  $customer_id Current customer ID.
  65.  *
  66.  * @return void
  67.  */
  68. function wooc_salvar_campos_extra_registro( $customer_id ) {
  69.         if ( isset( $_POST['billing_first_name'] ) ) {
  70.                 // WordPress default para o campo primeiro nome.
  71.                 update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  72.                 // WooCommerce primeiro nome.
  73.                 update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  74.         }
  75.         if ( isset( $_POST['billing_company'] ) ) {
  76.                 // WooCommerce Empresa
  77.                 update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
  78.         }
  79.         if ( isset( $_POST['billing_ie'] ) ) {
  80.                 // WooCommerce ie
  81.                 update_user_meta( $customer_id, 'billing_ie', sanitize_text_field( $_POST['billing_ie'] ) );
  82.         }
  83.  
  84.         if ( isset( $_POST['billing_cnpj'] ) ) {
  85.                 // WooCommerce cnpj
  86.                 update_user_meta( $customer_id, 'billing_cnpj', sanitize_text_field( $_POST['billing_cnpj'] ) );
  87.         }
  88. }
  89. add_action( 'woocommerce_created_customer', 'wooc_salvar_campos_extra_registro' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement