我今天安装了WooCommerce Brands插件,并注意到一件事,我想对其进行一些修改以使其以我喜欢的方式工作。在左侧栏中按某些品牌过滤器时,将显示该类别的产品品牌。但是,如果从“ Left Brands”窗口小部件上按其他品牌,则会在第二个选择上添加第二个选择,并显示如下所示的URL:
https://www.example.com/product-category/mens/?filtering=1&filter_product_brand=231,229
所以 231 是我点击的第一个品牌,而 229 是第二个点击的品牌,但是我只希望最后一次点击处于活动状态,而不是两个都激活!我检查了他们的documentation,但他们没有提供有关此更改的任何信息。我找到了进行此更改的代码(进入文件 class-wc-widget-brand-nav.php ),似乎是JQ代码:
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( '&', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy ) ) ) ) ) ) . "&filter_". esc_js( $taxonomy ) . "=' + jQuery( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' ).val();
});
" );
}
return $found;
}
protected function layered_nav_list( $terms, $taxonomy ) {
// List display
echo '<ul class="wc-brand-list-layered-nav-' . esc_attr( $taxonomy ) . '">';
$term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy );
$_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 );
$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( $_GET[ $filter_name ] ) ) : array();
$current_filter = array_map( 'intval', $current_filter );
if ( ! in_array( $term->term_id, $current_filter ) ) {
$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>';
echo esc_html( $term->name );
echo ( $count > 0 || $option_is_set ) ? '</a> ' : '</span> ';
echo apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term );
echo '</li>';
}
echo '</ul>';
return $found;
}
但我认为必须在此处进行更改:
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( '&', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy ) ) ) ) ) ) . "&filter_". esc_js( $taxonomy ) . "=' + jQuery( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' ).val();
});
" );
}
return $found;
}
// All current filters
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
foreach ( $_chosen_attributes as $name => $data ) {
if ( $name === $taxonomy ) {
continue;
}
$filter_name = sanitize_title( str_replace( 'pa_', '', $name ) );
if ( ! empty( $data['terms'] ) ) {
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
}
if ( 'or' == $data['query_type'] ) {
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
}
}
}
return $link;
}
这是实时example,存在问题。所以我的问题是,如何仅保留一个品牌选择器?