首页 > 解决方案 > 在 WooCommerce 快速订单预览中显示自定义字段

问题描述

在 WooCommerce 管理订单列表中,单击“眼睛图标”可以快速预览订单信息。

我添加了自定义结算结帐字段,但它们未显示在此快速预览中,而是在结算详细信息下显示“N/A”:

照片

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

如何显示该计费自定义字段以便快速预览?

标签: phpwordpresswoocommercemetadataorders

解决方案


在下面的代码中,您必须为每个计费自定义字段设置正确的元键。它将在计费部分下的快速订单预览中显示您的计费自定义字段:

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

代码位于您的活动子主题(或活动主题)的 function.php 文件中。它应该工作。

相关:在 Woocommerce 管理订单预览中显示自定义数据


推荐阅读