Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: America/Los_Angeles (GMT-07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Bobby 1 year, 7 months ago.

Assisted by: Bobby.

Author Posts
October 25, 2023 at 1:03 pm #14660891

mikev-2

Dear WPML,

Our clients' site heavily relies on the use of Webhooks to send orders to their external order management system, in this system the orders get expected in the language they were ordered in.

So for example a Spanish customer creates a Spanish order ( all translated with WPML ) the order is successful and the Webhook sends the data to external order management system.
The system expects this order to be Spanish but in some cases English, German or Dutch order is sent to the webhook.

After investigating myself, I noticed the API Response (also used as the Webhook response by Woocommerce ) usually returns the base language (English) but sometimes returns the order in a random language.

How can we force the API Response to always return in the language the order was made?
We did notice every order does have the Meta "wpml_language" set to the correct language, we tried adding some custom code to force the output to this language without success.

Hopefully, you guys can help us resolve this issue, thanks in advance!

wpml_api_response.png
October 26, 2023 at 9:21 pm #14674461

Bobby
WPML Supporter since 04/2015

Languages: English (English )

Timezone: America/Los_Angeles (GMT-07:00)

Hi there,

Initially reading your ticket I immediately though of the meta wpml_language, are you able to possibly filter using the wpml_language meta?

For example, if I make an order in Spanish, would you get the results in both EN and ES or in your case you are seeing that you might only get the English version at random times?

I also recommend to please review the following documentation:
https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/#view-orders

Let me know your results, please.

November 2, 2023 at 9:32 am #14714313

mikev-2

Hi Bobby,

Thanks for your reply.

The meta wpml_language is available and I assumed I would be able to filter the output using some hooks or filters in WP to force it to the language stored in the meta however I wasn't successful in doing this since I couldn't find a good hook/filter to do this before the data was actually generated.

Related to your question about the Spanish order, It usually gets returned in the base language ( EN ) but sometimes in a completely different language ( German, Dutch or as expected Spanish ).

The documentation you've sent isn't of much help since we are primarily using the Woocommerce Webhooks, which use the Woocommerce API to get the order info, but since the webhooks have no configuration options for sending extra parameters to the API ( to force the language ) it isn't of much help.

So I think our best bet would indeed be to filter the data to force the order output to the correct language but I am unsure what hook/filter to use for this, can you help me find the appropriate hook/filter for this?

I've written the following code but unfortunately, this doesn't seem to resolve the issue, any idea how to achieve this?

```
/**
* Force Woo order api response to be in the correct language
*
* @param array $args
* @param WP_REST_Request $request
*
* @return array
*/
add_filter( 'woocommerce_rest_orders_prepare_object_query', function( $args, $request ) {

// Get language code from related order meta and enforce it
$order_id = $request['id'];

// Get related order meta
$order = wc_get_order( $order_id );

// If order is not found, return
if ( empty( $order ) ) {
return $args;
}

// Get order meta
$meta = $order->get_meta_data();

// Get language code from order meta
foreach ($meta as $key => $value) {
if ($value->key === 'wpml_language') {
$lang = $value->value;
break;
}
}

// Force language switch
if( !empty( $lang ) ) {
do_action( 'wpml_switch_language', $lang );
}

return $args;
}, 10, 2 );
```

November 3, 2023 at 12:32 am #14721683

Bobby
WPML Supporter since 04/2015

Languages: English (English )

Timezone: America/Los_Angeles (GMT-07:00)

Thank you for sharing this with me along with the code!

Please note that we currently are not able to provide support for custom work within this forum. I will, however, try to point you in the correct direction.

For more complex inquiries we also suggest contacting one of our 3rd party contractors that specialize in custom work with WPML
https://wpml.org/contractors/

Please take a look at the following documentation for the WCML hooks available

https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/

This hook might be of assistance
https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/#hook-7636959

Let me know your results, please.

The topic ‘[Closed] Woo REST Api and Webhook reponse in wrong language’ is closed to new replies.