Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cs_wc_extra_register_fields() {
- ?>
- <p class="form-row form-row-first">
- <label for="reg_billing_first_name"><?php _e( 'Nome', 'textdomain' ); ?> <span class="required">*</span></label>
- <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'] ); ?>" />
- </p>
- <p class="form-row form-row-last">
- <label for="reg_billing_last_name"><?php _e( 'Sobrenome', 'textdomain' ); ?> <span class="required">*</span></label>
- <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'] ); ?>" />
- </p>
- <p class="form-row form-row-wide">
- <label for="billing_company"><?php _e( 'Razão Social', 'textdomain' ); ?> <span class="required">*</span></label>
- <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'] ); ?>" />
- </p>
- <p class="form-row form-row-first">
- <label for="billing_cnpj"><?php _e( 'CNPJ', 'textdomain' ); ?> <span class="required">*</span></label>
- <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'] ); ?>" />
- </p>
- <p class="form-row form-row-last">
- <label for="reg_billing_ie"><?php _e( 'Inscrição Estadual', 'textdomain' ); ?> <span class="required">*</span></label>
- <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'] ); ?>" />
- <?php
- }
- add_action( 'woocommerce_register_form_start', 'cs_wc_extra_register_fields' );
- /**
- * Validate the extra register fields.
- *
- * @param string $username Current username.
- * @param string $email Current email.
- * @param object $validation_errors WP_Error object.
- *
- * @return void
- */
- function cs_wc_validate_extra_register_fields( $username, $email, $validation_errors ) {
- if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
- $validation_errors->add( 'billing_first_name_error', __( '<strong>Erro</strong>: Digite o seu nome.', 'textdomain' ) );
- }
- if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
- $validation_errors->add( 'billing_last_name_error', __( '<strong>Erro</strong>: Digite o seu sobrenome.', 'textdomain' ) );
- }
- if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
- $validation_errors->add( 'billing_company', __( '<strong>Erro</strong>: Digite a Razão Social da sua empresa.', 'textdomain' ) );
- }
- if ( isset( $_POST['billing_cnpj'] ) && empty( $_POST['billing_cnpj'] ) ) {
- $validation_errors->add( 'billing_cnpj', __( '<strong>Erro</strong>: Digite o seu CNPJ.', 'textdomain' ) );
- }
- if ( isset( $_POST['billing_ie'] ) && empty( $_POST['billing_ie'] ) ) {
- $validation_errors->add( 'billing_ie', __( '<strong>Erro</strong>: Digite o seu Inscrição Estadual.', 'textdomain' ) );
- }
- }
- add_action( 'woocommerce_register_post', 'cs_wc_validate_extra_register_fields', 10, 3 );
- /**
- * Save the extra register fields.
- *
- * @param int $customer_id Current customer ID.
- *
- * @return void
- */
- function cs_wc_save_extra_register_fields( $customer_id ) {
- if ( isset( $_POST['billing_first_name'] ) ) {
- // WordPress default first name field.
- update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
- // WooCommerce billing first name.
- update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
- }
- if ( isset( $_POST['billing_last_name'] ) ) {
- // WordPress default last name field.
- update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
- // WooCommerce billing last name.
- update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
- }
- if ( isset( $_POST['billing_company'] ) ) {
- // WordPress default first cnpj field.
- update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
- // WooCommerce billing cnpj name.
- update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
- }
- if ( isset( $_POST['billing_cnpj'] ) ) {
- // WordPress default first cnpj field.
- update_user_meta( $customer_id, 'billing_cnpj', sanitize_text_field( $_POST['billing_cnpj'] ) );
- // WooCommerce billing cnpj name.
- update_user_meta( $customer_id, 'billing_cnpj', sanitize_text_field( $_POST['billing_cnpj'] ) );
- }
- if ( isset( $_POST['billing_ie'] ) ) {
- // WordPress default first inscrição estadual field.
- update_user_meta( $customer_id, 'billing_ie', sanitize_text_field( $_POST['billing_ie'] ) );
- // WooCommerce billing inscrição estadual name.
- update_user_meta( $customer_id, 'billing_ie', sanitize_text_field( $_POST['billing_ie'] ) );
- }
- }
- add_action( 'woocommerce_created_customer', 'cs_wc_save_extra_register_fields' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement