php - 在 WooCommerce 快速订单预览中显示自定义字段
问题描述
在 WooCommerce 管理订单列表中,单击“眼睛图标”可以快速预览订单信息。
我添加了自定义结算结帐字段,但它们未显示在此快速预览中,而是在结算详细信息下显示“N/A”:
However when choose the edit order page, I can see them.
如何显示该计费自定义字段以便快速预览?
解决方案
在下面的代码中,您必须为每个计费自定义字段设置正确的元键。它将在计费部分下的快速订单预览中显示您的计费自定义字段:
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 文件中。它应该工作。
推荐阅读
- python - SQLAlchemy:如何检测/抑制重复的 JOIN 子句?
- python - scipy.signal.fftconvolve 行为已经改变(比较 1.1 和 0.19.1)
- unit-testing - Spock 测试:可以省略 when/then 块标签(保持以前由标签分隔的代码)?标签是语法糖吗?
- python - Python - 我声明了两个参数,但缺少 1?
- python - 如何使用按钮将排序功能添加到 Matplotlib 条形图和线图
- reactive-programming - Spring webflux/reactor 使用@Scheduled 读取数据库并执行一些任务
- applescript - Apple 脚本遇到错误:如果您离开页面,“无法将缺失值输入到类型文档或选项卡中”
- python - 当 numpy select 满足两个或多个条件时如何添加首选项
- r - 如何操纵 RShiny Slider 的启发式方法以正确显示小范围而没有重复的中间标签?
- javascript - 使用 JS 是否可以检测 new Image().src 何时将其引用的图像加载到内存中?