Why template_include filter does not work with WPML plugin?

I have some issue with WPML. I use two different product page fro the phisical product and for the video lessons. Now I added one more language to the project ussin WPML plugin. And It's okay but my chunk of code I using for changed the template does not work. I use this code for changing template:

add_filter( 'template_include', 'bsl_single_product_template_include', 50, 1 );
function bsl_single_product_template_include( $template ) {
if ( is_singular('product')  (has_term( 'cosmetics', 'product_cat')) ) {
    $template = get_stylesheet_directory() . '/woocommerce/single-product-cosmetics.php';
}
   return $template;
}

How it should work: there is a product page and there is a video tutorial page. These pages have different templates. To change the template I use the code shown above. But after installing and configuring the second language when switching to the template does not occur. For example, I am on a physical product page, I switch the language and the page changes to the default one, although the product page should remain.

Topic plugin-wpml filters Wordpress

Category Web


Hi everyone I have found the solution. I think the comment of the @JacobPeattie help me to do that. Thank you @JacobPeattie! And here is the solution: in my code I use if statement for chacking what product category is:

if ( is_singular('product') && (has_term( 'cosmetics', 'product_cat')) )

But I did not take into account the fact that the WPML changes the category for the extra language. That's my fault. Now I will know that. And here is the correct code:

add_filter( 'template_include', 'alexandra_template_include', 15, 1 );
function alexandra_template_include( $template ) {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

if(is_product() &&  in_array( 'cosmetics', $categories) || in_array( 'cosmetics- 
en', $categories)){
    $template = get_stylesheet_directory() . '/woocommerce/single-product- 
cosmetics.php';
 }
   return $template;
  }

I need it for specific purposes. But maybe it will be useful for someone else.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.