Skip to content Skip to sidebar

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client was experiencing an issue where the variation_id for products in WooCommerce was different for each language after an update, whereas previously it was the same across all languages.
Solution:
The client reported they added a code solution to ensure the variation_id remains consistent across different languages. The client added a custom filter to their functions.php file to force the variation_id to revert to the default language's variation_id for all languages. Here is the code snippet they used:

add_filter('woocommerce_rest_prepare_shop_order_object', 'force_variation_ids_to_default_language', 10, 3);<br />function force_variation_ids_to_default_language($response, $order, $request) {<br />    if (!is_a($order, 'WC_Order')) return $response;<br />    $default_lang = function_exists('wpml_get_default_language') ? wpml_get_default_language() : 'nl';<br />    $order_lang = $order->get_meta('wpml_language');<br />    if ($order_lang && $order_lang !== $default_lang) {<br />        foreach ($response->data['line_items'] as $index => $item) {<br />            if (!empty($item['variation_id'])) {<br />                $current_variation_id = $item['variation_id'];<br />                $default_variation_id = apply_filters('wpml_object_id', $current_variation_id, 'product_variation', false, $default_lang);<br />                if ($default_variation_id) {<br />                    $response->data['line_items'][$index]['variation_id'] = (int) $default_variation_id;<br />                }<br />            }<br />            if (!empty($item['product_id'])) {<br />                $current_product_id = $item['product_id'];<br />                $default_product_id = apply_filters('wpml_object_id', $current_product_id, 'product', false, $default_lang);<br />                if ($default_product_id) {<br />                    $response->data['line_items'][$index]['product_id'] = (int) $default_product_id;<br />                }<br />            }<br />        }<br />    }<br />    return $response;<br />}

We didn't check this solution or had the chance to get to the bottom of this case. Please use it only if you are certain it can help in your case. And please take a backup of your site before proceeding.
If this solution does not resolve your issue or seems outdated, we recommend checking the related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket.

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.

This topic contains 2 replies, has 0 voices.

Last updated by ivanV-42 9 months, 1 week ago.

Assisted by: Itamar.

Author Posts
July 16, 2025 at 7:13 am #17240196

ivanV-42

Background of the issue:
I am trying to ensure that the variation_id remains the same for all languages in WPML Woocommerce. With an update, the variation_ids are now different for each language. Before the update, the variation_id was the same for all languages. As you can see here before: EN: hidden link FR: hidden link line_items > variation_id is both 20163 But now for FR it is 21653 hidden link

Symptoms:
The variation_id is different for each language after an update, whereas it was the same for all languages before.

Questions:
How can we change this back so that the variation_id is always the same for all languages?

July 17, 2025 at 12:58 pm #17246030

Itamar
WPML Supporter since 02/2016

Languages: Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

I checked the order 32745 at this link: hidden link

I don't see that the variation_id is 21653 as you said. I see it is 20163 as you expected it to be. Please see the attached screenshot.

If I'm missing something, please elaborate and explain what I am missing.

Also, you wrote: "The variation_id is different for each language after an update, whereas it was the same for all languages before."

Can you please explain which update you are referring to?
Did you update the product and its translation, or maybe an update to our plugins?

Regards,
Itamar.

2025-07-17_15-51-23.jpg
July 24, 2025 at 7:34 am #17266240

ivanV-42

Hi Itamar,

I handled it myself by adding following code to my functions.php.

/* AFTER UPDATE: VARIATION ID DIFFERENT FOR ALL LANGUAGES - CHANGE BACK TO DEFAULT LANGUAGE VARIATION_ID IN ALL LANGUAGES */
add_filter('woocommerce_rest_prepare_shop_order_object', 'force_variation_ids_to_default_language', 10, 3);
function force_variation_ids_to_default_language($response, $order, $request) {
if (!is_a($order, 'WC_Order')) return $response;

// Haal de default taal van WPML
$default_lang = function_exists('wpml_get_default_language') ? wpml_get_default_language() : 'nl';

// Haal de taal waarin deze bestelling is geplaatst
$order_lang = $order->get_meta('wpml_language');

// Alleen aanpassen als de order NIET in de default taal is
if ($order_lang && $order_lang !== $default_lang) {
foreach ($response->data['line_items'] as $index => $item) {
// === Fix variation_id ===
if (!empty($item['variation_id'])) {
$current_variation_id = $item['variation_id'];

// Zet deze variation ID om naar de default taal
$default_variation_id = apply_filters(
'wpml_object_id',
$current_variation_id,
'product_variation',
false,
$default_lang
);

if ($default_variation_id) {
$response->data['line_items'][$index]['variation_id'] = (int) $default_variation_id;
}
}

// === Fix product_id ===
if (!empty($item['product_id'])) {
$current_product_id = $item['product_id'];

$default_product_id = apply_filters(
'wpml_object_id',
$current_product_id,
'product',
false,
$default_lang
);

if ($default_product_id) {
$response->data['line_items'][$index]['product_id'] = (int) $default_product_id;
}
}
}
}

return $response;
}