2
votes

In WooCommerce admin orders list, clicking on the "eye icon" gives a quick preview for the order info.

I've added custom billing checkout fields, but they are not shown in this quick preview and instead under billing details it displays "N/A":

photo

However when choose the edit order page, I can see them.

How to display that billing custom fields in order quick preview?

1

1 Answers

2
votes

In the code below, for each of your billing custom fields, you will have to set the correct meta key. It will display your billing custom fields in the quick order preview under Billing section:

add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_billing_data', 10, 2 );
function admin_order_preview_add_custom_billing_data( $data, $order ) {
    $custom_billing_data = []; // initializing

    // Custom field 1: Replace '_custom_meta_key1' by the correct custom field metakey
    if( $custom_value1 = $order->get_meta('_custom_meta_key1') ) {
        $custom_billing_data[] = $custom_value1;
    }

    // Custom field 2: Replace '_custom_meta_key1' by the correct custom field metakey
    if( $custom_value2 = $order->get_meta('_custom_meta_key1') ) {
        $custom_billing_data[] = $custom_value2;
    }

    ## ……… And so on (for each additional custom field).

    // Check that our custom fields array is not empty
    if( count($custom_billing_data) > 0 ) {
        // Converting the array in a formatted string
        $formatted_custom_billing_data = implode( '<br>', $custom_billing_data );

        if( $data['formatted_billing_address'] === __( 'N/A', 'woocommerce' ) ) {
            $data['formatted_billing_address'] = $formatted_custom_billing_data;
        } else {
            $data['formatted_billing_address'] .= '<br>' . $formatted_custom_billing_data;
        }
    }

    return $data;
}

Code goes in function.php file of your active child theme (or active theme). It should work.

Related: Display custom data on Woocommerce admin order preview