Evakos
Evakos

Reputation: 312

Using Polylang to return terms in currently selected language

I'm using this basic loop to retrieve the category name:

$terms = get_the_terms( $product_id, 'product_cat' );
//var_dump ($terms);
if($items){
    foreach ( $terms as $term ) {
        //$cat_id = $term->id;
        $cat_name = $term->name; //returns category name, but only in original language booking was made in.
        }
    }
}

I'm returning these results in a custom WooCommerce dashboard.php template, in order to display the client order history. The code works but it returns the category name ($cat_name) but not in the currently selected language, only in the language the booking was originally made in. The function I have tried to use is pll_get_term() but I am not sure of the best way of implementing it.

Thanks for looking.

Upvotes: 4

Views: 3868

Answers (1)

Evakos
Evakos

Reputation: 312

With some help from Polylang premium support and correcting the way I was retrieving the id which i had written incorrectly it should be this:

$terms = get_the_terms( $product_id, 'product_cat' ); 


if($items){
    foreach ( $terms as $term ) {
    $cat_id = pll_get_term( $term->term_id, $current_lang );
    $cat_name = get_term( $cat_id )->name;
        }
    }
}

I hadn't shown the attempt to use the pll_get_term function in the original post, but this function needs an id as a parameter which should be term_id and not id.

Chris

Upvotes: 4

Related Questions