Skip to content

Commit

Permalink
Refactors/improvements to layered nav to support slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Feb 9, 2016
1 parent 1ca9d8f commit cae1fe6
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 215 deletions.
32 changes: 14 additions & 18 deletions includes/class-wc-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,36 +693,32 @@ public function stock_status_meta_query( $status = 'instock' ) {
* Layered Nav Init.
*/
public function layered_nav_init( ) {

if ( apply_filters( 'woocommerce_is_layered_nav_active', is_active_widget( false, false, 'woocommerce_layered_nav', true ) ) && ! is_admin() ) {

global $_chosen_attributes;

$_chosen_attributes = array();

$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $attribute_taxonomies ) {
if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
foreach ( $attribute_taxonomies as $tax ) {
$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
$taxonomy = wc_attribute_taxonomy_name( $attribute );
$name = 'filter_' . $attribute;
$filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( $_GET[ 'filter_' . $attribute ] ) ) : array();
$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ) ) ? wc_clean( $_GET[ 'query_type_' . $attribute ] ) : '';

if ( ! $query_type ) {
$query_type = apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
}

$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
$taxonomy = wc_attribute_taxonomy_name( $attribute );
$name = 'filter_' . $attribute;
$query_type_name = 'query_type_' . $attribute;

if ( ! empty( $_GET[ $name ] ) && taxonomy_exists( $taxonomy ) ) {

$_chosen_attributes[ $taxonomy ]['terms'] = explode( ',', $_GET[ $name ] );

if ( empty( $_GET[ $query_type_name ] ) || ! in_array( strtolower( $_GET[ $query_type_name ] ), array( 'and', 'or' ) ) )
$_chosen_attributes[ $taxonomy ]['query_type'] = apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
else
$_chosen_attributes[ $taxonomy ]['query_type'] = strtolower( $_GET[ $query_type_name ] );

if ( ! empty( $filter_terms ) && taxonomy_exists( $taxonomy ) ) {
$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding
$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type;
}
}
}

add_filter('loop_shop_post_in', array( $this, 'layered_nav_query' ) );
add_filter( 'loop_shop_post_in', array( $this, 'layered_nav_query' ) );
}
}

Expand Down
19 changes: 8 additions & 11 deletions includes/widgets/class-wc-widget-layered-nav-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,23 @@ public function widget( $args, $instance ) {
// Attributes
if ( ! is_null( $_chosen_attributes ) ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {

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

if ( ! isset( $term->name ) ) {
if ( ! $term = get_term_by( 'slug', $term_slug, $taxonomy ) ) {
continue;
}

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

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

if ( sizeof( $new_filter ) > 0 ) {
$link = add_query_arg( 'filter_' . $taxonomy_filter, implode( ',', $new_filter ), $link );
$link = add_query_arg( $filter_name, implode( ',', $new_filter ), $link );
}

echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . $term->name . '</a></li>';
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>';
}
}
}
Expand Down
Loading

0 comments on commit cae1fe6

Please sign in to comment.