Skip Navigation

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

Problem:
You are using the Advanced Product Fields plugin for WooCommerce and have successfully translated the product options on the product page. However, when products are added to the cart, the option values still appear in the original language, not the translated version.
Solution:
We recommend adding the following code to the

functions.php

file of your theme. Please ensure you perform a full site backup before implementing this code:

// Add the filter with a higher priority (lower number) than the plugin's filter
add_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10, 4);

function translate_wapf_fieldgroup_post_meta($value, $post_id, $meta_key, $single) {
    if ($meta_key === '_wapf_fieldgroup') {
        // Temporarily remove the filter to avoid infinite loops
        remove_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10);

        // Get the original meta value
        $fieldgroup_meta = get_post_meta($post_id, $meta_key, false);

        // Re-add the filter
        add_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10, 4);
		
		if (!empty($fieldgroup_meta) && is_array($fieldgroup_meta)) {
            foreach ($fieldgroup_meta as &$group) {
				if (isset($group['id']) && preg_match('/p_(\d+)/', $group['id'], $matches)) {
                    $post_id = $matches[1];
                    $translated_post_id = apply_filters('wpml_object_id', $post_id, 'post', true);
                    $group['id'] = str_replace("p_$post_id", "p_$translated_post_id", $group['id']);
                }
			}
		}

        return $fieldgroup_meta;
    }

    return $value;
}

This solution addresses the issue where the plugin stores the Advanced Product fields in _wapf_fieldgroup as a multidimensional array, which may not update correctly across different languages in the cart.

Please note, this workaround may not function correctly if languages are switched on the Cart or Checkout pages. To prevent potential issues, enable the "Prompt for a confirmation and reset the cart" option. For more details, see this article.

If this solution does not resolve your issue or seems outdated, please check related known issues, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. If needed, do not hesitate to open a new support ticket at WPML support forum.

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.

No supporters are available to work today on this forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Bigul 4 months, 2 weeks ago.

Assisted by: Bigul.

Author Posts
June 28, 2024 at 2:51 pm

Rico Heinrich

Background of the issue:
We are using the Advanced Product Fields plugin for custom product options. The translation on the product page works nicely using your guide here: https://wpml.org/de/forums/topic/advanced-product-fields-for-woocommerce-werden-in-uebersetzung-nicht-angezeigt/ or here: https://wpml.org/forums/topic/problem-with-translating-advanced-product-fields-pro-for-woocommerce/

Symptoms:
When the product is put into the cart it still shows the original non-translated option values there.

Questions:
Why are the translated option values not showing in the cart?
Is there a specific setting in WPML or the Advanced Product Fields plugin that needs to be adjusted?

June 28, 2024 at 4:16 pm
June 28, 2024 at 5:00 pm #15853485

Rico Heinrich

OK. I set up the test store with one product, advanced product fields and translations.

Please find the product here in English:
hidden link

You can see that the advanced product options are translated from German (German is default language) to English. They are called "Example Field, Selection 1, Selection 2".

Now check Selection 1 and Selection 2 and put the product into the cart.

In the cart the options are not translated (see attached screenshot).

Thanks in advance!

Bildschirmfoto 2024-06-28 um 18.55.39.png
June 30, 2024 at 9:53 am #15861948

Bigul
Supporter

Languages: English (English )

Timezone: Europe/Vienna (GMT+01:00)

Hello,

Thank you for reproducing the issue on the sandbox site. The ticket is escalated to our compatibility team for further debugging. We will get back to you as early as possible. Please wait.

--
Thanks!

Bigul

July 4, 2024 at 5:46 pm #15893593

Bigul
Supporter

Languages: English (English )

Timezone: Europe/Vienna (GMT+01:00)

Hello,

We are getting the expected results in the sandbox site after placing the following code in the functions.php file of the theme. Please try it after a full site backup and make sure the issue exists or not.

// Add the filter with a higher priority (lower number) than the plugin's filter
add_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10, 4);

function translate_wapf_fieldgroup_post_meta($value, $post_id, $meta_key, $single) {
    if ($meta_key === '_wapf_fieldgroup') {
        // Temporarily remove the filter to avoid infinite loops
        remove_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10);

        // Get the original meta value
        $fieldgroup_meta = get_post_meta($post_id, $meta_key, false);

        // Re-add the filter
        add_filter('get_post_metadata', 'translate_wapf_fieldgroup_post_meta', 10, 4);
		
		if (!empty($fieldgroup_meta) && is_array($fieldgroup_meta)) {
            foreach ($fieldgroup_meta as &$group) {
				if (isset($group['id']) && preg_match('/p_(\d+)/', $group['id'], $matches)) {
                    $post_id = $matches[1];
                    $translated_post_id = apply_filters('wpml_object_id', $post_id, 'post', true);
                    $group['id'] = str_replace("p_$post_id", "p_$translated_post_id", $group['id']);
                }
			}
		}

        return $fieldgroup_meta;
    }

    return $value;
}

This bug occurs because the plugin stores the Advanced Product fields in _wapf_fieldgroup as a multidimensional array.

Please note that this workaround will not work if we switch languages on the Cart or Checkout pages. However, switching languages in these areas is not common or recommended. Therefore we suggest enabling the "Prompt for a confirmation and reset the cart" option to avoid it. For more details, refer to the following article.

https://wpml.org/documentation/related-projects/woocommerce-multilingual/clearing-cart-contents-when-language-or-currency-change/

--
Thanks!

Bigul

July 4, 2024 at 8:17 pm #15893968

Rico Heinrich

Thank you very much, it works perfectly now.