Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Woocommerce orders Between Dates

I am trying the following code and want to get all orders between dates and print them

 $orders = $my_query->posts;
 $order = wc_get_order( $order_id );
 $order_data = $order->get_data(); // The Order data
 $order_id = $order_data['id'];
 if ($order_data['date_created']='12-12-2023')
 {
    echo('Order id-'.$order_id.'---order id end here-----');
 }

Getting error Uncaught Error: Call to a member function get_data() on bool

like image 985
Rohan Avatar asked Nov 19 '25 13:11

Rohan


2 Answers

please try to use this code:

$initial_date = yyyy-mm-dd;
$final_date = yyyy-mm-dd;
$orders = wc_get_orders(array(
    'limit'=>-1,
    'type'=> 'shop_order',
    'date_created'=> $initial_date .'...'. $final_date 
    )
);

$orders should now hold all orders between given dates.

like image 93
Titus Avatar answered Nov 21 '25 04:11

Titus


It also works with dates that include hours : minutes : seconds :

$date_a = '2022-08-18 09:45:00';
$date_b = '2022-09-02 19:30:00';

$on_hold_orders = wc_get_orders
(
    array 
    (
    'limit' => -1,
    'post_type' => 'shop_order',
    'status'    => 'on-hold',
    'date_created'  => strtotime($date_a) .'...'. strtotime($date_b)
    )
);
like image 24
Yoda - user264474 Avatar answered Nov 21 '25 04:11

Yoda - user264474