Skip to content

Filter the WooCommerce Product Categories Widget

First things first: I’d normally tweak the WooCommerce Product Categories widget—e.g., exclude certain categories—using the woocommerce_product_categories_widget_args filter hook.

If I want to add something to that list, however, like a link to the top-level shop page, I’ll have to find another way.

Luckily, the Product Categories widget extends WordPress’s general Categories widget. That means I can hook into the wp_list_categories filter and prepend a link of my own to the entire product categories list.

Voilà:

add_filter( 'wp_list_categories', function( $output, $args ) {
  // If the current widget is the product categories widget.
  if ( ! empty( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) {
    // Add the extra link to the general shop page.
    $output = '<li class="cat-item"><a href="' . esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ) . '">' . __( 'All categories', 'my-textdomain' ) . "</a></li>\n" . $output;
  }
  return $output;
}, 10, 2 );