We have just released WPML 4.3.3. Before reporting issues please make sure to update - this version resolves all known issues with the major WPML 4.3 release

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

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 kristinaF-2 1 month ago.

Assigned support staff: George Botsev.

Author Posts
October 7, 2019 at 8:22 am #4705305

kristinaF-2

Tell us what you are trying to do?

I am trying to programatically creating a WooCommerce product + create a translation of this product.
I can do so with a normal post, but if I do it with a WooCommerce product with an SKU, it says: "Invalid or duplicate SKU".

Is there any documentation that you are following?
https://wpml.org/wpml-hook/wpml_set_element_language_details/

Is there a similar example that we can see?
$default_language = 'en';
$product = new WC_Product();
$product->set_name('A product in orignal language');
$product->set_sku('1234');
$original_post_id = $product->save();

$default_language = 'da';
$product = new WC_Product();
$product->set_name('A product in translated language');
$product->set_sku('1234');
$post_id = $product->save();

$original_post_id = apply_filters( 'wpml_object_id', $original_post_id, 'product', false, $default_language);

$wpml_element_type = apply_filters('wpml_element_type', 'product');
$get_language_args = array('element_id' => $original_post_id, 'element_type' => 'product' );
$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );

$set_language_args = array(
'element_id' => $post_id,
'element_type' => $wpml_element_type,
'trid' => $original_post_language_info->trid,
'language_code' => 'da', // create "Danish" translation
'source_language_code' => $original_post_language_info->language_code
);

do_action( 'wpml_set_element_language_details', $set_language_args );

October 8, 2019 at 11:40 am #4714787

George Botsev
Supporter

Languages: English (English )

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

Hello and thank you for contacting us.

The SKU usually has to be unique, and that is why we have custom logic that syncs that for the translations.
When creating programmatically a product, perhaps it is best if you leave the SKU empty and if I recall correctly, the SKU should sync.

Another way probably is to use our REST api as noted here: https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/

And another way is probably to duplicate to the translation using our hooks as noted here: https://wpml.org/wpml-hook/wpml_admin_make_post_duplicates/

October 8, 2019 at 11:42 am #4714791

kristinaF-2

My issue is resolved now. Thank you!

I ended up adding

add_filter( 'wc_product_has_unique_sku', '__return_false' ); 

Before importing, and that fixed the issue