Woocommerce not displaying products in different langauges when the query is modified

[ad_1]

I’ve been dealing with this issue for so long already, as we know WPML support is useless with custom modifications so i’m hoping somebody could help me out with this.

​

Basically what i need is to display woocommerce products on a custom page with working categories and filters. I’ve managed to get categories working on the main language but whenever a category is selected on the second language, the query returins no posts.

​

Basically what i did is i enabled poduct\_cat query variable on a custom shop page by adding the rewrites.

​

// Adding custom query vars for category filtering on outlet products

add_action( ‘init’, ‘add_custom_rewrite’ );

function add_custom_rewrite($query){

$pages = [
[
‘page’ => ‘sooduspakkumised’,
‘slug’ => ‘kategooria’,
],
[
‘page’ => ‘special-offers’,
‘slug’ => ‘category’
] ];

foreach($pages as $page){
add_rewrite_rule(‘^’.$page[‘page’].’/’.$page[‘slug’].’/(.+?)/page/?([0-9]{1,})/?$’,’index.php?pagename=’.$page[‘page’].’&product_cat=$matches[1]&paged=$matches[2]’,’top’);
add_rewrite_rule(‘^’.$page[‘page’].’/’.$page[‘slug’].’/(.+?)/?$’,’index.php?pagename=’.$page[‘page’].’&product_cat=$matches[1]’,’top’);

}
flush_rewrite_rules(true);

}

I also tried with a custom url parameter by modifying the main wp query

​

add_action( ‘init’, ‘add_custom_rewrite’ );

function add_custom_rewrite($query){

$pages = [
[
‘page’ => ‘sooduspakkumised’,
‘slug’ => ‘kategooria’,
],
[
‘page’ => ‘special-offers’,
‘slug’ => ‘category’
] ];

foreach($pages as $page){
add_rewrite_tag(‘%product_cats%’, ‘([^&]+)’);
add_rewrite_rule(‘^’.$page[‘page’].’/’.$page[‘slug’].’/(.+?)/page/?([0-9]{1,})/?$’,’index.php?pagename=’.$page[‘page’].’&product_cats=$matches[1]&paged=$matches[2]’,’top’);
add_rewrite_rule(‘^’.$page[‘page’].’/’.$page[‘slug’].’/(.+?)/?$’,’index.php?pagename=’.$page[‘page’].’&product_cats=$matches[1]’,’top’);

}
flush_rewrite_rules(true);

}

// Enable category filtering on discounted products

add_action(‘pre_get_posts’, function($query){

if($query->get(‘meta_query’)[0][‘key’] == ‘onsale’ ){

get_query_var(‘product_cats’);

$query->set(‘tax_query’, array(
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => [get_query_var(‘product_cats’)],
‘operator’ => ‘IN’
)
));
//$query->set(‘suppress_filters’, true);

}

});

​

I can’t seem to figure out what else needs to be added to make it work with WPML. I also tried using untranslated slugs, original category ids etc.

​

I’ve tried pretty much everything i’ve found on the internet. I would expect this taxonomy query to work no matter which languge is selected.

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer