在Woocommerce中为每个订单状态计算不同的订单状态计数和总现金 [英] Calculate different order status count and total cash for each order status in Woocommerce

查看:107
本文介绍了在Woocommerce中为每个订单状态计算不同的订单状态计数和总现金的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在woocommerce查询中的几天之间获得不同状态的订单总计.为了使它能在某天之间循环遍历所有订单,我使用以下查询:

I need to get the order total of a different status in between some days in woocommerce query. For it to loop through all orders in between some day I use the following query:

$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);

通过使用此代码,我可以遍历所有查询并正确获取详细信息. 现在,我需要将详细信息转换为以下格式

By using this code I can loop through all the query and get the details correctly. Now I need to get the details into following format

wc-shipped:总订单-> 10 total_cash-> 300 $
wc-完成: Totla订单-> 34 total_cash-> 4580 $
wc-cancelled:总订单-> 12 total_cash-> 100 $

wc-shipped : Total order -> 10 total_cash -> 300$
wc- completed : Totla order -> 34 total_cash -> 4580$
wc-cancelled : Total order -> 12 total_cash -> 100$

如何以这种格式获取此详细信息?

How can I get this detail in this format ?

我知道如何获得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();

But I need the price also. For to get price I can use $order_total_array[]=$order->get_total();

但是我不知道如何将它们组合起来并以所需的格式获得结果.

But i don't know how to combine them and get the result in the desired format.

推荐答案

我知道的最简单的方法...

The easiest way I know...

使用WC_Admin_Report类...您可以获取结果数组并根据需要对其进行操作...示例结果显示在下面...

using the WC_Admin_Report class... you can get the result array and manipulate it as you want... sample result is printed below...

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);
}

$data的演示.单击执行代码按钮.

demo of $data. Click Execute code button.

打印像:

wc-cancelled:总订单2->总现金-> 35 $
wc-completed:总订单21->总现金-> 315 $
wc-shipped:总订单11->总现金-> 211 $

wc-cancelled : Total Orders 2 -> Total Cash -> 35$
wc-completed : Total Orders 21 -> Total Cash -> 315$
wc-shipped : Total Orders 11 -> Total Cash -> 211$

这篇关于在Woocommerce中为每个订单状态计算不同的订单状态计数和总现金的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆