Skip to content Skip to sidebar

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

Problem:
The client is experiencing issues with ACF fields in a multilingual WooCommerce setup using WPML. Specifically, when updating a product in a secondary language, fields such as repeaters and relationship fields incorrectly inherit or duplicate content from the primary language product, despite being set to 'Copy once'.
Solution:
We recommend the following steps to address the issue:
1. Create a full backup of the site.
2. Navigate to

wp-content/plugins/sitepress-multilingual-cms/classes/custom-field-translation/class-wpml-copy-once-custom-field.php

and edit the

WPML_Copy_Once_Custom_Field::copy

method.
3. Replace the existing code with the following:

if ( ! get_post_meta( $post_id, 'copyoncedone' ) == 1 ) {<br />    add_post_meta($post_id, 'copyoncedone', 1 );<br />    foreach ( $custom_fields_to_copy as $meta_key ) {<br />        $values = isset( $custom_fields[ $meta_key ] ) ? $custom_fields[ $meta_key ] : [];<br />        if ( ! empty( $values ) ) {<br />            $values = array( $values );<br />        }<br />        $values = apply_filters(<br />            'wpml_custom_field_values',<br />            $values,<br />            [<br />                'post_id'                   => $post_id,<br />                'meta_key'                  => $meta_key,<br />                'custom_fields_translation' => WPML_COPY_ONCE_CUSTOM_FIELD,<br />            ]<br />        );<br /><br />        if ( empty ( $values ) && $source_element_id ) {<br />            $this->sitepress->sync_custom_field( $source_element_id, $post_id, $meta_key );<br />        }<br />    }<br />}

4. Save the changes and verify if the issue is resolved.

If this solution does not apply to your case, or if it becomes outdated, 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 problem persists, please open a new support ticket.

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.

This topic contains 17 replies, has 2 voices.

Last updated by Carlos Rojas 1 year, 1 month ago.

Assisted by: Carlos Rojas.

Author Posts
June 4, 2024 at 2:52 pm #15702627

Carlos Rojas
WPML Supporter since 03/2017

Languages: English (English ) Spanish (Español )

Timezone: Europe/Madrid (GMT+02:00)

Hi there,

Our 2nd tier specialists and I can't reproduce the issue on the staging site. Did you make any changes? Can you still reproduce the issue?

June 5, 2024 at 6:20 am #15704939

mortenS-12

I can still reproduce the error and we didn't edit anything.

If you edit this new product i created:
hidden link

and change the product description (just add a word to it).

You will see that the rows from the danish product in the "hvad" tab of the ACF fields, will be added (Now it won't have the content from the danish, but it will copy the rows).

So the danish product has a row in these repeaters under the "Hvad" tab:

"Hvad" - 1 row
"Hvad second row" - 1 row
"Hvad three columns" - 1 row

The english version has no rows in any of them, if you update any part of the product it will add the rows in the repeaters. To recreate it after it happens, you can delete the rows on the english product. Update the product (everything is fine now) but when you again change any part of the product (like the product description) and update the product the second time, the removed rows will return.

Note this is not only the "hvad" tab, it's also the other ACF fields on the products that does this. If the danish product has a row, but the english don't it will import the row to the english product after you change any part of the english product. Depending on the translation option(copy once, translate etc.), it will either import the row and the content or only the row.

June 5, 2024 at 7:51 am #15705400

Carlos Rojas
WPML Supporter since 03/2017

Languages: English (English ) Spanish (Español )

Timezone: Europe/Madrid (GMT+02:00)

Thank you very much for double-checking the issue and sharing the steps again. I have updated our 2nd tier and we will continue working on this issue.

June 7, 2024 at 11:12 am #15715189

Carlos Rojas
WPML Supporter since 03/2017

Languages: English (English ) Spanish (Español )

Timezone: Europe/Madrid (GMT+02:00)

Hi there,
Please apply this workaround

- Create a full backup of the site
- Edit \WPML_Copy_Once_Custom_Field::copy in wp-content/plugins/sitepress-multilingual-cms/classes/custom-field-translation/class-wpml-copy-once-custom-field.php
- Change

if ( empty( $values ) && $source_element_id ) {
				$this->sitepress->sync_custom_field( $source_element_id, $post_id, $meta_key );
			}

to

if ( ! get_post_meta( $post_id, 'copyoncedone' ) == 1 ) {
			add_post_meta($post_id, 'copyoncedone', 1 );
			foreach ( $custom_fields_to_copy as $meta_key ) {
				$values = isset( $custom_fields[ $meta_key ] ) ? $custom_fields[ $meta_key ] : [];
				if ( ! empty( $values ) ) {
					$values = array( $values );
				}
				/**
				 * Custom fields values for given post obtained directly from database
				 *
				 * @param array<mixed> $values Custom fields values as they are in the database
				 * @param array<int|string> $args {
				 *
				 * @type int $post_id ID of post associated with custom field
				 * @type string $meta_key custom fields meta key
				 * @type int $custom_fields_translation field translation option
				 *
				 * }
				 * @since 4.1
				 *
				 */
				$values = apply_filters(
					'wpml_custom_field_values',
					$values,
					[
						'post_id'                   => $post_id,
						'meta_key'                  => $meta_key,
						'custom_fields_translation' => WPML_COPY_ONCE_CUSTOM_FIELD,
					]
				);

				if ( empty ( $values ) && $source_element_id ) {
					$this->sitepress->sync_custom_field( $source_element_id, $post_id, $meta_key );
				}
			}
}

- Save the changes and double-check if the issue disappeared