WPML & Woocommerce: How to select product of other languages in PHP?

If you are looking for a way that, by using PHP, you want to find out what product ID of other languages are, here is the code:

    $available_langs = icl_get_languages('skip_missing=0');
foreach($available_langs as $lang){
$lang_post_id = icl_object_id($existing_post_id, 'product', true, $lang['language_code']);
if(empty($lang_post_id) == FALSE){
/** * Do whatever you want here... /**/

Here is how this work:

  1. Check if WPML is installed by checking if icl_get_languages exists. I am getting all langs here (skip_missing=0).
  2. Use icl_get_languages to get ALL languages
  3. Use a loop to check existing language with language code. If that product exists, it will return a product ID, otherwise, it is NULL (Source).
  4. Use conditional statement to continue processing if $lang_post_id is not NULL
Hope it helps someone.


Popular posts from this blog

TCPDF How to show/display Chinese Character?

Using wget bypass htaccess username password 401 authorization

Wordpress Load balancing: 2 web servers 1 MySQL without any Cloud services