WooCommerce REST API - Filter Orders By Date Modified
Asked Answered
D

8

9

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?

Dr answered 1/3, 2017 at 12:18 Comment(0)
D
-1

I was able to get this working using the following request format:

wc-api/v1/orders?filter[updated_at_min]=2017-02-22&filter[updated_at_max]=2017-02-25

Dr answered 21/3, 2017 at 23:54 Comment(2)
That works for me in my testing wit the v2 REST API. You would also try something like this: /wp-json/wc/v2/orders?after=2017-05-17T00:00:00Z&before=2017-05-17T23:59:59ZDr
yes, the above used to work for orders, but this didn't work for customers. Also I think this is not a Woocommerce feature but a Wordpress one.Revers
E
7

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.

Essentiality answered 19/7, 2020 at 8:0 Comment(3)
By using woocommerce_rest_product_object_query, this exact solution works for products also. Thank you very much :)Lath
Worked for me, but returning only 10 results... with &per_page=20 you can increase to 20... Note that the maximum is 100 results only ! :(Boracite
@Meloman. tryout this and see whether it'll work for you. This is for products update to orders. #48477034Essentiality
C
4

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 products, orders and coupons endpoints.

More information:

Customable answered 28/12, 2021 at 20:56 Comment(0)
K
2

This is working:

/wp-json/wc/v2/orders?after=2019-01-10T00:00:00Z&before=2019-01-10T23:59:59Z
Koziol answered 28/6, 2019 at 9:38 Comment(1)
this is for "date_created", but not "date_modified" date :-(Sedimentation
D
1

I've solved the problem with the following steps:

  1. Create a folder.

  2. Create a file with the same name and the following content:

    <?php
    
    /**
     * Plugin Name: ModifyOrder
     */
    
    function modify_orders_after_query($request) {
        $request['date_query'][0]['column'] = 'post_modified';
        return $request;
    }
    
    add_filter( "woocommerce_rest_orders_prepare_object_query", 'modify_orders_after_query' );
    
  3. Drop it in the wp-content/plugins folder.

  4. In the admin panel, you can see your folder name as a plugin, activate it and try!

Disorientate answered 9/10, 2019 at 20:38 Comment(0)
V
0

It's Working

WooCommerce.get("products?after=2020-11-24T09:01:14&before=2020-11-25T04:51:22")
Vaticinal answered 25/1, 2021 at 10:35 Comment(0)
V
0

This something that i found on the woocommerce github page and it helped me out, its to bad this isn't included in the docs. Hope this link will hel some one else out:Add 'modified_before' and 'modified_after'

Valuation answered 22/5 at 9:24 Comment(0)
D
-1

I was able to get this working using the following request format:

wc-api/v1/orders?filter[updated_at_min]=2017-02-22&filter[updated_at_max]=2017-02-25

Dr answered 21/3, 2017 at 23:54 Comment(2)
That works for me in my testing wit the v2 REST API. You would also try something like this: /wp-json/wc/v2/orders?after=2017-05-17T00:00:00Z&before=2017-05-17T23:59:59ZDr
yes, the above used to work for orders, but this didn't work for customers. Also I think this is not a Woocommerce feature but a Wordpress one.Revers
R
-1

Ok, the woocommerce API doesnt actually implement the filter. Its from wordpress api which is used with woocommerce and latest wordpress doesnt support filter anymore. For latest wordpress you must add the filter functionality manually by adding wordpress rest api filter provided by the wp-api team

Rossetti answered 18/5, 2017 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.