我需要在woocommerce查询中的某些天之间获得不同状态的订单总数。为了让它在某一天之间遍历所有订单,我使用以下查询:
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
array(
'after' => array(
'year' => 2016,
'month' =>01,
'day' =>01,
),
'before' => array(
'year' => 2016,
'month' => 01,
'day' =>30,
),
'inclusive' => true,
),
),
);
$loop=new WP_Query($args);
通过使用此代码,我可以遍历所有查询并正确获取详细信息。 现在我需要将细节变成以下格式
wc-shipped:总订单 - > 10 total_cash - > $ 300
完成: Totla订单 - > 34 total_cash - > 4580 $
wc-canceled:总订单 - > 12 total_cash - > $ 100
如何以此格式获取此详细信息?
我知道如何获得wc-shipped : Total order -> 10
为此,我使用:
$order_status_get[]=$order->post_status;
$order_status_get= array_count_values($order_status_get);
foreach ($order_status_get as $key => $value) {
echo $key.'->'.$value;
}
但我也需要价格。为了获得价格,我可以使用$order_total_array[]=$order->get_total();
但我不知道如何将它们组合起来并以所需的格式获得结果。
答案 0 :(得分:3)
我知道最简单的方式......
使用WC_Admin_Report
类...你可以得到结果数组并根据需要进行操作......样本结果打印在下面......
include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php');
$reports = new WC_Admin_Report();
$args = array(
'data' => array(
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_cash'
),
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders'
),
'post_status' => array(
'type' => 'post_data',
'function' => '',
'name' => 'status'
),
),
'where' => array(
array(
'key' => 'post_date',
'value' => date( 'Y-m-d', strtotime( '02/01/2016' ) ), // starting date
'operator' => '>'
),
array(
'key' => 'post_date',
'value' => date( 'Y-m-d', strtotime( '02/31/2016' ) ), // end date...
'operator' => '<'
),
),
'where_meta' => array(
array(
'meta_key' => 'who',
'meta_value' => 'manik',
'operator' => '='
)
),
'order_status' => array( 'cancelled', 'completed', 'shipped' ),
'group_by' => 'posts.post_status',
'query_type' => 'get_results',
);
$data = $reports->get_order_report_data($args);
print_r($data);
打印类似
的内容Array
(
[0] => stdClass Object
(
[total_cash] => 35
[total_orders] => 2
[status] => wc-cancelled
)
[1] => stdClass Object
(
[total_cash] => 315
[total_orders] => 21
[status] => wc-completed
)
[2] => stdClass Object
(
[total_cash] => 211
[total_orders] => 11
[status] => wc-shipped
)
)
然后操纵$data
//print_r($data);
//
$currency = (function_exists('get_woocommerce_currency_symbol'))?get_woocommerce_currency_symbol():'';
foreach($data as $item) {
echo sprintf('<p>%s : Total Orders %s -> Total Cash -> %s%s </p>', $item->status, $item->total_orders, $item->total_cash, $currency);
}
demo of $data
.点击执行代码按钮。
打印如:
wc-canceled:总订单2 - &gt;总现金 - &gt; 35 $
wc-completed:Total Orders 21 - &gt;总现金 - &gt; 315 $
wc-shipped:总订单11 - &gt;总现金 - &gt; 211 $
答案 1 :(得分:3)
我会根据你的问题给出一个解决方案。
$order = new WC_Order('your order id ');
在你的情况下
while($loop->have_posts()): $loop->the_post();
$order_id=get_the_ID();
$order = new WC_Order($order_id);
(1)从我们可以使用$order->post_status
(2)为获得订单总额,我们可以使用$order->get_total()
您可以将它们组合并存储到一个数组中
$order_status_array[]=$order->post_status .'*'.$order->get_total();
这里我使用*
合并,您可以使用自己的。
使输出数组像
Array ( [0] => wc-cancelled*64 [1] => wc-cancelled*254 [2] =>wc-cancelled*93 [3] => wc-cancelled*44 [4] => wc-cancelled*213 [5] => wc-cancelled*44)
然后使用以下代码以正确格式排列此数组
$new_array = [];
foreach($order_status_array as $key => $value) {
list($name, $val) = explode('*', $value);
if(array_key_exists($name, $new_array)) {
$new_array[$name]['total_cash'] += $val;
$new_array[$name]['total_order']++;
} else {
$new_array[$name]['total_cash'] = $val;
$new_array[$name]['total_order'] = 1;
}
}
现在你的数组已准备就绪,就像
一样Array(
[wc-cancelled] => Array(
[total_cash] => ...
[total_order] =>...
)
...
)
现在使用以下代码
foreach ($new_array as $l=>$m){
echo $l.'Total Order:->'.$m['total_order'] .'Total Cash:->'.$m['total_cash'].'</br>';
}
。这会奏效。您也可以使用其他好的解决方案。试试这个。
所以整个代码
while($loop->have_posts()): $loop->the_post();
$order_id=get_the_ID();
$order = new WC_Order($order_id);
$order_status_array[]=$order->post_status .'*'.$order->get_total();
endwhile;
$new_array = [];
foreach($order_status_array as $key => $value) {
list($name, $val) = explode('*', $value);
if(array_key_exists($name, $new_array)) {
$new_array[$name]['total_cash'] += $val;
$new_array[$name]['total_order']++;
} else {
$new_array[$name]['total_cash'] = $val;
$new_array[$name]['total_order'] = 1;
}
}
foreach ($new_array as $l=>$m){
echo $l.'Total Order:->'.$m['total_order'] .'Total Cash:->'.$m['total_cash'].'</br>';
}
答案 2 :(得分:1)
如果我正确理解了您的问题,您希望所有订单都缩减为 var bookings = [{
bookingNumber: "123456789",
outboundDate: new Date("February 4, 2016 10:13:00"),
returnDate: new Date("February 4, 2016 10:13:00"),
route: "Dünkirchen - Dover",
vehicleType: "Car 1.85m x 4.5m",
Passengers: "1 adult and 1 child"
}];
console.log(bookings)
字段汇总的行列表,对吗?
最后你需要这样的东西:
post_status
我看到两个选项:
1)更改查询以使用$order_status = array(
'wc-shipped' => 10,
'wc-completed' => 20,
'wc-cancelled' => 30
);
仅返回汇总列。
2)迭代结果集中的所有行,并通过posts_groupby
手动汇总它们。使用带有状态键的数组,并将值增加post_status