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

Last updated by Laura 1 year ago.

Assigned support staff: Laura.

Author Posts
October 10, 2018 at 5:08 pm #2805280

margusP

I am trying to Create a plugin that imports translated products and adds translated plugins to these products. Creating translated products works well but creating translated categories fails.

What code can I use to create category in one language with name "name1" and its translation in other language with name "name2"

This code does not work properly.

        $category = wp_insert_term($catName, 'product_cat', [
            'description' => $catName,
            'slug'        => $slug
        ]);

        if (is_wp_error($category)) {
            error_log('Failed inserting category: ' . print_r($category, true));
        }

        $langinfArgs = [
            'element_id'   => $category['term_taxonomy_id'],
            'element_type' => 'product_cat'
        ];
        $langInf     = apply_filters('wpml_element_language_details', null, $langinfArgs);

        $trid = $langInf->trid;
        $args = [
            'element_id'           => $category['term_taxonomy_id'],
            'element_type'         => 'product_cat',
            'trid'                 => $trid,
            'language_code'        => $lang,
            'source_language_code' => apply_filters('wpml_default_language', null)
        ];
        do_action('wpml_set_element_language_details', $args);

Code for creating translated products which works well.

            $postId = wp_insert_post([
                'post_title'   => strtoupper($product->ItemCode),
                'post_content' => $product->Description,
                'post_status'  => 'publish',
                'post_type'    => "product",
            ]);

            $translationId = wp_insert_post([
                'post_title'   => strtoupper($product->ItemCode),
                'post_content' => $product->Description2,
                'post_status'  => 'publish',
                'post_type'    => "product",
            ]);

            $trid = $sitepress->get_element_trid($postId, 'post_product');
            // Tell WPML the second post is a translation of the first
            $sitepress->set_element_language_details($postId, 'post_product', $trid, $primaryLanguage);
            $sitepress->set_element_language_details($translationId, 'post_product', $trid, $secondaryLanguage);
October 11, 2018 at 3:38 pm #2809025

Laura
Supporter

Languages: English (English ) Italian (Italiano ) Portuguese (Brazil) (Português )

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

Hi,

since your code is quite complex, I escalated the ticket to our 2nd tier so they can give us opinions.

I'll update you as soon as possible.

October 12, 2018 at 12:40 pm #2812020

Laura
Supporter

Languages: English (English ) Italian (Italiano ) Portuguese (Brazil) (Português )

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

Hi,

since the plugin is not public, we have to consider it as a custom code, therefore this goes beyond our support policy: https://wpml.org/purchase/support-policy/

Did you search our hooks reference? https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
In particular, this set of hooks: https://wpml.org/wpml-hook/wpml_set_element_language_details/

Also, I don't know exactly what you want to achieve, but have you considered using WPML All Import? It does import products and categories and it's fully compatible with WPML.