Calculate your order status account and total cash for each order status in Woocommerce

I need to get the total order quantity of a different status within a few days in a woocommerce request. In order for it to scroll through all the orders between them, 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);

Using this code, I can execute a full request loop and get the data correctly. Now I need to get the details in the following format

wc-shipped: General order → 10 total_cash → 300 $
   wc- completed: Order Totla → 34 total_cash → 4580 $
  wc-cancel: General order → 12 total_cash → 100 $

How can I get this item in this format?

I know how to get wc-shipped : Total order -> 10

For this, I use:

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

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

, .

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

$data. " ".

:

wc-cancel: Total Orders 2 → → 35 $
wc-completed: 21 → → 315 $
wc-: 11 → → 211 $

+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();

, *, .

out,

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>';
}
+3

, , , post_status, ?

- :

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

:

1) Modify the query to use posts_groupbyto return totals columns.

2) Iterate over all the rows in the result set and summarize them post_statusmanually. Use an array with a status key and increase the value by$order->get_total()

+1
source

Source: https://habr.com/ru/post/1627261/


All Articles