我目前正在开发一个项目,需要构建一个页面来展示 WooCommerce 订单的具体详细信息。这些详细信息包括订单 ID、订单状态(仅限于已完成的订单)、交货日期、费用金额以及相应的费用原因。
为了实现这一目标,我在我的 WooCommerce 订单中添加了名为“expense”和“expense_reason”的自定义元字段。
这是我面临的挑战:
当我尝试显示所有订单时,一切都运行顺利,没有任何问题。然而,当我尝试过滤并仅显示那些具有相关费用的订单时,问题就出现了。不幸的是,我正在努力找出这个错误的根源。
这是我正在使用的代码:
<?php
/**add_action('woocommerce_account_dashboard', 'run_on_specific_page',10);
function run_on_specific_page(){ */
if ( is_user_logged_in()) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$user_roles = $current_user->roles;
if(in_array('driver', $user_roles)){
//echo'<style>nav.woocommerce-MyAccount-navigation{ display:none;}.woocommerce-MyAccount-content{width: 100% !important;}';
//echo'.woocommerce-MyAccount-content p:nth-child(3){display:none}</style>';
$args = array(
'numberposts' => -1,
'relation' => 'AND',
array(
'meta_key' => 'Driver Expense',
'compare' => 'EXISTS',
),
array(
'key' => 'lddfw_driverid',
'value' => $user_id,
'compare' => '=',
),
'status' => 'completed',
);
$orders = wc_get_orders($args); ?>
<style>.text-white{color:#fff;}</style>
<table style="width:100%">
<tr style="background-color:#111;">
<td colspan="1"><h4 style="color:#fff;">Driver Detail</h4></td>
<td colspan="3"><h4 style="color:#fff;"><?php echo $current_user->user_email; ?></h4></td>
<td colspan="3"><h4 style="color:#fff;"><?php echo $user_id; ?></h4></td>
</tr>
<tr style="background-color:#111;">
<td class="text-white">Order No.</td>
<td class="text-white">Order Status</td>
<td class="text-white">Order Payment Method</td>
<td class="text-white">Delivery Date</td>
<td class="text-white">Expense Amount</td>
<td class="text-white">Expense Reason</td>
<td class="text-white">Expense Change Reason</td>
</tr>
<?php
foreach ($orders as $order) {
$order_id = $order->get_id();
$order_status = $order->get_status();
$order_total = $order->get_total();
$payment_method = $order->get_payment_method();
$delivery_date = $order->get_meta('Delivery Date');
$driver_expense = $order->get_meta('Driver Expense');
$driver_expense_reason = $order->get_meta('Driver Expense Reason');
$expense_change_reason = $order->get_meta('Expense Change Reason');
/**$meta_datas = get_post_meta($order_id);
$cod_meta = array();
foreach ($meta_datas as $meta_data) {
//print_r($meta_data);
if ($meta_data[0] === 'cod') {
$cod_meta[] = $meta_data[0];
$cod_value= $cod_meta[0];
//echo $cod_value;
}
}*/
?>
<tr>
<td><?php echo $order_id; ?></td>
<td><?php echo $order_status; ?></td>
<td><?php echo $payment_method; ?></td>
<td><?php echo $delivery_date; ?></td>
<td><?php echo $driver_expense; ?></td>
<td><?php echo $driver_expense_reason; ?></td>
<td><?php echo $expense_change_reason; ?></td>
</tr>
<?php
} ?>
</table> <?php
}
}
?>
如果任何人都可以阐明可能导致此错误的原因或提供更有效的方法来完成此任务,我将非常感谢您的帮助。
WC_Order_query
需要额外的东西来处理多个自定义字段(处理元查询)。您将在下面找到一个扩展 wc_get_orders()
以使用您的自定义字段的函数:
// Extend WooCommerce WC_Order_Query parameters (custom fields)
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_order_custom_query_vars', 10, 2 );
function handle_order_custom_query_vars( $query, $query_vars ) {
if ( isset($query_vars['expense']) && ! empty($query_vars['expense']) && $query_vars['expense'] === 'EXISTS' ) {
$query['meta_query'][] = array(
'key' => 'Driver Expense',
'compare' => 'EXISTS',
);
}
if ( isset($query_vars['driver-id']) && ! empty($query_vars['driver-id']) ) {
$query['meta_query'][] = array(
'key' => 'lddfw_driverid',
'value' => esc_attr($query_vars['driver-id']),
);
}
return $query;
}
代码位于子主题的functions.php文件中(或插件中)。
相关:WC_Order_Query,其中元数据不存在或不等于
现在您的工作订单查询将如下:
$orders = wc_get_orders( array(
'limit' => -1,
'status' => 'completed',
'expense' => 'EXISTS',
'driver-id' => $current_user->ID,
) );
我重新审视并优化了你的代码。尝试以下操作:
add_action('woocommerce_account_dashboard', 'run_on_specific_page',10);
function run_on_specific_page(){
global $current_user;
if ( $current_user > 0 ) {
if(in_array('administrator', $current_user->roles)){ ?>
<style>
table.drivers{width:100%;}
tr.bg-dark, tr.bg-dark td {background-color:#111 !important;}
table.drivers h4, table.drivers .white{color:white;}
</style>
<table class="drivers" style="width:100%">
<tr class="bg-dark">
<td colspan="1"><h4><?php _e('Driver Detail'); ?></h4></td>
<td colspan="3"><h4><?php echo $current_user->user_email; ?></h4></td>
<td colspan="3"><h4><?php echo $current_user->ID; ?></h4></td>
</tr>
<tr class="bg-dark">
<td class="white"><?php _e('Order No'); ?></td>
<td class="white"><?php _e('Order Status'); ?></td>
<td class="white"><?php _e('Order Payment Method'); ?></td>
<td class="white"><?php _e('Delivery Date'); ?></td>
<td class="white"><?php _e('Expense Amount'); ?></td>
<td class="white"><?php _e('Expense Reason'); ?></td>
<td class="white"><?php _e('Expense Change Reason'); ?></td>
</tr>
<?php
$orders = wc_get_orders( array(
'limit' => -1,
//'status' => 'completed',
'expense' => 'EXISTS',
'driver-id' => $current_user->ID,
) );
foreach ($orders as $order) { ?>
<tr>
<td><?php echo $order->get_order_number(); ?></td>
<td><?php echo $order->get_status(); ?></td>
<td><?php echo $order->get_payment_method(); ?></td>
<td><?php echo $order->get_meta('Delivery Date'); ?></td>
<td><?php echo $order->get_meta('Driver Expense'); ?></td>
<td><?php echo $order->get_meta('Driver Expense Reason'); ?></td>
<td><?php echo $order->get_meta('Expense Change Reason'); ?></td>
</tr><?php
}
} ?>
</table><?php
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。