[Resolved] Filter different categories on different pages

Home Forums Support [Resolved] Filter different categories on different pages

Home Forums Support Filter different categories on different pages

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1437346
    Fredrik

    Hello,

    I’m adding this element hook (see below) to woocommerce_archive_description (borrowed from site library Niche) to toggle between product categories. Works as expected πŸ™‚

    But could it be possible to somehow only use the toggle for a specific parent category?

    Right now I have two parent categories as menu items in the navigation. So ideally, I could navigate to parent category #1 and toggle only the categories in that parent category. Navigate to parent category #2 and only toggle between the categories in that parent category. Hope it makes sense. Thx!

    <?php
    $cat_args = array(
    	'orderby'    => 'name',
    	'order'      => 'asc',
    	'hide_empty' => true,
    );
     
    $product_categories = get_terms( 'product_cat', $cat_args );
     
    if ( ! empty( $product_categories ) ) {
    	echo '<ul class="woo-cat-nav">';
    		foreach ( $product_categories as $key => $category ) {
    			printf( 
    				'<li>
    					<a href="%1$s">
    						%2$s
    					</a>
    				</li>',
    				get_term_link( $category ),
    				$category->name
    			);
    		}
    	echo '</ul>';
    }
    ?>
    #1437762
    David
    Staff
    Customer Support

    Hi there,

    this user had a similar request:

    https://generatepress.com/forums/topic/add-a-category-menu-to-product-pages/page/2/#post-1402255

    See his questions and see my reply πŸ™‚

    #1438446
    Fredrik

    Hello David,

    Ah! Thank you πŸ™‚ This is very close to what I was looking for.

    One thing that would make it better is that the category menu stays and not disappears when you click on a child category within the parent, to remove the need for a back button.

    Let me know πŸ™‚

    #1438699
    David
    Staff
    Customer Support

    Try this:

    <?php
    // Setup get_terms product category
    
    $taxonomy = 'product_cat';
    $current_term = get_queried_object();
    $parent = $current_term->term_id;
    $children = get_term_children($parent, $taxonomy);
    
    $cat_args = array(
        'parent' => $parent,
        'orderby'    => 'name',
        'order'      => 'asc',
        'hide_empty' => true,
        'depth'  => 1
    );
    
    // If shop set top level only
    if ( is_shop() || empty($children) ) {
    	$cat_args['parent'] = 0;
    }
    
    $product_categories = get_terms( $taxonomy , $cat_args );
    
    // If either exists output terms
    if ( ! empty( $product_categories ) ) {
      echo '<ul class="woo-cat-nav">';
      foreach ( $product_categories as $category ) {
        $term = get_term( $category, $taxonomy );
        printf( 
          '<li><a href="%1$s">%2$s</a></li>',
          get_term_link( $term ),
          $term->name
        );
      }
      echo '</ul>';
    }
    ?>
    #1438753
    Fredrik

    Hello David,

    Great πŸ™‚ now the category menu follows.

    But clicking a child menu item now will get you back to showing the top-level parent, instead of back to the children within the parent.

    I think this prototype that I just did explains it better:

    PROTOTYPE

    #1439640
    David
    Staff
    Customer Support

    I know what your trying to achieve.
    Unfortunately that particular piece of code just cannot do that – it would take Custom Development i am afraid.

    #1439968
    Fredrik

    I figured so πŸ™‚ no worries! I will try to figure it out somehow, also, is there any other way to go about this? Maybe it’s simply the wrong approach.

    Thank you for all your help πŸ™‚

    #1440370
    David
    Staff
    Customer Support

    Its one of those WP challenges thats probably best asked on Stack Overflow if a paid developer is not an option.

    It effectively requires two Loops of the get_terms – one for the parent level, and one for the child level. Which entails two separate queries. So we could do something like this:

    <?php
    // Setup get_terms product category
    
    $taxonomy = 'product_cat';
    $current_term = get_queried_object();
    $parent = $current_term->term_id;
    
    $cat_args = array( 
        'orderby'    => 'name',
        'order'      => 'asc',
        'hide_empty' => true,
        'depth'  => 1
    );
    
    // Build top level navigation
    $product_categories = get_terms( $taxonomy , array($cat_args, 'parent' => 0) );
    build_cat_nav($product_categories);
    
    // If not top level ie. shop then build child terms navigation
    if( ! is_shop() ) {
    	$product_categories = get_terms( $taxonomy , array($cat_args, 'parent' => $parent) );
    	build_cat_nav($product_categories);
    } 
    
    // If either exists output terms
    function build_cat_nav($product_categories) {
    if ( ! empty( $product_categories ) ) {
      echo '<ul class="woo-cat-nav">';
      foreach ( $product_categories as $category ) {
        $term = get_term( $category, $taxonomy );
        printf( 
          '<li><a href="%1$s">%2$s</a></li>',
          get_term_link( $term ),
          $term->name
        );
      }
      echo '</ul>';
    }
    }
    ?>

    This will always display the top level terms.
    If not on the shop page ie. where on a category, it will display any child terms.

    The issue comes when you then navigate to the childless category – getting it to display ITS parent categories is where it becomes a problem.

    #1443922
    Fredrik

    Hello David,

    I’ll try asking over at Stack Overflow & check with a dev. Either way, if I come up with a solution I’ll post it here πŸ™‚

    Thank you for your guidance & help!

    #1444014
    David
    Staff
    Customer Support

    Awesome – let us know πŸ™‚

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.