Skip Navigation

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

Problem:
If you're using

apply_filters('wpml_copy_post_to_language')

to create translations of products and find that it does not copy product attributes, you might encounter difficulties in achieving complete product translations.
Solution:
First, ensure that in WPML > Settings, under Taxonomy translation, the translation preference for Attributes is set to "Copy". If this does not resolve the issue, you can manually copy product attributes using custom code. Here's a step-by-step guide:
1. Create an independent translation of the post in the target language using

$translated_post_id = apply_filters('wpml_copy_post_to_language', $product->get_id(), $target_language, false);

2. Retrieve the translated product with

$translated_product = wc_get_product($translated_post_id);

3. Get attributes from the original product with

$attributes = $product->get_attributes();

4. Set these attributes to the translated product with

$translated_product->set_attributes($attributes);

5. Save the translated product with

$translated_product->save();

If you need further customization, consider hiring a developer from WPML Contractors.

Please note that this solution might be outdated or not applicable to your specific case. We 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. If the issue persists, please 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.

Our next available supporter will start replying to tickets in about 7.65 hours from now. Thank you for your understanding.

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Prosenjit Barman 1 month ago.

Assisted by: Prosenjit Barman.

Author Posts
August 19, 2024 at 4:13 am #16082008

lubomirJ

Background of the issue:
I'm using apply_filters('wpml_copy_post_to_language') to create a translation of products in my plugin. It works great, copies the title, description, photos, and product categories, but it does not copy the product attributes for some reason. I also tried using wpml_make_post_duplicates, but that marked the translated product as a duplicate and I need it to be an independent copy, so that I can manually update the title, description, etc. The site I need help with is hidden link, but it also happens on a separate WP install on my localhost, even when I deactivate all other plugins.

Symptoms:
The function wpml_copy_post_to_language does not copy product attributes.

Questions:
What am I doing wrong with wpml_copy_post_to_language that it does not copy product attributes?
How can I create an independent copy of a product with all attributes using WPML?

August 20, 2024 at 8:36 am #16086173

Prosenjit Barman
Supporter

Languages: English (English )

Timezone: Asia/Dhaka (GMT+06:00)

Hello There,
Thanks for contacting WPMl Support.

The issue you're facing with wpml_copy_post_to_language not copying product attributes is likely due to how WPML handles product attributes during the translation process. Product attributes are stored as taxonomy terms, and these might not be automatically copied over when creating a translation, depending on how the function is implemented.

Please go to "WPML > Settings" and from the Taxonomy translation, please make sure that the translation preference for the Attributes is set to "Copy".

If the issue persists even after that, you might consider writing a custom function to manually copy product attributes. You can do this by retrieving the product attributes from the original product and then applying them to the translated product after it has been created.

Please note that custom coding support is beyond the scope of our services. For that reason, we can only help by providing suggestions that might help. We recommend hiring a developer who specializes in customizing WPML and its addons, which you can do through this link. I hope they can help in achieving what you're looking for.

Please feel free to let me know if you have any other questions or need clarification. I will be happy to help.

Best regards,
Prosenjit

August 22, 2024 at 4:44 am #16092561

lubomirJ

Hi Prosenjit,

your proposed solutions didn't help.

I made it work using custom code. Hopefully it will help others who might have the same issue.

// CREATE independent translation of the post in the target language
$translated_post_id = apply_filters( 'wpml_copy_post_to_language', $product->get_id(), $target_language, false );

// GET the product translation
$translated_product = wc_get_product( $translated_post_id );

// GET attributes from the original product
$attributes = $product->get_attributes();

// SET attributes to the translated product
$translated_product->set_attributes( $attributes );

// SAVE the translated product
$translated_product->save();

lubomirJ confirmed that the issue was resolved on 2024-08-22 04:44:34.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.