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!

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.