Background of the issue:
I am trying to manage stock on my website using WPML. I expected to see the same stock levels for the same products in all languages. The issue can be seen on this page: hidden link.
Symptoms:
The stock levels are not synced between languages. Specifically, Stock EN is 16, while Stock NL is 0.
Questions:
Why is the product stock not syncing between languages?
How can I ensure that stock levels are the same for all languages?
I forced the stock on the NL product using this snippet:
/*
* Force stock
*/
function force_stock_for_translated_product( $translated_id, $new_stock ) {
// Get the default language (usually 'en' or 'nl')
$default_lang = apply_filters( 'wpml_default_language', null );
// Get the original product ID in default language
$original_id = apply_filters( 'wpml_object_id', $translated_id, 'product', false, $default_lang );
// If original exists, update its stock
if ( $original_id ) {
update_post_meta( $original_id, '_stock', $new_stock );
wc_delete_product_transients( $original_id );
wc_delete_product_transients( $translated_id ); // Just to be sure
} else {
// If no link found, force update on translated product directly
update_post_meta( $translated_id, '_stock', $new_stock );
wc_delete_product_transients( $translated_id );
}
}
force_stock_for_translated_product(38060, 16);
This works; so why is WPML not syncing the stock level?
I changed the setting _stock from `don't translate` to `copy` and now the stock synces between languages when I save the product. What is the correct setting?