diff --git a/admin/woocommerce-admin-reports.php b/admin/woocommerce-admin-reports.php index f5a9b36efad..ec63b023470 100644 --- a/admin/woocommerce-admin-reports.php +++ b/admin/woocommerce-admin-reports.php @@ -7,7 +7,7 @@ * @author WooThemes * @category Admin * @package WooCommerce/Admin/Reports - * @version 1.6.4 + * @version 1.7 */ /** @@ -68,6 +68,11 @@ function woocommerce_reports() { 'description' => '', 'function' => 'woocommerce_category_sales' ), + array( + 'title' => __('Sales by coupon', 'woocommerce'), + 'description' => '', + 'function' => 'woocommerce_coupon_sales' + ) ) ), 'customers' => array( @@ -2199,4 +2204,302 @@ function woocommerce_category_sales() { }); get_var( "SELECT post_date FROM $wpdb->posts WHERE post_date != 0 AND post_type='shop_order' 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' ); + + $order_statuses = implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ); + + + $coupons = $wpdb->get_col( " + SELECT DISTINCT meta.meta_value FROM {$wpdb->postmeta} AS meta + LEFT JOIN {$wpdb->posts} AS posts 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 = 'coupons' + AND posts.post_type = 'shop_order' + AND posts.post_status = 'publish' + AND tax.taxonomy = 'shop_order_status' + AND term.slug IN ('{$order_statuses}') + "); + + + + ?> + +
+ + 0 ) : + + $coupons = $_POST['show_coupons']; + + $coupon_sales = $monthly_totals = array(); + + foreach( $coupons as $coupon ) : + + $monthly_sales = $wpdb->get_results($wpdb->prepare(" + SELECT SUM(postmeta.meta_value) AS order_total, date_format(posts.post_date, '%%Y%%m') as month FROM {$wpdb->posts} AS posts + + INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID=postmeta.post_ID + INNER JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID + INNER JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) + INNER JOIN {$wpdb->terms} AS term USING( term_id ) + + WHERE postmeta.meta_key = '_order_total' + AND tax.taxonomy = 'shop_order_status' + AND term.slug IN ('{$order_statuses}') + AND posts.post_type = 'shop_order' + AND posts.post_status = 'publish' + AND '{$current_year}' = date_format(posts.post_date,'%%Y') + AND posts.ID IN ( + SELECT post_id FROM {$wpdb->postmeta} AS meta + + WHERE meta.meta_key = 'coupons' + AND meta.meta_value = '%s' + ) + GROUP BY month", $coupon ), OBJECT ); + + foreach( $monthly_sales as $sales ) { + $month = $sales->month; + $coupon_sales[$coupon][$month] = $sales->order_total; + } + + + endforeach; + + ?> + ++ = date ( 'm' ) && $current_year == date( 'Y' ) ) + continue; + $month = date( 'Ym', strtotime( date( 'Ym', strtotime( '+ '. $count . ' MONTH', $start_date ) ) . '01' ) ); + + // set elements before += them below + $monthly_totals[$month] = 0; + + $column_count++; + ?> + | + + | + |
---|---|---|
' . $coupon_code . ' | '; + + for( $count = 0; $count < 12; $count++ ) : + + if ( $count >= date ( 'm' ) && $current_year == date( 'Y' ) ) + continue; + + $month = date( 'Ym', strtotime( date( 'Ym', strtotime( '+ '. $count . ' MONTH', $start_date ) ) . '01' ) ); + + $amount = isset( $sales[$month] ) ? $sales[$month] : 0; + echo '' . woocommerce_price( $amount ) . ' | '; + + $monthly_totals[$month] += $amount; + + $chart_data[$coupon_code][] = array( strtotime( date( 'Ymd', strtotime( $month . '01' ) ) ) . '000', $amount ); + + endfor; + + echo '' . woocommerce_price( array_sum( $sales ) ) . ' | '; + + // total sales across all months + $coupon_totals[$coupon_code] = array_sum( $sales ); + + echo '
' . __( 'Total', 'woocommerce' ) . ' | '; + + foreach( $monthly_totals as $month => $totals ) + echo '' . woocommerce_price( $totals ) . ' | '; + + echo '' . woocommerce_price( array_sum( $monthly_totals ) ) . ' |