Home›Support›English Support›[Resolved] Cron Job - translated products displaying as not having attributes
[Resolved] Cron Job - translated products displaying as not having attributes
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.
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.
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.
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() .
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.
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.
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:
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.