Skip Navigation

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

Last updated by Andreas 1 year, 11 months ago.

Assisted by: Andreas.

Author Posts
December 2, 2022 at 3:03 pm #12579397

olivierA-13

My problem is the same one that has been described here but using the default WooCommerce taxonomies, not custom ones: https://wpml.org/forums/topic/translate-custom-taxonomy-via-api-rest/
I am trying to create translations of product categories and product tags using the REST API, but some of the terms have matching names since they are called the same in different languages, so I keep getting the error 'A term with the name provided already exists in this taxonomy.'

December 2, 2022 at 7:37 pm #12580651

Andreas

Hello there

Thank you for contacting us. I am happy to help you.

So to understand, you are using our REST API , try to add in secondary language a term with the same name and you are getting error?

Here is the documentation just in case :

https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/

If the above is true, I need to show me the data that you are trying to pass in creating and translating a term in order to replicate the issue locally.

Regards,
Andreas

December 7, 2022 at 2:27 pm #12607841

olivierA-13

Hi Andreas

I have narrowed down the issue in meantime, and I am not actually sure if this is even a WPML related issue.
Because I couldn't create product categories or tags with custom fields in a single request (whatever data is there just gets ignored during creation), I decided to do 2 requests each time. First using the wpml REST API to create the category/tag including the needed wpml attributes like lang, translationOf etc.
And then a second update request that sets the ACF fields using the wordpress REST API.

Now here is a program flow that fails:
1. Create a tag using the wpml REST API (products/tags)

{
  "name": "Coimbra",
  "description": null,
  "lang": "en",
  "acf": {
    "directus_id": "113",
    "tile_image": null
  },
}

=> Creates tag with id 374 and slug 'coimbra'

2. Update the acf fields using the wordpress REST API (/wp-json/wp/v2/product_tag/374)

{
  acf: {
    "directus_id": "113",
    "tile_image": null
  }
}

=> Works just fine

3. Create a translation (lang: pt) using the wpml REST API (products/tags)

{
  "name": "Coimbra",
  "description": null,
  "lang": "pt",
  "acf": {
    "directus_id": "113",
    "tile_image": null
  },
  "translation_of": 374,
}

=> creates tag with id 375 and slug 'coimbra-2'

4. Update the acf fields using the wordpress REST API (/wp-json/wp/v2/product_tag/375)

{
  acf: {
    "directus_id": "113",
    "tile_image": null
  }
}

=> Fails with the following error:

{
  "code": "duplicate_term_slug",
  "message": "The slug "coimbra-2" is already in use by another term.",
  "data": null
}

The tags are created and are recognized by wpml as translations.
It's just that any update on the translated tag seems to fail with that error, so I am unable to set the acf fields on that tag.

And the error doesn't really make sense to me because the slug that is supposedly duplicated is the one that belongs to the tag I just created and am trying to update...

Best regards
Olivier

December 7, 2022 at 7:50 pm #12609695

Andreas

Thank you for the detailed explanation.

First of all, I would like to inform you that I have never tested a scenario with ACF fields in the taxonomies of WooCommerce.

Also, we only have official support WooCommerce Rest API with a combination of WooCommerce Multilingual. WPML is not officially supported Rest API so I am not sure about the results you are getting.

From my point of view , you are trying to update ACF fields with WordPress Rest API endpoint, so I am not sure if this is related to WPML.

What can I suggest to you is this :

When you create the taxonomy in the rest call, pass also the slug attribute and define something specific for the taxonomy, right now the slug created automatically. For example try to define the slug as coimbra for ENG and coimbra-pt for PT and see if this can help you.

Regards,
Andreas

December 10, 2022 at 11:34 am #12625629

olivierA-13

I have tried your suggestion and created the slugs in that format. Unfortunately that didn't make any difference.
As a workaround I now ended up just creating a tiny custom REST endpoint that takes my data and uses the 'update_field()' function to set the acf fields.
Thank you nevertheless.

Regards,
Olivier

December 12, 2022 at 11:13 am #12633745

Andreas

Thank you very much for your reply and appreciate that you shared your solution.

If you need anything else do not hesitate to contact us.

Closing this ticket now.

Regards,
Andreas