Search code examples
phpwordpresswoocommerceorders

Display custom WooCommerce checkout fields in order details and make them editable


I'm updating a plugin for WooCommerce, and I have got a hooks.php file in which I handle WooCommerce hooks. I added four new billing fields to checkout and I need to display them in order details; I need also to make them editable.

Here is the code in which I succeed in displaying my fields:

add_action('woocommerce_admin_order_data_after_billing_address', function($order) 
  echo p(strong(__('Codice Fiscale', 'fatt-24')).': <br />' . order_c_fis($order));
  echo p(strong(__('Partita Iva / VAT number', 'fatt-24')).': <br />' . order_p_iva($order));
  echo p(strong(__('Codice destinatario', 'fatt-24')) . ': <br />' . order_recipientcode($order));
  echo p(strong(__('Indirizzo PEC', 'fatt-24')) . ': <br />' . order_pec_address($order));
}, 10, 1);

I'm not able to edit and save it.


Solution

  • At last I found this solution:

      <div class="edit_address">
        <?php 
    /* here I make the fields editable */
    if (customer_use_cf()){
            woocommerce_wp_text_input( array( 'id' => '_billing_fiscalcode', 'label' =>__('Codice Fiscale', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) ); // here I make the fields editable
    }
    
    if (customer_use_vat()){
            woocommerce_wp_text_input( array( 'id' => '_billing_vatcode', 'label' => __('Partita Iva / VAT number', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
    }
    
     if(customer_use_recipientcode()) {
                woocommerce_wp_text_input( array( 'id' => '_billing_recipientcode', 'label' => __('Codice destinatario', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
                woocommerce_wp_text_input( array( 'id' => '_billing_pecaddress', 'label' => __('Indirizzo PEC', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
     }?>
        </div>
    
        <?php
    
    }, 10, 1);
    
    /* here I update the post */
    
     add_action( 'woocommerce_process_shop_order_meta',  function ( $post_id, $post ){
        update_post_meta( $post_id, '_billing_pecaddress', wc_clean( sanitize_text_field($_POST[ '_billing_pecaddress' ] )) ); // here I update the post
        update_post_meta( $post_id, '_billing_recipientcode ', wc_clean(sanitize_text_field( $_POST[ '_billing_recipientcode' ] )) );
        update_post_meta( $post_id, '_billing_fiscalcode', wc_clean( sanitize_text_field($_POST[ '_billing_fiscalcode' ]) ) );
        update_post_meta( $post_id, '_billing_vatcode', wc_clean( sanitize_text_field($_POST[ '_billing_vatcode' ]) ) );
        }, 45, 2 );