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!
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?
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 );
}
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
The topic ‘[Closed] Woo REST Api and Webhook reponse in wrong language’ is closed to new replies.
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.