phpwordpresswoocommercemultilingualwpml

(Updated) Get WooCommerce category on second language using WPML


I'm using WPML on my WordPress site with Spanish and English languages.

I need to retrieve WooCommerce products from a translated (English) category. This is my working code for the main language (Spanish):

// get all products from the category talleres
$productos = wc_get_products([
    'status' => 'publish', 
    'limit' => -1, 
    'product_cat' => 'talleres'
]);

// then get some information about each product in that category
$locaciones = array_map(function ($product) {
    /** @var WC_Product $product */
    return [
        'lat' => get_post_meta( $product->get_id(), 'lat_mapa_tl', true),
        'long' => get_post_meta( $product->get_id(), 'long_mapa_tl', true),
        'icono' => get_post_meta( $product->get_id(), 'icono_mapa_tl', true ),
        'popup_html' => '<div class="titulo">' . $product->get_title() . '</div><br /><a href="' . get_permalink( $product->get_id() ) . '" class="sinborde"><img src="' . get_the_post_thumbnail_url( $product->get_id(), 'full' ) . '" width="250" height="141" title="Hacé click para ver el time-lapse" /></a><br /><div class="coleccion">Categorizado en ' . $product->get_categories() . '</div><a href="' . get_permalink( $product->get_id() ) . '">Ver time-lapse</a>'
    ];
}, $productos);

This is working perfectly. The problem comes when trying to use the translated category of talleres, which is "workshops". Using that category, this would be my code:

$productos = wc_get_products([
    'status' => 'publish', 
    'limit' => -1, 
    'product_cat' => 'workshops'
]);

But that gets the information from the original untranslated category in Spanish, not in English (my second language).

Edit:

Someone suggested I should add this line inside wc_get_products:

'suppress_filters' => true

But that didn't work.


Solution

  • I found a solution to my issue here. Just added these two lines of code and everything is working as expected:

    global $sitepress;
    
    // switch to the desired language, French for example
    $sitepress->switch_lang( 'fr' );
    

    Edit: here's another possible fix in case anybody else need it: https://wpml.org/forums/topic/problems-to-show-in-all-languages-suppress_filters-true-not-working/#post-872537

    Didn't test it.