Show custom fields in WooCommerce quick order preview

a 夏天 提交于 2021-02-08 02:05:08

问题


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":

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

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


回答1:


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



来源:https://stackoverflow.com/questions/57846511/show-custom-fields-in-woocommerce-quick-order-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!