Rating counts
This commit is contained in:
parent
5f98720206
commit
6bdd12d960
|
@ -593,6 +593,40 @@ class WC_Query {
|
|||
return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tax query which was used by the main query.
|
||||
* @return array
|
||||
*/
|
||||
public static function get_main_tax_query() {
|
||||
global $wp_the_query;
|
||||
|
||||
$args = $wp_the_query->query_vars;
|
||||
$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
|
||||
|
||||
if ( ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) {
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => $args['taxonomy'],
|
||||
'terms' => array( $args['term'] ),
|
||||
'field' => 'slug'
|
||||
);
|
||||
}
|
||||
|
||||
return $tax_query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the meta query which was used by the main query.
|
||||
* @return array
|
||||
*/
|
||||
public static function get_main_meta_query() {
|
||||
global $wp_the_query;
|
||||
|
||||
$args = $wp_the_query->query_vars;
|
||||
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
|
||||
|
||||
return $meta_query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Layered Nav Init.
|
||||
*/
|
||||
|
|
|
@ -226,26 +226,13 @@ class WC_Widget_Layered_Nav extends WC_Widget {
|
|||
$_products_in_term = wc_get_term_product_ids( $term->term_id, $taxonomy );
|
||||
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
|
||||
$option_is_set = in_array( $term->slug, $current_values );
|
||||
$count = $this->get_filtered_term_product_count( $term, $taxonomy, $query_type );
|
||||
|
||||
// If this is an AND query, only show options with count > 0
|
||||
if ( 'and' === $query_type ) {
|
||||
$count = $this->get_filtered_term_count( $term, $taxonomy, $query_type );
|
||||
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
}
|
||||
|
||||
if ( 0 === $count && ! $option_is_set ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this is an OR query, show all options so search can be expanded
|
||||
} else {
|
||||
$count = $this->get_filtered_term_count( $term, $taxonomy, $query_type );
|
||||
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
}
|
||||
// Only show options with count > 0
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
} elseif ( 'and' === $query_type && 0 === $count && ! $option_is_set ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
|
||||
|
@ -310,26 +297,17 @@ class WC_Widget_Layered_Nav extends WC_Widget {
|
|||
}
|
||||
|
||||
/**
|
||||
* Count terms after other filters have occured by adjusting the main query.
|
||||
* Count products after other filters have occured by adjusting the main query.
|
||||
* @param object $term
|
||||
* @param string $taxonomy
|
||||
* @param string $query_type
|
||||
* @return int
|
||||
*/
|
||||
protected function get_filtered_term_count( $term, $taxonomy, $query_type ) {
|
||||
global $wpdb, $wp_the_query;
|
||||
protected function get_filtered_term_product_count( $term, $taxonomy, $query_type ) {
|
||||
global $wpdb;
|
||||
|
||||
$args = $wp_the_query->query_vars;
|
||||
$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
|
||||
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
|
||||
|
||||
if ( ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) {
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => $args['taxonomy'],
|
||||
'terms' => array( $args['term'] ),
|
||||
'field' => 'slug'
|
||||
);
|
||||
}
|
||||
$tax_query = WC_Query::get_main_tax_query();
|
||||
$meta_query = WC_Query::get_main_meta_query();
|
||||
|
||||
if ( 'or' === $query_type ) {
|
||||
foreach ( $tax_query as $key => $query ) {
|
||||
|
@ -386,21 +364,13 @@ class WC_Widget_Layered_Nav extends WC_Widget {
|
|||
continue;
|
||||
}
|
||||
|
||||
$count = $this->get_filtered_term_count( $term, $taxonomy, $query_type );
|
||||
$count = $this->get_filtered_term_product_count( $term, $taxonomy, $query_type );
|
||||
|
||||
// If this is an AND query, only show options with count > 0
|
||||
if ( 'and' === $query_type ) {
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
}
|
||||
if ( 0 === $count && ! $option_is_set ) {
|
||||
continue;
|
||||
}
|
||||
// If this is an OR query, show all options so search can be expanded
|
||||
} else {
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
}
|
||||
// Only show options with count > 0
|
||||
if ( 0 < $count ) {
|
||||
$found = true;
|
||||
} elseif ( 'and' === $query_type && 0 === $count && ! $option_is_set ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) );
|
||||
|
|
|
@ -74,6 +74,47 @@ class WC_Widget_Rating_Filter extends WC_Widget {
|
|||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count products after other filters have occured by adjusting the main query.
|
||||
* @param int $rating
|
||||
* @return int
|
||||
*/
|
||||
protected function get_filtered_product_count( $rating ) {
|
||||
global $wpdb;
|
||||
|
||||
$tax_query = WC_Query::get_main_tax_query();
|
||||
$meta_query = WC_Query::get_main_meta_query();
|
||||
|
||||
// Unset current rating filter
|
||||
foreach ( $meta_query as $key => $query ) {
|
||||
if ( ! empty( $query['rating_filter'] ) ) {
|
||||
unset( $meta_query[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
// Set new rating filter
|
||||
$meta_query[] = array(
|
||||
'key' => '_wc_average_rating',
|
||||
'value' => $rating,
|
||||
'compare' => '>=',
|
||||
'type' => 'DECIMAL',
|
||||
'rating_filter' => true
|
||||
);
|
||||
|
||||
$meta_query = new WP_Meta_Query( $meta_query );
|
||||
$tax_query = new WP_Tax_Query( $tax_query );
|
||||
|
||||
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
|
||||
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );
|
||||
|
||||
$sql = "SELECT COUNT( {$wpdb->posts}.ID ) FROM {$wpdb->posts} ";
|
||||
$sql .= $tax_query_sql['join'] . $meta_query_sql['join'];
|
||||
$sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' ";
|
||||
$sql .= $tax_query_sql['where'] . $meta_query_sql['where'];
|
||||
|
||||
return absint( $wpdb->get_var( $sql ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* widget function.
|
||||
*
|
||||
|
@ -93,6 +134,8 @@ class WC_Widget_Rating_Filter extends WC_Widget {
|
|||
return;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
$min_rating = isset( $_GET['min_rating'] ) ? absint( $_GET['min_rating'] ) : '';
|
||||
|
||||
$this->widget_start( $args, $instance );
|
||||
|
@ -100,8 +143,15 @@ class WC_Widget_Rating_Filter extends WC_Widget {
|
|||
echo '<ul>';
|
||||
|
||||
for ( $rating = 4; $rating >= 1; $rating-- ) {
|
||||
$link = $this->get_page_base_url();
|
||||
$link = $min_rating !== $rating ? add_query_arg( 'min_rating', $rating, $link ) : $link;
|
||||
$count = $this->get_filtered_product_count( $rating );
|
||||
|
||||
if ( ! $count ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$found = true;
|
||||
$link = $this->get_page_base_url();
|
||||
$link = $min_rating !== $rating ? add_query_arg( 'min_rating', $rating, $link ) : $link;
|
||||
|
||||
echo '<li class="wc-layered-nav-rating ' . ( ! empty( $_GET['min_rating'] ) && $rating === absint( $_GET['min_rating'] ) ? 'chosen' : '' ) . '">';
|
||||
|
||||
|
@ -109,7 +159,7 @@ class WC_Widget_Rating_Filter extends WC_Widget {
|
|||
|
||||
echo '<span class="star-rating" title="' . esc_attr( sprintf( __( 'Rated %s and above', 'woocommerce' ), $rating ) ). '">
|
||||
<span style="width:' . esc_attr( ( $rating / 5 ) * 100 ) . '%">' . sprintf( __( 'Rated %s and above', 'woocommerce'), $rating ) . '</span>
|
||||
</span>';
|
||||
</span> (' . $count . ')';
|
||||
|
||||
echo '</a>';
|
||||
|
||||
|
@ -119,5 +169,11 @@ class WC_Widget_Rating_Filter extends WC_Widget {
|
|||
echo '</ul>';
|
||||
|
||||
$this->widget_end( $args );
|
||||
|
||||
if ( ! $found ) {
|
||||
ob_end_clean();
|
||||
} else {
|
||||
echo ob_get_clean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue