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>';
}