1

In my PHP Symfony app I'm trying to retrieve Woocommerce subscriptions filtered by modified date and there's no documentation available as to how "filter" parameter can be used with the API. (https://prospress.github.io/subscriptions-rest-api-docs/#list-all-subscriptions). For now all I can use is "after" and "before" for date filtering which seem to be using subscription created date.

use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\Admin\Notes\WooCommerceSubscriptions;

$wc_subscription = new Client($this->getParameter('woocommerce_url'), $this->getParameter('woocommerce_key'), $this->getParameter('woocommerce_secret'), ['version' => 'wc/v1',]);

$subs = $wc_subscription->get('subscriptions', ['page' => 1, 'per_page' => 100, 'filter' => ['date_modified' => '2021-02-01']]);

This returns extremely high record number which is incorrect because only a few subscriptions have got updated.

How can I filter by "date_modified" or any other field in the subscription response object.

yivi
  • 42,438
  • 18
  • 116
  • 138
Teshan N.
  • 2,307
  • 3
  • 30
  • 59

1 Answers1

0

This from the WC rest posts controller might help:

/**
 * Filter the query arguments for a request.
 *
 * Enables adding extra arguments or setting defaults for a post
 * collection request.
 *
 * @param array           $args    Key value array of query var to query value.
 * @param WP_REST_Request $request The request used.
 */
 $args = apply_filters( "woocommerce_rest_{$this->post_type}_query", $args, $request );
 $query_args = $this->prepare_items_query( $args, $request );

It seems you could use the "woocommerce_rest_shop_subscription_query" to adjust the args prior to the WP_Query. And, since you get the request as a param, you can base your adjustment on the precise values wanted.

Remember that the WP_Query date query needs to have the column value in it to use the modified date rather than post date.

'date_query' => [
    'column' => 'post_modified',
    'after'  => [
        'year'  => 2021,
        'month' => 2,
        'day'   => 15,
    ],
],
John C
  • 96
  • 1
  • 5