【发布时间】:2023-03-22 19:05:01
【问题描述】:
您好,我正在开发 woocommerce
我在 Woocommerce 中为订单添加了自定义字段,这是一个代理名称,每个订单都分配有一个代理。
我想获取代理名称及其总订单数。
我已经创建了自定义小部件并且它运行良好,只是每次都会发出代理名称。我只需要一次代理名称及其总数。
下面是我试过的代码:
function wc_orders_dashboard_widget_function() {
$args = array(
'meta_key' =>'_wc_acof_2',
'post_type' => 'shop_order',
'post_status' => 'All',
'posts_per_page' => 5
);
$orders = get_posts( $args );
if( count( $orders ) > 0 ) {
?>
<table width="100%" class="vao_orders_table">
<tr>
<th><?php _e( 'Agent Name', 'woocommerce' ); ?></th>
<th><?php _e( 'Order Count', 'woocommerce' ); ?></th>
</tr>
<?php
foreach ( $orders as $key => $value ) {
?>
<tr>
<td>
<?php
$order = new WC_Order( $value->ID );
$order_id = $order->get_order_number();
$agent_name = get_post_meta( $order_id, '_wc_acof_2', true );
// 1. Get the order ID and its link
if ( $agent_name ) {
echo $agent_name;
?>
</td>
<td>
<?php
// 2. Get status of the order
$order_count += wp_count_posts($value->post_type );
echo $order_count;
}
?>
</td>
</tr>
<?php
}
?></table><?php
// 4. Adding All orders link which will redirect to Orders page of WooCommerce
printf( '<div id="vao_orders"><span class="dashicons dashicons-cart"></span> <a href="%s">' . __( 'View all orders' ) . '</a></div>', admin_url( 'edit.php?post_type=shop_order' ) );
}else{
// If no orders then display No Orders message
echo __( 'No Orders.' );
}
}
但它显示如下输出:
代理名称订单计数
代理1 1
代理1 2
特工2 3
特工1 4
我的预期输出:
代理名称订单计数
代理 1 3
特工2 1
你们能帮我解决这个问题吗?我错过了什么?
【问题讨论】:
标签: php sql wordpress woocommerce custom-fields