I have developed an importer of woocommerce products and I need to translate the products and of course the related taxonomy.
For the translations I decided to use wpml and unfortunately I can't assign the German language to the translated taxonomy.
Unfortunately I can't even find help in solving my problem in the WPML support forum and the code available online doesn't work for me.
global $sitepress;
$args_it = array( 'description'=> $term_name_it, 'slug' => $term_slug, 'parent' => 0 );
$args_de = array( 'description'=> $term_name_de, 'slug' => $term_slug. "-de", 'parent' => 0 );
$term_it = wp_insert_term( $term_name_it, 'product_cat', $args_it );
$term_de = wp_insert_term( $term_name_de, 'product_cat', $args_de );
// get the trid from the original
$trid = $sitepress->get_element_trid($term_it['term_id'], 'tax_product_cat');
// associate the translated term to the original
$sitepress->set_element_language_details($term_de['term_id'], 'tax_product_cat', $trid, 'de', $sitepress->get_default_language());
At the moment, using this code, I don't have any errors, but it shows me all the terms as if they were all in Italian and not translations.
I realized that $trid was NULL and that the "wp_icl_translations" table in element_id saved the value "term_taxonomy_id" instead of "term_id".
Below the code that solved my problem. Have fun.
global $sitepress;
$args_it = array( 'description'=> $term_name_it, 'slug' => $term_slug, 'parent' => 0 );
$args_de = array( 'description'=> $term_name_de, 'slug' => $term_slug. "-de", 'parent' => 0 );
$term_it = wp_insert_term( $term_name_it, 'product_cat', $args_it );
$term_de = wp_insert_term( $term_name_de, 'product_cat', $args_de );
// get the trid from the original
$trid = $sitepress->get_element_trid($term_it['term_taxonomy_id'], 'tax_product_cat');
if (! empty($trid)) {
// associate the translated term to the original
$sitepress->set_element_language_details($term_de['term_taxonomy_id'], 'tax_product_cat', $trid, 'de', $sitepress->get_default_language());
$sitepress->set_element_language_details($term_it['term_taxonomy_id'], 'tax_product_cat', $trid, 'it', $sitepress->get_default_language());
} else {
// $trid is NULL
}