8

I need get a list of orders in woocommerce passing start date, final date and status.

I tryed use some techniques like described by Mike Jolley, and I mixed with this. But I have not had success. This return all orders. I´m using the woocommerce version 2.2.10.

Thanks for help.

My code:

public function get_orders(){
        global $json_api;
        $initial_date = $json_api->query->para1;
        $final_date = $json_api->query->para2;
        $order_id = $json_api->query->para3;
        $status_order = $json_api->query->para4;

        define('GET_ORDERS_FILTER_DATE_FROM', $initial_date );
        define('GET_ORDERS_FILTER_DATE_TO', $final_date );
        add_filter('posts_where', array( __CLASS__, 'get_orders_where_dates_between') );
        $orders = get_posts( array(
            'post_type'   => 'shop_order',
            'orderby' => 'post_date',
            'order'     => 'DESC',
            'post_status' => array_keys( $status_order )
        ) );
        remove_filter('posts_where', 'order_page_get_orders_where_dates_between');

        return $orders;
}

function get_orders_where_dates_between( $where ){
        global $wpdb;
        if( ! defined('GET_ORDERS_FILTER_DATE_FROM') || ! defined('PARCELWARE_GET_ORDERS_FILTER_DATE_TO') )
            return $where;

        $where .= $wpdb->prepare(" AND post_date >= '%s' ", GET_ORDERS_FILTER_DATE_FROM);
        $where .= $wpdb->prepare(" AND post_date <= '%s' ", GET_ORDERS_FILTER_DATE_TO);

        return $where;
}

3 Answers 3

21

You can use wc_get_orders

$initial_date = yyyy-mm-dd;
$final_date = yyyy-mm-dd;
$orders = wc_get_orders(array(
    'limit'=>-1,
    'type'=> 'shop_order',
    'status'=> array( 'wc-completed','wc-refunded' ),
    'date_created'=> $initial_date .'...'. $final_date 
    )
);
2
  • 6
    According to github.com/woocommerce/woocommerce/wiki/…, the use of this interface is the preferred method.
    – B5A7
    Apr 15, 2020 at 0:06
  • Works great but it was missing colons for the dates. $initial_date = 'yyyy-mm-dd'; $final_date = 'yyyy-mm-dd';
    – kjid
    May 23, 2022 at 7:37
13

How about using custom wpdb query like this?

global $wpdb;

$date_from = '2015-11-20';
$date_to = '2015-12-20';
$post_status = implode("','", array('wc-processing', 'wc-completed') );

$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts 
            WHERE post_type = 'shop_order'
            AND post_status IN ('{$post_status}')
            AND post_date BETWEEN '{$date_from}  00:00:00' AND '{$date_to} 23:59:59'
        ");

echo "<pre>";
print_r($result);
2
  • 2
    I prefer your solution since wc_get_orders seems very buggy when searching for a date range (github tickets about it plus my direct bad experience). Aug 18, 2020 at 14:00
  • I'm afraid this approach is not HPOS safe, so if you use it in a plugin used in multiple stores, it won't work. This is because the table and columns are different if this setting (which is the default in newer versions) is enabled. Mar 7 at 0:23
0

If you want to use timestamps (date AND time):

$timestamp_start = '2022-04-07 20:00:00.000'; // example
$timestamp_end   = '2022-04-21 20:00:01.000'; // example
'date_created' => strtotime($timestamp_start ) .'...'. strtotime($timestamp_end)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.