WC_Widget_Brand_Nav::layered_nav_list()protectedWC 1.0

Show list based layered nav.

Method of the class: WC_Widget_Brand_Nav{}

Return

true|false. Will nav display?

Usage

// protected - for code of main (parent) or child class
$result = $this->layered_nav_list( $terms, $taxonomy, $depth );
$terms(array) (required)
Terms.
$taxonomy(string) (required)
Taxonomy.
$depth(int)
Depth.

WC_Widget_Brand_Nav::layered_nav_list() code WC 9.5.1

protected function layered_nav_list( $terms, $taxonomy, $depth = 0 ) {
	// List display.
	echo '<ul class="' . ( 0 === $depth ? '' : 'children ' ) . 'wc-brand-list-layered-nav-' . esc_attr( $taxonomy ) . '">';

	$term_counts        = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, 'or' );
	$_chosen_attributes = $this->get_chosen_attributes();
	$current_values     = ! empty( $_chosen_attributes ) ? $_chosen_attributes : array();
	$found              = false;

	$filter_name = 'filter_' . $taxonomy;

	foreach ( $terms as $term ) {
		$option_is_set = in_array( $term->term_id, $current_values, true );
		$count         = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;

		// skip the term for the current archive.
		if ( $this->get_current_term_id() === $term->term_id ) {
			continue;
		}

		// Only show options with count > 0.
		if ( 0 < $count ) {
			$found = true;
		} elseif ( 0 === $count && ! $option_is_set ) {
			continue;
		}

		$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$current_filter = array_map( 'intval', $current_filter );

		if ( ! in_array( $term->term_id, $current_filter, true ) ) {
			$current_filter[] = $term->term_id;
		}

		$link = $this->get_page_base_url( $taxonomy );

		// Add current filters to URL.
		foreach ( $current_filter as $key => $value ) {
			// Exclude query arg for current term archive term.
			if ( $value === $this->get_current_term_id() ) {
				unset( $current_filter[ $key ] );
			}

			// Exclude self so filter can be unset on click.
			if ( $option_is_set && $value === $term->term_id ) {
				unset( $current_filter[ $key ] );
			}
		}

		if ( ! empty( $current_filter ) ) {
			$link = add_query_arg(
				array(
					'filtering'  => '1',
					$filter_name => implode( ',', $current_filter ),
				),
				$link
			);
		}

		echo '<li class="wc-layered-nav-term ' . ( $option_is_set ? 'chosen' : '' ) . '">';

		echo ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">' : '<span>'; // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment

		echo esc_html( $term->name );

		echo ( $count > 0 || $option_is_set ) ? '</a> ' : '</span> ';

		echo wp_kses_post( apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term ) );// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment

		$child_terms = get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => true,
				'parent'     => $term->term_id,
			)
		);

		if ( ! empty( $child_terms ) ) {
			$found |= $this->layered_nav_list( $child_terms, $taxonomy, $depth + 1 );
		}

		echo '</li>';
	}

	echo '</ul>';

	return $found;
}