diff --git a/admin/woocommerce-admin-reports.php b/admin/woocommerce-admin-reports.php index 0787cbd6584..5da6ba33cae 100644 --- a/admin/woocommerce-admin-reports.php +++ b/admin/woocommerce-admin-reports.php @@ -62,7 +62,12 @@ function woocommerce_reports() { 'title' => __('Top earners', 'woocommerce'), 'description' => '', 'function' => 'woocommerce_top_earners' - ) + ), + array( + 'title' => __('Sales by category', 'woocommerce'), + 'description' => '', + 'function' => 'woocommerce_category_sales' + ), ) ), 'customers' => array( @@ -1908,4 +1913,284 @@ function woocommerce_monthly_taxes() { get_var( "SELECT post_date FROM $wpdb->posts WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;" ); + $first_year = ( $first_year ) ? date( 'Y', strtotime( $first_year ) ) : date( 'Y' ); + + $current_year = isset( $_POST['show_year'] ) ? $_POST['show_year'] : date( 'Y', current_time( 'timestamp' ) ); + $start_date = strtotime( $current_year . '0101' ); + ?> +
+

+

+
+ current_time( 'timestamp' ) . '000' ) + continue; + + $month = date( 'Ym', strtotime( date( 'Ym', strtotime( '+ '. $count . ' MONTH', $start_date ) ) . '01' ) ); + + // Get order items + $order_items_serialized = $wpdb->get_col(" + SELECT meta.meta_value AS items 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 ) + + 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 ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "') + AND '{$month}' = date_format(posts.post_date,'%Y%m') + "); + + if ( $order_items_serialized ) { + foreach ( $order_items_serialized as $order_items_array ) { + $order_items_array = maybe_unserialize( $order_items_array ); + if ( is_array( $order_items_array ) ) + foreach ( $order_items_array as $item ) { + + if ( ! isset( $item_sales[ $count ][ $item['id'] ] ) ) + $item_sales[ $count ][ $item['id'] ] = 0; + + if ( ! empty( $item['line_total'] ) ) + $item_sales[ $count ][ $item['id'] ] += $item['line_total']; + + } + } + } + } + + $categories = get_terms( 'product_cat', array( 'parent' => 0 ) ); + ?> + + + + + = date ( 'm' ) && $current_year == date( 'Y' ) ) + continue; + ?> + + + + + + = date( 'm' ) && $current_year == date( 'Y' ) ) + break; + else + $month_totals[ $count ] = 0; + + foreach ( $categories as $category ) { + + $cat_total = 0; + $category_chart_data = $term_ids = array(); + + $term_ids = get_term_children( $category->term_id, 'product_cat' ); + $term_ids[] = $category->term_id; + $product_ids = get_objects_in_term( $term_ids, 'product_cat' ); + + $category_sales_html = ''; + + for ( $count = 0; $count < 12; $count++ ) { + + if ( $count >= date( 'm' ) && $current_year == date( 'Y' ) ) + continue; + + if ( ! empty( $item_sales[ $count ] ) ) { + $matches = array_intersect_key( $item_sales[ $count ], array_flip( $product_ids ) ); + $total = array_sum( $matches ); + $cat_total += $total; + } else { + $total = 0; + } + + $month_totals[ $count ] += $total; + + $category_sales_html .= ''; + + $category_chart_data[] = array( strtotime( date( 'Ymd', strtotime( '2012-' . ( $count + 1 ) . '-01' ) ) ) . '000', $total ); + } + + if ( $cat_total == 0 ) + continue; + + $category_totals[] = $cat_total; + + $category_sales_html .= ''; + + $category_sales_html .= ''; + + echo $category_sales_html; + + $chart_data[ $category->name ] = $category_chart_data; + + if ( $cat_total > $top_cat ) { + $top_cat = $cat_total; + $top_cat_name = $category->name; + } + + if ( $cat_total < $bottom_cat || $bottom_cat === '' ) { + $bottom_cat = $cat_total; + $bottom_cat_name = $category->name; + } + + } + + sort( $category_totals ); + + echo ''; + for ( $count = 0; $count < 12; $count++ ) + if ( $count >= date( 'm' ) && $current_year == date( 'Y' ) ) + break; + else + echo ''; + echo ''; + + ?> +
' . $category->name . '' . woocommerce_price( $total ) . '' . woocommerce_price( $cat_total ) . '
' . __( 'Total', 'woocommerce' ) . '' . woocommerce_price( $month_totals[ $count ] ) . '' . woocommerce_price( array_sum( $month_totals ) ) . '
+ +
+ +
+
+
+

+
+

+
+
+
+

+
+

+
+
+
+

+
+

0 ) + echo woocommerce_price( array_sum( $category_totals ) / sizeof( $category_totals ) ); + else + echo __( 'N/A', 'woocommerce' ); + ?>

+
+
+
+

+
+

+
+
+
+
+
+

+
+
+
+
+
+
+ +