0

I have added custom fields in the billing address in Woocommerce checkout page, this works fine. Now I would like to show these 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 ).

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>';  
}
1
  • Questions about third-party plugins are off-topic on this StackExchange, and should be asked on the WooCommerce official support page.
    – YourManDan
    Commented Aug 8, 2023 at 14:40

1 Answer 1

1

I have run into these issues before myself. I am not entirely sure it's possible to get adjust the billing address box directly unless you adjust the actual email template for the emails.

Specifically, if you have access to the file structure, look in the "/wp-content/plugins/woocommerce/templates/emails" folder of the WooCommerce Plugin. They are made to be modifiable in the sense that you should be able to use the wordpress theme structure to create a template by adding a new folder called woocommerce/emails and put the modified template in there. There may be a way to do this within a plugin as well but thats a bit more advanced.

As far as making them editable check out this blog post: Mishas blog. Misha is awesome.

Also, here is a handy guide for the hooks in the emails I use that might be helpful. Business Bloomer Email Hooks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.