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 9 months, 1 week ago.
Assisted by: Itamar.
