-1

I have added custom fields in the billing address in Woocommerce checkout page, this works fine. Now I would like to show the value of these fields custom fields inside the "billing address" box in the email and on the order summary page in "my account". This is the code I'm using, it works fine but the only things I'm missing are positioning the fields well in the email and order summary views and making the fields in the admin page editable (like the other billing fields in woocomerce ). is there a hooks or a way to show the value of those custom fields inside the "billing address" box? I can not do it I would be very grateful if you help me

creating custom fields

add_filter( 'woocommerce_checkout_fields', 'custom_fields_checkout' );
function custom_fields_checkout( $fields ) {
    
   $fields['billing']['codice_fiscale'] = array(
      'label' => 'Codice fiscale',
      'type'  => 'text',
      'required' => true,
      'class' => array( 'form-row-last' ),
      'priority' => 30,
   );
    
    $fields['billing']['partita_iva'] = array(
      'label' => 'Partita iva',
      'type'  => 'text',
      'required' => false,
      'class' => array( 'form-row-first' ),
      'priority' => 31,
   );
    
   $fields['billing']['codice_univoco'] = array(
      'label' => 'Codice Univoco',
      'type'  => 'text',
      'required' => false,
      'class' => array( 'form-row-last' ),
      'priority' => 32,
   );
   return $fields;  
    
}

saving fields

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
   if ( ! empty( $_POST['codice_fiscale'] ) ) {
       update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
   }
   if ( ! empty( $_POST['partita_iva'] ) ) {
       update_post_meta( $order_id, 'partita_iva', sanitize_text_field( $_POST['partita_iva'] ) );
   }
   if ( ! empty( $_POST['codice_univoco'] ) ) {
       update_post_meta( $order_id, 'codice_univoco', sanitize_text_field( $_POST['codice_univoco'] ) );
   }

}

show the fields in the order administration page

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
   if($codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true ));
       echo '<p><strong>Codice fiscale: </strong>' . $codice_fiscale . '</p>';
   if($partita_iva = get_post_meta( $order->get_id(), 'partita_iva', true ));
       echo '<p><strong>Partita iva: </strong>' . $partita_iva .'</p>';
   if($codice_univoco = get_post_meta( $order->get_id(), 'codice_univoco', true ));
       echo '<p><strong>Codice univoco: </strong>' . $codice_univoco . '</p>';
}

show the fields in the email

add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    if($codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true ));
        echo '<p><strong>Codice fiscale: </strong>' . $codice_fiscale . '</p>';
    if($partita_iva = get_post_meta( $order->get_id(), 'partita_iva', true ));
        echo '<p><strong>Partita iva: </strong>' . $partita_iva .'</p>';
    if($codice_univoco = get_post_meta( $order->get_id(), 'codice_univoco', true ));
        echo '<p><strong>Codice univoco: </strong>' . $codice_univoco . '</p>';  
}

test modified code, where am I wrong?

function add_custom_billing_fields( $fields ) {

    $fields['billing_codice_fiscale'] = array(
        'label' => ( 'Codice fiscale'),
        'type'  => 'text',
        'required'  => true,
        'class'     => array( 'form-row-last' ),
        'priority' => 30,
        
    );

    $fields['billing_partita_iva'] = array(
        'label' => 'Partita iva',
        'type'  => 'text',
        'required'  => false,
        'class'     => array('form-row-wide'),
        'priority' => 31,
    );

    $fields['billing_codice_univoco'] = array(
        'label' => 'Codice Univoco',
        'type'  => 'text',
        'required'  => false,
        'class'     => array( 'form-row-wide' ),
        'priority' => 32,
    );

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'add_custom_billing_fields' );
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['billing_codice_fiscale'] ) ) {
        update_post_meta( $order_id, 'billing_codice_fiscale', sanitize_text_field( $_POST['billing_codice_fiscale'] ) );
    }
    if ( ! empty( $_POST['billing_partita_iva'] ) ) {
        update_post_meta( $order_id, 'billing_partita_iva', sanitize_text_field( $_POST['billing_partita_iva'] ) );
    }
    if ( ! empty( $_POST['billing_codice_univoco'] ) ) {
        update_post_meta( $order_id, 'billing_codice_univoco', sanitize_text_field( $_POST['billing_codice_univoco'] ) );
    }

}
LesCa
  • 27
  • 6
  • What is the difference between the billing fields you prefult in the checkout and the custom ones? because I noticed that unlike the default ones, the custom ones do not memorize the value to be able to precompile it in a subsequent order. – LesCa Aug 08 '23 at 10:55
  • WooCommerce recently introduced High Performance Order Storage and `get_post_meta()` and `update_post_meta()` may not be relevant any more. You should use CRUD which was introduced in 3.0. – helgatheviking Aug 08 '23 at 21:32
  • @helgatheviking, i tried to use update and to use the other hooks (woocommerce_billing_fields) but now the fields don't show anymore in the checkout page, i put the modified code under the above question – LesCa Aug 09 '23 at 07:52

0 Answers0