From b6b2da9463b94965fe37664ce012b158cdacc43c Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 11 May 2012 00:51:31 +0100 Subject: [PATCH] Optimisations to monthly and product reports --- admin/woocommerce-admin-reports.php | 222 ++++++++++++++-------------- readme.txt | 1 + 2 files changed, 115 insertions(+), 108 deletions(-) diff --git a/admin/woocommerce-admin-reports.php b/admin/woocommerce-admin-reports.php index 0c91a886d70..f48c0013d68 100644 --- a/admin/woocommerce-admin-reports.php +++ b/admin/woocommerce-admin-reports.php @@ -1050,127 +1050,133 @@ function woocommerce_top_earners() { */ function woocommerce_product_sales() { - global $start_date, $end_date, $woocommerce; + global $wpdb, $woocommerce; - $chosen_product_id = (isset($_POST['product_id'])) ? $_POST['product_id'] : ''; + $chosen_product_ids = ( isset( $_POST['product_ids'] ) ) ? (array) $_POST['product_ids'] : ''; - if ($chosen_product_id) : - $start_date = date('Ym', strtotime( '-12 MONTHS', current_time('timestamp') )).'01'; - $end_date = date('Ymd', current_time('timestamp')); + if ( $chosen_product_ids && is_array( $chosen_product_ids ) ) { + + $start_date = date( 'Ym', strtotime( '-12 MONTHS', current_time('timestamp') ) ) . '01'; + $end_date = date( 'Ymd', current_time( 'timestamp' ) ); + + $max_sales = $max_totals = 0; + $product_sales = $product_totals = array(); - $start_date = strtotime($start_date); - $end_date = strtotime($end_date); + // Get titles and ID's related to product + $chosen_product_titles = array(); + $children_ids = array(); - // Get orders to display in widget - add_filter( 'posts_where', 'orders_within_range' ); + foreach ( $chosen_product_ids as $product_id ) { + $children = (array) get_posts( 'post_parent=' . $product_id . '&fields=ids&post_status=any&numberposts=-1' ); + $children_ids = $children_ids + $children; + $chosen_product_titles[] = get_the_title( $product_id ); + } + + // Get order items + $order_items = $wpdb->get_results(" + SELECT meta.meta_value AS items, posts.post_date FROM {$wpdb->posts} AS posts + + LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id + LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID + LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) + LEFT JOIN {$wpdb->terms} AS term USING( term_id ) - $args = array( - 'numberposts' => -1, - 'orderby' => 'post_date', - 'order' => 'ASC', - 'post_type' => 'shop_order', - 'post_status' => 'publish' , - 'suppress_filters'=> 0, - 'tax_query' => array( - array( - 'taxonomy' => 'shop_order_status', - 'terms' => array('completed', 'processing', 'on-hold'), - 'field' => 'slug', - 'operator' => 'IN' - ) - ) - ); - $orders = get_posts( $args ); + WHERE meta.meta_key = '_order_items' + AND posts.post_type = 'shop_order' + AND posts.post_status = 'publish' + AND tax.taxonomy = 'shop_order_status' + AND term.slug IN ('completed', 'processing', 'on-hold') + AND posts.post_date > date_sub( NOW(), INTERVAL 1 YEAR ) + ORDER BY posts.post_date ASC + "); - $max_sales = 0; - $max_totals = 0; - $product_sales = array(); - $product_totals = array(); + if ( $order_items ) { - // Get ID's related to product - $chosen_product = new WC_Product( $chosen_product_id ); - $child_ids = $chosen_product->get_children(); - - if ($orders) : - foreach ($orders as $order) : - $date = date('Ym', strtotime( $order->post_date )); - $order_items = (array) get_post_meta( $order->ID, '_order_items', true ); - foreach ($order_items as $item) : - - if (isset($item['line_total'])) $row_cost = $item['line_total']; - elseif (isset($item['cost'])) $row_cost = $item['cost'] * $item['qty']; - else continue; - - if ($item['id']!=$chosen_product_id && !in_array($item['id'], $child_ids)) continue; - $product_sales[$date] = isset($product_sales[$date]) ? $product_sales[$date] + $item['qty'] : $item['qty']; - - $product_totals[$date] = isset($product_totals[$date]) ? $product_totals[$date] + $row_cost : $row_cost; - - if ($product_sales[$date] > $max_sales) $max_sales = $product_sales[$date]; - if ($product_totals[$date] > $max_totals) $max_totals = $product_totals[$date]; - endforeach; - endforeach; - endif; - - remove_filter( 'posts_where', 'orders_within_range' ); - endif; - ?> -
-

-

+ +
+