added rating_filter method to WC_Query

This commit is contained in:
Ibrahim Ibn Dawood 2015-11-13 15:11:44 +05:30
parent c369fac02b
commit 8deac2c563
1 changed files with 54 additions and 0 deletions

View File

@ -920,6 +920,60 @@ class WC_Query {
return (array) $filtered_posts;
}
/**
* Rating filter Init.
*/
public function rating_filter_init() {
if ( apply_filters( 'woocommerce_is_rating_filter_active', is_active_widget( false, false, 'woocommerce_rating_filter', true ) ) && ! is_admin() ) {
add_filter( 'loop_shop_post_in', array( $this, 'rating_filter' ) );
}
}
/**
* Rating Filter post filter.
*
* @param array $filtered_posts
* @return array
*/
public function rating_filter( $filtered_posts = array() ) {
global $wpdb;
if( isset( $_GET['min_rating'] ) ) {
$matched_products = array();
$min = isset( $_GET['min_rating'] ) ? floatval( $_GET['min_rating'] ) : 0;
$matched_products_query = apply_filters( 'woocommerce_rating_filter_results', $wpdb->get_results( $wpdb->prepare( "
SELECT comment_post_ID, ROUND( AVG( meta_value ), 2 ) as average_rating FROM {$wpdb->commentmeta}
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_approved = '1'
AND meta_value > 0
GROUP BY comment_post_ID
HAVING ROUND( AVG( meta_value ), 2 ) >= %d
", $min ), OBJECT_K ), $min );
if ( $matched_products_query ) {
foreach ( $matched_products_query as $commentmeta ) {
$matched_products[] = $commentmeta->comment_post_ID;
}
}
$matched_products = array_unique( $matched_products );
// Filter the id's
if ( 0 === sizeof( $filtered_posts ) ) {
$filtered_posts = $matched_products;
} else {
$filtered_posts = array_intersect( $filtered_posts, $matched_products );
}
$filtered_posts[] = 0;
}
return (array) $filtered_posts;
}
}
endif;