From cac6a5dee7337d4c8782b862ffa197e7e2533aba Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 23 Mar 2012 20:54:45 +0000 Subject: [PATCH] price filter --- readme.txt | 1 + widgets/widget-price_filter.php | 39 ++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/readme.txt b/readme.txt index 4e8c29a3e7f..07651cef1cd 100644 --- a/readme.txt +++ b/readme.txt @@ -158,6 +158,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Tweak - Queries on report pages to replace get_posts to resolve issues on stores with a shed load of orders * Tweak - Remove case sensitively from order tracking and force email lowercase on checkout * Tweak - woocommerce_update_variation_values trigger in JS +* Tweak - Optimised price filter query (Thanks Lucas Stark) * Fix - After tax coupons for categories * Fix - Multiple variation image upload * Fix - User email check for coupons diff --git a/widgets/widget-price_filter.php b/widgets/widget-price_filter.php index 5fe336cf9d9..f0940f72ffa 100644 --- a/widgets/widget-price_filter.php +++ b/widgets/widget-price_filter.php @@ -37,38 +37,37 @@ function woocommerce_price_filter_init() { function woocommerce_price_filter($filtered_posts) { global $wpdb; - if (isset($_GET['max_price']) && isset($_GET['min_price'])) : + if ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) { $matched_products = array(); - $low = floatval($_GET['min_price']); - $high = floatval($_GET['max_price']); + $min = floatval( $_GET['min_price'] ); + $max = floatval( $_GET['max_price'] ); - $matched_products_query = $wpdb->get_results( - $wpdb->prepare( - "SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts - INNER JOIN $wpdb->postmeta ON ID = post_id - WHERE (post_type='product' OR post_type='product_variation') AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d", '_price', $low, $high), OBJECT_K); + $matched_products_query = $wpdb->get_results( $wpdb->prepare(" + SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts + INNER JOIN $wpdb->postmeta ON ID = post_id + WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d + ", '_price', $min, $max ), OBJECT_K ); - - if ($matched_products_query) : - foreach ($matched_products_query as $product) : - if ($product->post_type == 'product') + if ( $matched_products_query ) { + foreach ( $matched_products_query as $product ) { + if ( $product->post_type == 'product' ) $matched_products[] = $product->ID; - if ($product->post_parent > 0 && !in_array($product->post_parent, $matched_products)) + if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) $matched_products[] = $product->post_parent; - endforeach; - endif; + } + } // Filter the id's - if (sizeof($filtered_posts) == 0) : + if ( sizeof( $filtered_posts ) == 0) { $filtered_posts = $matched_products; $filtered_posts[] = 0; - else : - $filtered_posts = array_intersect($filtered_posts, $matched_products); + } else { + $filtered_posts = array_intersect( $filtered_posts, $matched_products ); $filtered_posts[] = 0; - endif; + } - endif; + } return (array) $filtered_posts; }