【发布时间】:2019-09-08 23:41:07
【问题描述】:
在 WooCommerce 管理订单列表中,单击“眼睛图标”可以快速预览订单信息。
我添加了自定义结算结帐字段,但它们未显示在此快速预览中,而是在结算详细信息下显示“N/A”:
但是在选择编辑订单页面时,我可以看到它们。
如何在订单快速预览中显示该计费自定义字段?
【问题讨论】:
标签: php wordpress woocommerce metadata orders
在 WooCommerce 管理订单列表中,单击“眼睛图标”可以快速预览订单信息。
我添加了自定义结算结帐字段,但它们未显示在此快速预览中,而是在结算详细信息下显示“N/A”:
但是在选择编辑订单页面时,我可以看到它们。
如何在订单快速预览中显示该计费自定义字段?
【问题讨论】:
标签: php wordpress woocommerce metadata orders
在下面的代码中,您必须为每个结算自定义字段设置正确的元键。它将在“计费”部分下的快速订单预览中显示您的计费自定义字段:
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 文件中。它应该可以工作。
【讨论】:
return $data;...然后将“计费”一词更改为仅在此副本中到处运送。您必须为自定义运输字段设置正确的元键。