Skip Navigation

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

Problem:
The client needed to access translated product attributes in WPML and was attempting to modify the code to get attributes in a specific language ('ru' for Russian), but the applied changes were not working.

Solution:
1. We informed the client that custom coding is beyond our support scope and directed them to our support policy for clarification.
2. We provided links to our hooks reference, which could help in finding the necessary information to solve the problem:

3. We requested the client's Debug information to better understand the site configuration.
4. We provided a code snippet that retrieves the translated value and label of product attributes, which works based on the page's language. The code is available on Pastebin: https://pastebin.com/0HqyaRqw.

If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. We also highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum: 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.

Tagged: 

This topic contains 18 replies, has 2 voices.

Last updated by Osama Mersal 1 year, 2 months ago.

Assisted by: Osama Mersal.

Author Posts
February 20, 2024 at 4:48 am #15320532

christopherW-28

I am updating some code to use WPML. The original code (as follows) works but only gets the attributes in the original language:

...

$attributes = $this->get_product()->get_attributes();

foreach ( $attributes as $param ) {
$param_val = $this->get_product()->get_attribute( wc_attribute_taxonomy_name_by_id( $param->get_id() ) );

...

I changed the last line to:

$param_val = apply_filters('wpml_object_id', $param->get_id(), 'product', false, 'ru' );

and also:

$param_val = apply_filters('wpml_object_id', $param->get_id(), 'product_attribute', false, 'ru' );

but it does not work.

How do I access the translated product attributes?

Thanks

February 20, 2024 at 9:48 am #15321463

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for contacting WPML forums support. I'll be glad to help you today.

1) Unfortunately, custom coding is out of our support scope. Kindly check our support policy. (https://wpml.org/purchase/support-policy/)

You can find what you need in our hooks reference. Kindly check the following:

1- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

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

2) Could you please share your Debug information with me?
You can read a detailed explanation about it here. (http://wpml.org/faq/provide-debug-information-faster-support)
The debug info will give me much information about how your site is configured.

Best regards,
Osama

February 20, 2024 at 9:58 am #15321510

christopherW-28

Hi

I think you misunderstand. I do not want you to write any custom code for me but direct me to information, or better yet, example code showing how to retrieve the translation of a WooCommerce product attribute.

Thanks

February 20, 2024 at 10:35 am #15321781

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for your reply. The information can be found in the references in my previous reply.

The following examples might help you write the correct function for your case.

$attributes = $this->get_product()->get_attributes();

foreach ($attributes as $attribute) {
    // Get the taxonomy name (for global attributes) or the custom attribute name
    $taxonomy_name = wc_attribute_taxonomy_name_by_id($attribute->get_id());

    // For global attributes, you might want to ensure the attribute name is translated
    // This step is more about translating attribute names if they're displayed somewhere
    $translated_taxonomy_name = apply_filters('wpml_translate_single_string', $taxonomy_name, 'WordPress', 'Product attribute name');

    // Retrieve the attribute value(s), WooCommerce should return them in the current language by default
    $param_val = $this->get_product()->get_attribute($translated_taxonomy_name);
}

Or

$attributes = $this->get_product()->get_attributes();

foreach ($attributes as $attribute) {
    // Check if the attribute is a taxonomy
    if ($attribute->is_taxonomy()) {
        $attribute_taxonomy_name = $attribute->get_name();
        $terms = wp_get_post_terms($this->get_product()->get_id(), $attribute_taxonomy_name);

        foreach ($terms as $term) {
            // Translate the term ID to the current language
            $translated_term_id = apply_filters('wpml_object_id', $term->term_id, $attribute_taxonomy_name, true, 'ru');
            if ($translated_term_id !== null) {
                $translated_term = get_term_by('id', $translated_term_id, $attribute_taxonomy_name);
                echo $translated_term->name . PHP_EOL; // Output the translated term name
            }
        }
    } else {
        // For custom attributes, directly display the value
        // Custom attributes are stored as serialized arrays and are not necessarily translatable via WPML without custom logic
        echo $attribute->get_data()['value'] . PHP_EOL;
    }
}

Please keep in mind that these are basic examples and might need adjustments based on your specific setup, especially regarding attribute handling and the exact context where WPML expects the string to be registered for translation. Also, ensure your product attributes and translations are correctly set up in WPML.

I hope this helps you. If you need further assistance, please let me know.

Best regards,
Osama

February 20, 2024 at 3:17 pm #15323700

christopherW-28

Hi

Thanks for your reply.

Neither of these solutions works, they simply return the original (English) attribute terms.

For the first version (I have tried with added 'ru' parameter and without, same result:

$taxonomy_name = wc_attribute_taxonomy_name_by_id($param->get_id());
$translated_taxonomy_name = apply_filters('wpml_translate_single_string', $taxonomy_name, 'WordPress', 'Product attribute name', 'ru');
$param_val = '1. ' . $taxonomy_name . ', ' . $translated_taxonomy_name . ', ' . $this->get_product()->get_attribute($translated_taxonomy_name);

The output for $param_val is:

1. pa_country, pa_country, United Kingdom

For the second version:

foreach ( $attributes as $param ) {
// Check if the attribute is a taxonomy
if ($param->is_taxonomy()) {
$attribute_taxonomy_name = $param->get_name();
$terms = wp_get_post_terms($this->get_product()->get_id(), $attribute_taxonomy_name);

foreach ($terms as $term) {
// Translate the term ID to the current language
$translated_term_id = apply_filters('wpml_object_id', $term->term_id, $attribute_taxonomy_name, true, 'ru');
if ($translated_term_id !== null) {
$translated_term = get_term_by('id', $translated_term_id, $attribute_taxonomy_name);
$param_val = $attribute_taxonomy_name . ', ' . $translated_term_id . ', ' . $translated_term->name; // Output the translated term name

...

The output is:

Pa_country, 245, United Kingdom

I have been on this for two days and it seems nothing works.

Any suggestions?

February 20, 2024 at 3:57 pm #15323989

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Please login to this sandbox site and try to replicate the issue. (hidden link)

If the issue is replicable, please let me know how to reproduce it.

Thanks for your cooperation

Best regards,
Osama

February 21, 2024 at 12:20 pm #15327736

christopherW-28

Hello

I can't set up on the sandbox - it will take far too much time.

I came across someone having the same issue in your forums:

https://wpml.org/forums/topic/display-the-translation-of-a-woocommerce-product-attribute/

However, I can't see the solution for getting the attribute term as it is a hidden link, can you share it?

Also, I extracted the code and put it in its own clean function in functions.php, it generates the output you can see on this page:

hidden link

As you can see below, this is your suggested code but it just returns the English version of the attribute.

And, for the translated taxonomy name, for example, "brand" it returns the original language slug, but there is nothing in the translation (see image) - is this the problem?

Code for test page as follows ...

function testing_atts(){

global $product;

$product_id = 130;
$product = wc_get_product( $product_id );

$ru_product_id = apply_filters( 'wpml_object_id', $product_id, 'product', true, 'ru' );
$ru_product = wc_get_product( $ru_product_id );
$ru_description = $ru_product->get_description();
$ru_title = $ru_product->get_title();

$attributes = $product->get_attributes();

$param_val = "<div><h2>' . $ru_title . '</h2><p>" . $ru_description . '</p><table><th>Taxonomy ID</th><th>Taxonomy name</th><th>Translated taxonomy name</th><th>Translated attribute value</th>';

foreach ( $attributes as $param ) {

$param_val = $param_val . '<tr>';

$taxonomy_name = wc_attribute_taxonomy_name_by_id($param->get_id());

$translated_taxonomy_name = apply_filters('wpml_translate_single_string', $taxonomy_name, 'WordPress', 'Product attribute name', 'ru');

$param_val = $param_val . '<td>'. $param->get_id() . '</td><td>' . $taxonomy_name . '</td><td>' . $translated_taxonomy_name . '</td><td>' . $product->get_attribute($translated_taxonomy_name) . '</td>';

$param_val = $param_val . '</tr>';

}

$param_val = $param_val . '</table></div>';

return $param_val;

}

1.png
February 21, 2024 at 12:49 pm #15327966

christopherW-28

Sorry to send two replies close together ...

I set up on the sandbox, copied the code to functions.php, created a test product, "brand" attribute and you can see the results here:

hidden link

Its the same output. No translations for attribute terms etc.

February 21, 2024 at 12:56 pm #15328009

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

1) Here are the links in the mentioned ticket.

1- hidden link

2- https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/#get-product-taxonomy-terms-product-categories-tags-attributes-shipping-classes

2) I translated the page, and the translated attribute value appear correctly. (hidden link)

Best regards,
Osama

February 21, 2024 at 1:22 pm #15328176

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

I made some edits to the code to get the translated taxonomy name. Please check this page. (hidden link)

Note: The new function is in the theme's function.php file.

Best regards,
Osama

February 21, 2024 at 1:43 pm #15328301

christopherW-28

OK, so I can see that it is working on a Russian page now, but .... I need the translated attribute value regardless of what the page language is in.

These values are to be output to a file.

February 21, 2024 at 1:47 pm #15328312

christopherW-28

Sorry I sent that before your email - there are no edits to the function.php file. I think I had it open and they maybe gone?

February 21, 2024 at 1:51 pm #15328320

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Please try this code. (https://pastebin.com/0HqyaRqw)

It gets the translated value and label. (hidden link)

However, it depends on the page's language. I'm not sure how to get it regardless of the language, but you can try to tweak the code.

Best regards,
Osama

February 21, 2024 at 2:08 pm #15328544

christopherW-28

Hi

Thanks for all your assistance.

According to the documentation: https://wpml.org/wpml-hook/wpml_translate_single_string/

Adding the language code should return the translation in this language, so I added 'ru' as the language code, but it still returns in the default language.

$translated_taxonomy_name = apply_filters('wpml_translate_single_string', $taxonomy_name, 'WordPress', 'Product attribute name', 'ru');

Did I miss something, wrong language code or something?

February 21, 2024 at 2:24 pm #15328654

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Please check if this is the output you want. (hidden link)

Best regards,
Osama