在 WooCommerce 快速订单预览中显示自定义字段 [英] Show custom fields in WooCommerce quick order preview

查看:59
本文介绍了在 WooCommerce 快速订单预览中显示自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

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

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

这篇关于在 WooCommerce 快速订单预览中显示自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆