【问题标题】:Calculate different order status count and total cash for each order status in Woocommerce计算 Woocommerce 中每个订单状态的不同订单状态计数和总现金
【发布时间】:2016-02-04 09:48:00
【问题描述】:

我需要在 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$
wc-完成: 总订单 -> 34 总现金 -> 4580$
wc-cancelled : 总订单 -> 12 总现金 -> 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();

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

【问题讨论】:

    标签: wordpress woocommerce woothemes


    【解决方案1】:

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

    使用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-cancelled : 总订单 2 -> 总现金 -> 35$
    wc-completed : 总订单 21 -> 总现金 -> 315$
    wc-shipped : 总订单 11 -> 总现金 -> 211$

    【讨论】:

    • 谢谢雷格尔。我要检查一下。
    • 能否请您检查这一行 date( 'Y-m-d', strtotime( '01/01/2016' ) ),'value' => date( 'Y-m-d', strtotime( ' 2016 年 2 月 1 日'))。你写对了吗?
    • 另外,我如何检查此查询中的自定义字段值?因为在 shop_order 帖子类型中我也有一个自定义字段。我也在 wp_query 中写了这个,例如 'meta_key' => 'who', 'meta_value' => 'manik'
    • 我更新了代码。where_meta 是您可以添加meta_keymeta_value 的地方。随时根据您的需要更改日期。
    • 谢谢 Reigel,它正在工作。唯一的问题是我需要写两个查询。
    【解决方案2】:

    我会根据你的问题给出一个解决方案。

    $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-&gt;post_status

    (2) 获取订单总数我们可以使用$order-&gt;get_total()

    您可以将它们组合并存储到一个数组中

     $order_status_array[]=$order->post_status .'*'.$order->get_total();
    

    这里我结合使用*,你可以使用你自己的。

    让输出数组像

    Array ( [0] =&gt; wc-cancelled*64 [1] =&gt; wc-cancelled*254 [2] =&gt;wc-cancelled*93 [3] =&gt; wc-cancelled*44 [4] =&gt; wc-cancelled*213 [5] =&gt; 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>';
    }
    

    【讨论】:

    • 工作。这和我的代码一样。谢谢。
    • 运行良好,当然......但在代码实践方面并不好......特别是如果你有数十万个订单......这个循环运行成本太高...... -但是,是的,作为一名程序员,您始终可以选择使用哪一个......我并不是说它不起作用......what's the difference, in terms of display, with this one anyway?
    • 好的。你可能是对的。我会接受你的回答:)。但真的感谢 sagin 提供的解决方案。我检查了解决方案。
    • 你好,请检查这个问题stackoverflow.com/questions/36526268/…
    • 我的回答是回答这个问题的一种方法。但@Reigel 的答案是最好的。
    【解决方案3】:

    如果我正确理解您的问题,您希望将所有订单简化为由post_status 字段求和的行列表,对吗?

    最后你想要这样的东西:

    $order_status = array(
       'wc-shipped' => 10,
       'wc-completed' => 20,
       'wc-cancelled' => 30
    );
    

    我看到两个选项:

    1) 更改查询以利用 posts_groupby 仅返回汇总列。

    2) 遍历结果集中的所有行,并通过post_status 手动汇总它们。使用带有状态键的数组并将值增加$order-&gt;get_total()

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-19
    • 2017-05-24
    • 2018-08-16
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2019-07-22
    • 2017-04-02
    相关资源
    最近更新 更多