Skip to content

Commit

Permalink
Merge pull request woocommerce#9665 from seriusokhatsky/master
Browse files Browse the repository at this point in the history
Slugs instead of IDs for layered nav by terms
  • Loading branch information
mikejolley committed Feb 9, 2016
2 parents 1a976fb + ef86713 commit 1ca9d8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion includes/class-wc-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public function layered_nav_query( $filtered_posts ) {
array(
'taxonomy' => $attribute,
'terms' => $value,
'field' => 'term_id'
'field' => 'slug'
)
)
);
Expand Down
8 changes: 4 additions & 4 deletions includes/widgets/class-wc-widget-layered-nav-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ public function widget( $args, $instance ) {
if ( ! is_null( $_chosen_attributes ) ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {

foreach ( $data['terms'] as $term_id ) {
$term = get_term( $term_id, $taxonomy );
foreach ( $data['terms'] as $term_slug ) {
$term = get_term_by( 'slug', $term_slug, $taxonomy );

if ( ! isset( $term->name ) ) {
continue;
}

$taxonomy_filter = str_replace( 'pa_', '', $taxonomy );
$current_filter = ! empty( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '';
$new_filter = array_map( 'absint', explode( ',', $current_filter ) );
$new_filter = array_diff( $new_filter, array( $term_id ) );
$new_filter = array_map( 'sanitize_text_field', explode( ',', $current_filter ) );
$new_filter = array_diff( $new_filter, array( $term_slug ) );

$link = remove_query_arg( array( 'add-to-cart', 'filter_' . $taxonomy_filter ) );

Expand Down
10 changes: 5 additions & 5 deletions includes/widgets/class-wc-widget-layered-nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function widget( $args, $instance ) {

}

echo '<option value="' . esc_attr( $term->term_id ) . '" ' . selected( isset( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '' , $term->term_id, false ) . '>' . esc_html( $term->name ) . '</option>';
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( isset( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '' , $term->slug, false ) . '>' . esc_html( $term->name ) . '</option>';
}

echo '</select>';
Expand Down Expand Up @@ -278,8 +278,8 @@ public function widget( $args, $instance ) {

$current_filter = array_map( 'esc_attr', $current_filter );

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

// Base Link decided by current page
Expand Down Expand Up @@ -331,13 +331,13 @@ public function widget( $args, $instance ) {
}

// Current Filter = this widget
if ( isset( $_chosen_attributes[ $taxonomy ] ) && is_array( $_chosen_attributes[ $taxonomy ]['terms'] ) && in_array( $term->term_id, $_chosen_attributes[ $taxonomy ]['terms'] ) ) {
if ( isset( $_chosen_attributes[ $taxonomy ] ) && is_array( $_chosen_attributes[ $taxonomy ]['terms'] ) && in_array( $term->slug, $_chosen_attributes[ $taxonomy ]['terms'] ) ) {

$class = 'class="chosen"';

// Remove this term is $current_filter has more than 1 term filtered
if ( sizeof( $current_filter ) > 1 ) {
$current_filter_without_this = array_diff( $current_filter, array( $term->term_id ) );
$current_filter_without_this = array_diff( $current_filter, array( $term->slug ) );
$link = add_query_arg( $arg, implode( ',', $current_filter_without_this ), $link );
}

Expand Down

0 comments on commit 1ca9d8f

Please sign in to comment.