WC_Widget_Brand_Nav::layered_nav_dropdown()protectedWC 1.0

Show dropdown layered nav.

Method of the class: WC_Widget_Brand_Nav{}

No Hooks.

Return

true|false. Will nav display?

Usage

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

WC_Widget_Brand_Nav::layered_nav_dropdown() code WC 9.5.1

protected function layered_nav_dropdown( $terms, $taxonomy, $depth = 0 ) {
	$found = false;

	if ( $taxonomy !== $this->get_current_taxonomy() ) {
		$term_counts        = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, 'or' );
		$_chosen_attributes = $this->get_chosen_attributes();

		if ( 0 === $depth ) {
			echo '<select class="wc-brand-dropdown-layered-nav-' . esc_attr( $taxonomy ) . '">';
			echo '<option value="">' . esc_html__( 'Any Brand', 'woocommerce' ) . '</option>';
		}

		foreach ( $terms as $term ) {
			// If on a term page, skip that term in widget list.
			if ( $term->term_id === $this->get_current_term_id() ) {
				continue;
			}

			// Get count based on current view.
			$current_values = ! empty( $_chosen_attributes ) ? $_chosen_attributes : array();
			$option_is_set  = in_array( $term->term_id, $current_values, true );
			$count          = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;

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

			echo '<option value="' . esc_attr( $term->term_id ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( str_repeat( '&nbsp;', 2 * $depth ) . $term->name ) . '</option>';

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

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

		if ( 0 === $depth ) {
			$link = $this->get_page_base_url( $taxonomy );
			echo '</select>';

			wc_enqueue_js(
				"
				jQuery( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' ).change( function() {
					var slug = jQuery( this ).val();
					location.href = '" . preg_replace( '%\/page\/[0-9]+%', '', str_replace( array( '&amp;', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', $link ) ) ) ) . '&filter_' . esc_js( $taxonomy ) . "=' + jQuery( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' ).val();
				});
			"
			);
		}
	}

	return $found;
}