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 : 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 ?
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 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…
using the
WC_Admin_Report
class… you can get the result array and manipulate it as you want… sample result is printed below…print something like
then manipulate
$data
demo of
$data
. Click Execute code button.Prints like:
I will give a solution that based on your question .
In your case
(1) get order status from order we can use
$order->post_status
(2) to get order total we can use
$order->get_total()
You can combine them and store it in to one array
here i combined using
*
, you can use your own.so that the out put array like
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)
Then use the following code to arrange this array in proper format
now your array is ready , and it’s like
now use the following code
. This will work . You can use other good solutions also . Try this .
so entire code is
If I understand your question correctly, you want all orders dumbed down to a list of rows summed by the
post_status
field, right?In the end you want something like this:
I see two options:
1) Change the query to utilize
posts_groupby
to only return summarized columns.2) Iterate over all the rows in the result set and summarize them by
post_status
manually. Use an array with the key of the status and increment the value by$order->get_total()