Stranger
Stranger

Reputation: 10611

WooCommerce REST API v3 - Filter products by modified date

I'm new to WooCommerce development and have little experience in Wordpress plugin development. I'm trying to use the WooCommerce REST API for retrieving all the products that were modified after a particular date.

From the documentation that is available here, https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products, I can see that products can be filtered by date by using a parameter called date which refers to the created date of the product. But I wanted to retrieve it by using the modified date of the products.

I'm unable to find any proper documentation for WooCommerce hooks to extend this functionality. Any reference or guidance on this would be greatly appreciated.

Upvotes: 2

Views: 1747

Answers (1)

Stranger
Stranger

Reputation: 10611

After several hours of meddling with different documentation and APIs, here's the solution that worked for me.

add_filter('woocommerce_rest_product_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);

It's to be noted that this is for version v3 (latest when this answer is written) and it may or may not work for the previous versions.

Upvotes: 1

Related Questions