Permalink
Browse files

added 'only' option to layered nav filter, allows radio button style …

…toggle of a single attribute value within the current category of products, include an 'all' link to reset filter. Only functional in list mode.
  • Loading branch information...
hughc committed Jul 18, 2014
1 parent 7a4c0f9 commit effd6b04846eb72fac262ae755bbcea564351490
Showing with 43 additions and 10 deletions.
  1. +43 −10 includes/widgets/class-wc-widget-layered-nav.php
@@ -90,7 +90,8 @@ public function init_settings() {
'label' => __( 'Query type', 'woocommerce' ),
'options' => array(
'and' => __( 'AND', 'woocommerce' ),
'or' => __( 'OR', 'woocommerce' )
'or' => __( 'OR', 'woocommerce' ),
'only' => __( 'ONLY', 'woocommerce')
)
),
);
@@ -234,6 +235,9 @@ public function widget( $args, $instance ) {
// List display
echo "<ul>";
// $buffer output, as we may want to prepend 'all' item to it
$output_buffer = array();
foreach ( $terms as $term ) {
// Get count based on current view - uses transients
@@ -253,7 +257,7 @@ public function widget( $args, $instance ) {
continue;
// If this is an AND query, only show options with count > 0
if ( $query_type == 'and' ) {
if ( $query_type == 'and') {
$count = sizeof( array_intersect( $_products_in_term, WC()->query->filtered_product_ids ) );
@@ -311,7 +315,7 @@ public function widget( $args, $instance ) {
if ( ! empty( $data['terms'] ) )
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
if ( $data['query_type'] == 'or' )
if ( $data['query_type'] == 'or' || $data['query_type'] == 'only' )
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
}
}
@@ -342,7 +346,13 @@ public function widget( $args, $instance ) {
} else {
$class = '';
$link = add_query_arg( $arg, implode( ',', $current_filter ), $link );
if ( $query_type == 'only' ) {
$link = add_query_arg( $arg, $term->term_id, $link );
} else if ( ! empty( $data['terms'] ) ) {
$link = add_query_arg( $arg, implode( ',', $current_filter ), $link );
}
}
@@ -357,19 +367,42 @@ public function widget( $args, $instance ) {
// Query type Arg
if ( $query_type == 'or' && ! ( sizeof( $current_filter ) == 1 && isset( $_chosen_attributes[ $taxonomy ]['terms'] ) && is_array( $_chosen_attributes[ $taxonomy ]['terms'] ) && in_array( $term->term_id, $_chosen_attributes[ $taxonomy ]['terms'] ) ) )
$link = add_query_arg( 'query_type_' . sanitize_title( $instance['attribute'] ), 'or', $link );
$li = '';
$li.= '<li ' . $class . '>';
echo '<li ' . $class . '>';
$li.= ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">' : '<span>';
echo ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">' : '<span>';
$li.= $term->name;
echo $term->name;
$li.= ( $count > 0 || $option_is_set ) ? '</a>' : '</span>';
echo ( $count > 0 || $option_is_set ) ? '</a>' : '</span>';
$li.= ' <small class="count">' . $count . '</small></li>';
$output_buffer[] = $li;
}
// if we're operating in only mode, install an 'all' option to the front of the list
// base off the last item in the list, minus the filtered param
if ($query_type == 'only' ) {
$extra_item = array();
echo ' <small class="count">' . $count . '</small></li>';
// recalc the current page URL's filter
$current_filter = ( isset( $_GET[ $arg ] ) ) ? explode( ',', $_GET[ $arg ] ) : array();
if ( ! is_array( $current_filter ) )
$current_filter = array();
$current_filter = array_map( 'esc_attr', $current_filter );
}
$class = count($current_filter) == 0 ? 'class = "chosen"' : '';
$extra_item[] = '<li ' . $class . '>';
$link = remove_query_arg($arg, $link);
$extra_item[] = '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">';
$extra_item[] = 'All';
$extra_item[] = '</a>';
$output_buffer = array_merge($extra_item, $output_buffer);
}
echo implode('', $output_buffer);
echo "</ul>";
} // End display type conditional

1 comment on commit effd6b0

@hughc

This comment has been minimized.

Show comment
Hide comment
@hughc

hughc Jul 18, 2014

Owner

'all' option selected

image

'rings' option selected

image

Owner

hughc commented on effd6b0 Jul 18, 2014

'all' option selected

image

'rings' option selected

image

Please sign in to comment.