[Resolved] Add a category menu to product pages

Home Forums Support [Resolved] Add a category menu to product pages

Home Forums Support Add a category menu to product pages

  • This topic has 30 replies, 6 voices, and was last updated 9 months ago by Leo.
Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #1391875
    Simon

    Thank you David,

    I made the change described to Woocommerce Shop Category Menu element successfully, and the code you kindly added creates the similar child category list perfectly. I now have two lists and wonder if it’s possible to display just the child categories when they exist and the main when they dont?

    What do you think?

    Thank you very much btw

    #1392143
    David
    Staff
    Customer Support

    Try this:

    <?php
    // Setup get_X_terms args
    $cat_args = array(
      'orderby'    => 'name',
      'order'      => 'asc',
      'hide_empty' => true,
    );
    
    $navClass = 'child';
    // query child category
    $current_term = get_queried_object();
    $parent = $current_term->term_id;
    $product_categories = get_term_children( $parent, 'product_cat', $cat_args ); 
    
    // if no child get parent terms
    if ( empty($product_categories) ) {
      $navClass = 'parent';
      $cat_args['parent'] = 0;
      $product_categories = get_terms( 'product_cat', $cat_args );
    }
     
    // If either exists output terms
    if ( ! empty( $product_categories ) ) {
      echo '<ul class="woo-cat-nav ' . $navClass  . '">';
      foreach ( $product_categories as $category ) {
        $term = get_term( $category, 'product_cat' );
        printf( 
          '<li><a href="%1$s">%2$s</a></li>',
          get_term_link( $term ),
          $term->name
        );
      }
      echo '</ul>';
    }
    ?>
    #1394917
    Simon

    That works brilliantly, thank you!

    #1395076
    David
    Staff
    Customer Support
    #1402255
    Simon

    Hi David,

    Is there a way to limit this to only show the next level and not all hereditary levels? i.e. the parent shows the children of that category, but not the grandchildren (or great grandchildren)?

    For example:
    Shop -> shows Parent(s)
    inside Parent cat(1) -> displays Children (cat1) only
    inside Children -> displays Grandchildren only
    inside Grandchildren -> Great Grandchildren only

    inside Parent cat(2) -> displays Children (cat2) only

    I hope this makes sense! I can try and re-define if needed.

    All the best

    Simon

    #1403004
    David
    Staff
    Customer Support

    Give this a try instead:

    <?php
    // Setup get_terms product category
    
    $taxonomy = 'product_cat';
    $current_term = get_queried_object();
    $parent = $current_term->term_id;
    
    $cat_args = array(
        'parent' => $parent,
        'orderby'    => 'name',
        'order'      => 'asc',
        'hide_empty' => true,
        'depth'  => 1
    );
    
    // If shop set top level only
    if ( is_shop() ) {
    	$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>';
    }
    ?>
    #1403019
    Simon

    Works perfectly, Thank you once more David!

    #1403052
    David
    Staff
    Customer Support

    Awesome – glad to hear

    #1681555
    Rafał

    David, this is great!
    How could you polish the snippet for not throwing a PHP notice:
    Notice: Undefined property: WP_Post_Type::$term_id in …\wp-content\plugins\gp-premium\elements\class-hooks.php(196) : eval()'d code on line 6
    The notice falls out only at root, means Shop main page, but categories, and subcategories stay clean.

    #1681984
    David
    Staff
    Customer Support

    HI there,

    is the Shop archive the front page of the site ?

    #1682390
    Rafał

    No, it isn’t.

    #1682985
    David
    Staff
    Customer Support

    Can you raise a new topic and share a link to the site where i can see the error?

    #1683457
    Rafał

    David, thank you for your efforts, but don’t make me raise a new topic, please…
    You can see the notice here: http://prot3.rafiozoo.ovh/sklep/

    To be honest I don’t realize what weight is the issue.
    I’m willing to set in wp-config.php
    define('WP_DEBUG', false);
    (possibly) Let’s make life easier 😉

    #1683590
    Leo
    Staff
    Customer Support

    Hi Rafal,

    Please open a new topic as David suggested since there are multiple authors in this thread already?

    Thanks 🙂

    #1684272
    Rafał

    Hi there!
    If we set the condition earlier, then there’s no need to fetch term_id, which does not exist at Shop page, does it?

    <?php
    // Setup get_terms product category
    
    $taxonomy = 'product_cat';
    $current_term = get_queried_object();
    
    if ( is_shop() ) {
      $parent = 0;
    } else {
      $parent = $current_term->term_id;
    }
    
    $cat_args = array(
        'parent' => $parent,
        'orderby'    => 'name',
        'order'      => 'asc',
        'hide_empty' => true,
        'depth'  => 1
    );
    
    $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>';
    }
    ?>

    Anyway, works with no notice, and you are the best support on the web! Thank you!

Viewing 15 posts - 16 through 30 (of 31 total)
  • The topic ‘Add a category menu to product pages’ is closed to new replies.