I'm trying to filter the response from /wp-json/wc/v3/orders for a specific API key. The filtering should be that orders from a specific country should not be included. The store still uses the WordPress posts storage (legacy) instead of HPOS. For some reason I can not find the correct approach on how to do this even though I thought there would be a simple solution to this. Below are two attempts, the first one isnt triggered at all and the second one is triggered but does not actually filter the response and I havent even limited it to the specific user / api key. I'm hoping someone can guide me in the right direction.
This does not get triggered
add_filter('woocommerce_rest_shop_order_query', 'filter_orders_exclude_iceland', 10, 2);
function filter_orders_exclude_iceland($args, $request) {
$consumer_key = $request->get_param('consumer_key');
if ($consumer_key === 'ck_xxxxx') {
$args['meta_query'][] = [
'relation' => 'AND',
[
'key' => '_billing_country',
'value' => 'DE',
'compare' => '!=',
],
[
'key' => '_shipping_country',
'value' => 'DE',
'compare' => '!=',
],
];
}
return $args;
}
This is triggered but does not actually filter any orders (and I have not limited it to a specific user or API key)
add_filter('woocommerce_order_data_store_cpt_get_orders_query', function($query, $query_vars) {
$existing_meta_query = isset($query['meta_query']) ? $query['meta_query'] : [];
$custom_filter = [
'relation' => 'AND',
[
'key' => '_shipping_country',
'value' => 'DE',
'compare' => '!=',
],
];
if (!empty($existing_meta_query)) {
$query['meta_query'] = [
'relation' => 'AND',
$existing_meta_query,
$custom_filter,
];
} else {
$query['meta_query'] = $custom_filter;
}
return $query;
}, 10, 2);
The composite filter hook woocommerce_rest_{$this->post_type}_query
doesn't seem to be anymore active on WooCommerce Rest API V3 for WooCommerce orders at least.
By searching on WooCommerce Rest API V3 controler source code I found in WC_REST_CRUD_Controller
class inside prepare_objects_query()
method the following composite filter hook with the same arguments (located line 354):
$args = apply_filters( "woocommerce_rest_{$this->post_type}_object_query", $args, $request );
To target WooCommerce Orders, you need to replace {$this->post_type}
with shop_order
post type (or order type), in your case.
Then you should simply replace in your first code snippet:
add_filter('woocommerce_rest_shop_order_query', 'filter_orders_exclude_iceland', 10, 2);
with:
add_filter('woocommerce_rest_shop_order_object_query', 'filter_orders_exclude_iceland', 10, 2);
Note that your meta query will only work on WooCommerce Orders with WordPress posts storage (legacy), as with High Performance Order Storage (HPOS), the billing and shipping country are located in wp_wc_order_addresses
WooCommerce custom database table, so not saved as order metadata anymore.
Now if you look at WC_Order_Query
source code, you will see in get_default_query_vars()
method that you can use the following query var arguments for billing and shipping country (without a starting undersore):
billing_country
,shipping_country
.Those arguments can be used directly When HPOS is enabled.