Set different language for some WooCommerce products
You might need to change the default language for some of your products written in another language. Assuming you have a shop category designated for this different language, you could use the following snippet:
// Add a filter to modify the language attributes
add_filter('language_attributes', function($lang) {
// Check if the current page is a single product page
if(is_product()) {
// Retrieve the product categories
$terms = get_the_terms(get_the_ID(), 'product_cat');
// Loop through each category
foreach($terms as $term) {
// Check if the category slug matches 'czech-products'
if($term->slug == 'czech-products') {
// If a match is found, replace 'en-GB' with 'cs' in the language attributes
$lang = str_replace('en-GB', 'cs', $lang);
// Break the loop as we found the category we were looking for
break;
}
}
}
// Return the modified (or unmodified) language attributes
return $lang;
});