Site logo

Archived topic

Add a category menu to product pages

30 replies · Started by Emma on April 25, 2020

Viewing posts 16–30 of 31

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

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>';
}
?>

That works brilliantly, thank you!

Glad to be of help

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

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>';
}
?>

Works perfectly, Thank you once more David!

Awesome - glad to hear

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.

HI there,

is the Shop archive the front page of the site ?

No, it isn't.

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

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 ;)

Hi Rafal,

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

Thanks :)

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!

This archived topic is closed to new replies.