Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @arrchnumbat

    I’m looking at the developer API, and I was wondering if it were possible to filter orders by country? It does not look like there is anything that allows me to do that going by the documentation:

    https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-orders

    If this is not possible, what plugins would people recommend that allow filtering support for the API?

    From what I understand, you’re curious about filtering orders by country using the WooCommerce REST API, correct? Unfortunately, the API doesn’t support this feature directly. It’s designed mainly to fetch orders, with filtering options based on order properties like status and customer.

    But don’t worry, there’s a workaround. You can use the ‘meta_key‘ and ‘meta_value‘ parameters in your API request. Here, ‘meta_key‘ would be ‘_billing_country‘ (or ‘_shipping_country‘, depending on your needs), and ‘meta_value‘ would be the country code you’re looking to filter by.

    Here’s how a sample API request would look:

    GET /wp-json/wc/v3/orders?meta_key=_billing_country&meta_value=US

    This will give you all orders where the billing country is the United States.

    About plugins that offer enhanced filtering capabilities for the WooCommerce REST API, there aren’t many that specifically cater to this requirement. You may want to think about custom development or hiring a developer if you need more advanced filtering options.

    📌 It’s important to note that while we’re always here to help, our scope of support does not cover custom development or design patterns, including troubleshooting or providing specific guidance on how to use custom action hooks or filters.

    For custom solutions, our recommendation is to connect with Codeable.io, our trusted WooCommerce development partner. They’re experts in crafting custom solutions for WooCommerce and might be able to help you develop a custom solution for your problem. Check them out here: 👉 https://woocommerce.com/codeable

    I hope this information is helpful. If you have any other questions or need further assistance, please feel free to ask.

    Thread Starter arrchnumbat

    (@arrchnumbat)

    Hi, thank you so much for the fast reply!

    It looks like the meta_key and meta_value will do what I want. I’m just after a list of orders for a particular country via the REST API. But when I test it, it looks like those parameters are not doing the filtering as I would expect.

    Is there any configuration that needs to be done to enable the meta_key parameter?

    Hi there @arrchnumbat 👋

    Is there any configuration that needs to be done to enable the meta_key parameter?

    Just to clarify, since the meta_key in the example my colleague shared above would be either the _billing_country or _shipping_country, therefore details which are included with every order, there is nothing further to be configured.

    I hope this is helpful! Please let us know if you have any further questions or concerns.

    Thread Starter arrchnumbat

    (@arrchnumbat)

    Thanks for the reply.

    I’ve tried to do some more testing, but I can confirm that the filtering is not happening as expected. This is effectively what I am querying using cURL (key and url masked):

    curl --request GET \
    --url 'https://******.com/wp-json/wc/v3/orders?meta_key=_shipping_country&meta_value=AU' \
    --header 'Authorization: Basic ********'

    What I am receiving is all of the orders, the first few being from the US in my case. So I know it’s not the endpoint or authentication. I’ve also tried with the _billing_country and meta values to no effect. I’ve also tried a corresponding query on a different installation (swapping the addres and auth key).

    So that’s why I was wondering is there something else I may need to enable for this filtering to happen.

    Plugin Support Kader Ibrahim S. a11n

    (@kaderibrahim)

    Hello @arrchnumbat,

    Thank you for getting back. I understand that the recommended solution is not working. The reason is that the meta_key is not a query argument for the request; hence, it gets removed. However, we can easily override it with the woocommerce_rest_{$this->post_type}_object_query filter. Here is an example:

    add_filter( 'woocommerce_rest_shop_order_object_query', 'wc_enable_country_filter', 10, 2 );
    
    function wc_enable_country_filter( $args, $request ) {
    	$billing_country = $request->get_param( 'billing_country' );
    	if ( $billing_country ) {
    		$args['meta_key']   = '_billing_country';
    		$args['meta_value'] = $billing_country;
    	}
    	return $args;
    }

    In the above code, we override the shop order object query by checking for the billing_country param in the query argument and adding it to the meta_key argument. Once you add the above code to your child theme’s functions.php , you can access the REST API URL like this:

    wp-json/wc/v3/orders/?billing_country=US

    Please note the above code does not add any validation to the incoming params. Hence, I request you to add that in case you wish to use the above code.

    Hope this helps. Please let us know should have further questions.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Developer API – filter orders by country?’ is closed to new replies.