2
votes

My WooCommerce shop has two main categories:

cat1
- subcat1
- subcat2

and

cat2
- subcatA
- subcatB

I've made menus of these two branches and they are displayed on category pages. For easier navigation, I also want to show them on single product pages.

My code (located in woocommerce.php) is:

<?php 
if ( is_tax( 'product_cat', array(14,18,19,20,21,22,23,24)) OR is_single() ) {
   wp_nav_menu( array( 'theme_location' => 'cat1' ) ); 
   }
   elseif ( is_tax( 'product_cat', array(15,16,17,20)) OR is_single() ) {
   wp_nav_menu( array( 'theme_location' => 'cat2' ) ); 
   }
?>

This works for the category pages but not for the single product pages.

How can I assign all the single products of cat1 and cat2 and display the assigned menu?

Thanks

1

1 Answers

3
votes

The conditional tag is_tax() checks if a custom taxonomy archive page is being displayed. So as you see is not appropriated for single product pages.

You should use has_term() conditional function, that will work in a similar way for your single product pages.

So your code is going to be:

<?php

$cats1 = array(14,18,19,20,21,22,23,24);
$cats2 = array(15,16,17,20);
$tax = 'product_cat';

if ( is_tax( $tax, $cats1 ) || has_term( $cats1, $tax ) )
   wp_nav_menu( array( 'theme_location' => 'cat1' ) ); 
elseif ( is_tax( $tax, $cats2 ) || has_term( $cats2, $tax ) )
   wp_nav_menu( array( 'theme_location' => 'cat2' ) ); 

?>

This should work as you expect for single product pages and archives pages .