Skip Navigation

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

Problem:
You are encountering an issue where, after running a job every hour to update products and attributes in the primary language, the translated products appear without attributes despite having translated attributes created.
Solution:
We recommend using WPML API hooks to update the attributes of translated products programmatically after updating the attributes of default language products. Here is an example code designed for a cron job context:

add_action('wssupp_custom_cron_job', 'wssupp_custom_task_function');<br />function wssupp_custom_task_function() {<br /><br />    // update the product attributes of a specific product as a demo<br />    $product_id = 21;<br />    $attribute = 'pa_color';<br />    $term_slug = 'yellow';<br /><br />    $original_term = get_term_by( 'slug', $term_slug, $attribute );<br />    $default_language = apply_filters( 'wpml_default_language', NULL );<br /><br />    // Step 1. Programmatically set product attribute for sample default-language post<br />    wp_set_object_terms( $product_id, $term_slug, $attribute );<br /><br />    // Step 2. Get the translations of the product to other languages<br />    $product_trid = apply_filters( 'wpml_element_trid', NULL, $product_id, 'post_product' );<br />    $product_translations = apply_filters( 'wpml_get_element_translations', NULL, $product_trid, 'post_product' );<br /><br />    // Step 3. Get the term translations<br />    $term_trid = apply_filters( 'wpml_element_trid', NULL, $original_term->term_id, 'tax_'.$attribute );<br />    $term_translations = apply_filters( 'wpml_get_element_translations', NULL, $term_trid, 'tax_'.$attribute );<br /><br />    // Step 4. Iterate over the product translations and update product attribute for each<br />    foreach ($product_translations as $lang => $product_translation) {<br /><br />        if ( $lang == $default_language ) {<br />            continue; // skip default language<br />        }<br /><br />        wp_set_object_terms( $product_translation->element_id, $term_translations[$lang]->term_id, $attribute );<br />    }<br />}<br />

For further details, refer to our WPML Hooks Reference. You should adjust the code to your needs.

If this solution does not resolve your issue or seems outdated, please check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. We highly recommend opening a new support ticket if needed.

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 12 replies, has 2 voices.

Last updated by Itamar 3 months ago.

Assisted by: Itamar.

Author Posts
May 29, 2024 at 7:22 am

dimitrisD-7

we are facing an issue where we are running every hour a job that updates the products and the attributes in primary language and then save them, as a result the translated products displaying as not having attributes despite the fact that we have created the translated attributes. If we run manually the synchronize attributes and update variant products the attributes are restored.

May 29, 2024 at 8:17 am
May 29, 2024 at 8:21 am #15682061

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

This ticket has been inactive for quite a while. I didn't get a reply from you. So, I've transformed this chat into a support ticket because you may still need our help.

I could access your staging site. Please proceed with the following.

A. Update WooCommerce to its latest version.
b. Update our plugins to their latest versions. You can see the latest versions here: https://wpml.org/account/downloads/.
c. If the above does not help, point us directly to the piece of code that is in charge for updating the products and the attributes.

Regards,
Itamar.

June 5, 2024 at 9:43 am #15706007

dimitrisD-7

Hello,

we have perform the steps A and B but we still have that issue.

I have update Woocommerce as well as your plugins to their latest version in the staging site too.

The code that update the product is inside the plugin Woocommerce Connection with Envision ERP in folder includes/woocommerce/Woo_Product.php the fucntion's name is update_product() .

Thank you in advance.

June 5, 2024 at 2:00 pm #15707262

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

I logged into your site and used the Plugin File Editor. I'm sorry, but I can't see the Woo_Product.php file you mentioned.

Please share the update_product() function you created here.

Thanks,
Itamar.

June 6, 2024 at 7:33 am #15709574

dimitrisD-7

Hello,

the file that I'm referring to is the following:

hidden link

Thank you.

June 6, 2024 at 10:21 am #15710275

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Thanks,

Your plugin is unknown to us. It is not on our list of officially compatible plugins here - https://wpml.org/plugin/. Moreover, it is not available to download or purchase publicly.

As I already mentioned in our chat, supporting custom code is out of the scope of our support forum. However, I presented this case to our second-tier supporters and asked for their advice. When I have their reply, I'll update you here.

Thank you for your patience.
Itamar.

June 10, 2024 at 7:28 am #15720386

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

Here are the suggestions from our second-tier supporter.

I suggest they review our code which is triggered when you run that troubleshooting batch process.

That is handled in the file sitepress-multilingual-cms/classes/troubleshoot/class-wpml-troubleshoot-sync-posts-taxonomies.php. They should note the function synchronize_batch. See how for each active language that calls a utility function to sync_terms.

That is defined in sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translation-utils.class.php, with the actual functionality in the private method synchronize_terms.

We hope that this information will help you achieve what you need.

Regards,
Itamar.

June 10, 2024 at 1:07 pm #15722149

dimitrisD-7

Hello,

I added some code to update_product function according to your instructions but it still does not synchronize the terms in english.

Can you take a look?

Thank you.

June 10, 2024 at 1:15 pm #15722158

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

Reviewing your entire code/plugin is out of the scope of our support forum.

Please send us the code you added, and we will check if you did something wrong.

Otherwise, if you need further help with what you are trying to achieve, I would suggest you consider contacting one of our certified partners from this link:

https://wpml.org/contractors/

Before doing so, please have a look also here:

https://wpml.org/documentation/about-wpml-contractors/what-type-of-work-you-can-expect-from-contractors/

With our contractors, you might get the custom assistance you need to get on with your project.

Regards,
Itamar.

June 10, 2024 at 1:24 pm #15722191

dimitrisD-7

Hello,

ok I have added

require_once (ABSPATH . 'wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translation-utils.class.php');

at the top to include the wpml sync fucntion inside my code.

Then when I update a product I have add the following:

global $sitepress;
$wpml_term_translation_utils = new \WPML_Term_Translation_Utils($sitepress);

in order to initiate the class.

And finally I call the sync_terms function:

$wpml_term_translation_utils->sync_terms( $product_id, $lang );

Thank you.

June 10, 2024 at 3:35 pm #15723054

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

I've sent this to our second-tier supporter to see if he has any input.

I'll update you once I have his reply.

Regards,
Itamar.

June 19, 2024 at 9:29 am #15754452

dimitrisD-7

Hello,

do you have any update regarding my issue?

Thank you.

June 19, 2024 at 6:26 pm #15758030

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

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

Hi,

Our second-tier supporter wrote an example code especially for you with WPML API hooks to update product attributes of translations after programmatically updating product attributes of default language products. The following code demonstrates the concept to study and adapt as required. The second-tier supporter wrote this code in the context of a cron job callback to verify that it works in that context. In step 1, our second-tier supporter updates the product attributes of some test products in the default language. The subsequent steps are those that are relevant to you and should be self-explanatory.

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

Here's the code:

add_action('wssupp_custom_cron_job', 'wssupp_custom_task_function');
function wssupp_custom_task_function() {

    // update the product attributes of a specific product as a demo
    $product_id = 21;
    $attribute = 'pa_color';
    $term_slug = 'yellow';

    $original_term = get_term_by( 'slug', $term_slug, $attribute );
    $default_language = apply_filters( 'wpml_default_language', NULL );

    // Step 1. Programmatically set product attribute for sample default-language post
    wp_set_object_terms( $product_id, $term_slug, $attribute );

    // Step 2. Get the translations of the product to other languages
    $product_trid = apply_filters( 'wpml_element_trid', NULL, $product_id, 'post_product' );
    $product_translations = apply_filters( 'wpml_get_element_translations', NULL, $product_trid, 'post_product' );

    // Step 3. Get the term translations
    $term_trid = apply_filters( 'wpml_element_trid', NULL, $original_term->term_id, 'tax_'.$attribute );
    $term_translations = apply_filters( 'wpml_get_element_translations', NULL, $term_trid, 'tax_'.$attribute );

    // Step 4. Iterate over the product translations and update product attribute for each
    foreach ($product_translations as $lang => $product_translation) {

        if ( $lang == $default_language ) {
            continue; // skip default language
        }

        wp_set_object_terms( $product_translation->element_id, $term_translations[$lang]->term_id, $attribute );
    }
}

We hope that this will help you achieve what you need.

Regards,
Itamar.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.