WC_Widget_Brand_Nav::layered_nav_dropdown
Show dropdown layered nav.
Method of the class: WC_Widget_Brand_Nav{}
No Hooks.
Returns
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() WC Widget Brand Nav::layered nav dropdown code WC 10.4.3
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( ' ', 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>';
$handle = 'wc-brand-widget-dropdown-layered-nav-' . $taxonomy;
wp_register_script( $handle, '', array(), WC_VERSION, array( 'in_footer' => true ) );
wp_enqueue_script( $handle );
$redirect_url = add_query_arg( 'filtering', '1', preg_replace( '%\/page\/[0-9]+%', '', esc_url_raw( $link ) ) );
wp_add_inline_script(
$handle,
"
(function() {
'use strict';
const dropdown = document.querySelector( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' );
if ( dropdown ) {
dropdown.addEventListener( 'change', function() {
const slug = this.value;
location.href = '" . esc_js( $redirect_url ) . '&filter_' . esc_js( $taxonomy ) . "=' + slug;
} );
}
})();
"
);
}
}
return $found;
}