最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce product query not working in other WPML languages

programmeradmin2浏览0评论

I am trying to hide all Woocommerce products from a specific product category on the main shop archive page (products only visible when a customer goes to the specific category archive).

The following code is working to hide those products in my original language (Dutch), but not in the other 3 site languages, even though I also added their correct category id's.

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
function prefix_custom_pre_get_posts_query( $q ) {
  
  if( is_shop() || is_page('shop') ) { // set conditions here
      $tax_query = (array) $q->get( 'tax_query' );
  
      $tax_query[] = array(
             'taxonomy' => 'product_cat',
             'field'    => 'term_id',
             'terms'    => array(27,952,951,1119), // set product categories here
             'operator' => 'NOT IN',
      );
  
      $q->set( 'tax_query', $tax_query );
  }
}

Anyone knows how I could adapt this code snippet so it works in all languages? Thanks!

I am trying to hide all Woocommerce products from a specific product category on the main shop archive page (products only visible when a customer goes to the specific category archive).

The following code is working to hide those products in my original language (Dutch), but not in the other 3 site languages, even though I also added their correct category id's.

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
function prefix_custom_pre_get_posts_query( $q ) {
  
  if( is_shop() || is_page('shop') ) { // set conditions here
      $tax_query = (array) $q->get( 'tax_query' );
  
      $tax_query[] = array(
             'taxonomy' => 'product_cat',
             'field'    => 'term_id',
             'terms'    => array(27,952,951,1119), // set product categories here
             'operator' => 'NOT IN',
      );
  
      $q->set( 'tax_query', $tax_query );
  }
}

Anyone knows how I could adapt this code snippet so it works in all languages? Thanks!

Share Improve this question asked Aug 17, 2020 at 6:27 Senne VandenputteSenne Vandenputte 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I would do two things

  • Check if you get inside the IF-statement in the other languages. is_page('shop') refers to a specific post/page ID. You can bypass this problem by fetching the current's language translation post-ID with is_page( apply_filters( 'wpml_object_id', {{orig_page_id}}, 'page', true ) )
  • I would reverse the query, instead of excluding the unneeded product categories. You can use 'operator' => 'IN' to just fetch the terms that are in that specific category.

Hope this helps

发布评论

评论列表(0)

  1. 暂无评论