Filtering by last modified date in WooCommerce API

One of the great mysteries of life is why the WooCommerce API does not support a way to query orders by last modified date – even though this feature is a must-have for many ERP integrations.

Fortunately, it’s quite easy to implement:

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);

The query param is modified_after and it accepts an ISO8601 compliant date just like the rest of Woo API:

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

Tested with Woo 4.0.x / API v3.

Indrek Kõnnussaar

I'm a veteran Wordpress developer, context-driven tester, security enthusiast and the mastermind behind Codelight. I love building stuff that works and fixing stuff that doesn't.

Write me directly [email protected]

2 Responses to “Filtering by last modified date in WooCommerce API”

  1. Ken Loveless

    Wow! Thanks! This works for created_after as well. Works for Products and Orders. Can’t seem to make it work for Customers.

    Reply
  2. Justin

    Excellent information! This is exactly what I was looking for and a quick google led me to your site. Thanks so much.

    Reply

Leave a Reply

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×