Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce REST API - Filter Orders By Date Modified

I'm using the WooCommerce REST API (http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction) and can download Customers, Orders, etc successfully.

I'm now trying to get a filtered list of Orders where the Date Modified for the Order is after a certain date, but haven't been able to get this to work so far. The response to get GET request for an Order includes:

"date_modified": "2016-12-21T00:33:38",

I've tried the following:

wp-json/wc/v1/orders?filter[modified]=2017-02-14

but that just returns all orders. I would like to change the = to be a >= so it gets all Orders after the specified date, but haven't been able to find an example of how to structure the request URL for this?

like image 326
user982124 Avatar asked Mar 01 '17 12:03

user982124


3 Answers

This worked for me. Tested with Woo 4.0.x / API v3

add_filter('woocommerce_rest_orders_prepare_object_query', function(array $args, \WP_REST_Request $request) {
    $modified_after = $request->get_param('modified_after');

    if (!$modified_after) {
        return $args;
    }

    $args['date_query'][0]['column'] = 'post_modified';
    $args['date_query'][0]['after']  = $modified_after;

    return $args;

}, 10, 2);

/wp-json/wc/v3/orders/?modified_after=2020-05-09T14:00:00

Hope it helps someone.

like image 81
Muhwezi Jerald basasa Avatar answered Sep 30 '22 03:09

Muhwezi Jerald basasa


As of WooCommerce 5.8 (released October 21, 2021), Muhwezi Jerald Basasa's solution is no longer necessary.

The WooCommerce API now supports the modified_after and modified_before parameters for the product, order and coupon endpoints.

More information:

  • https://developer.woocommerce.com/2021/10/12/woocommerce-5-8-released/
  • https://github.com/woocommerce/woocommerce/blob/release/5.8/changelog.txt
like image 26
simplism Avatar answered Sep 30 '22 04:09

simplism


This is working:

/wp-json/wc/v2/orders?after=2019-01-10T00:00:00Z&before=2019-01-10T23:59:59Z
like image 34
Leandro Silva Avatar answered Sep 30 '22 03:09

Leandro Silva