show_categories = array_map( 'absint', $_GET['show_categories'] ); elseif ( isset( $_GET['show_categories'] ) ) $this->show_categories = array( absint( $_GET['show_categories'] ) ); } /** * Get the legend for the main chart sidebar * @return array */ public function get_chart_legend() { if ( ! $this->show_categories ) return array(); $legend = array(); $index = 0; foreach( $this->show_categories as $category ) { $category = get_term( $category, 'product_cat' ); $term_ids = get_term_children( $category->term_id, 'product_cat' ); $term_ids[] = $category->term_id; $total = 0; $product_ids = array_unique( get_objects_in_term( $term_ids, 'product_cat' ) ); foreach ( $product_ids as $id ) { if ( isset( $this->item_sales[ $id ] ) ) { $total += $this->item_sales[ $id ]; } } //if ( ! $total ) // continue; $legend[] = array( 'title' => sprintf( __( '%s sales in %s', 'woocommerce' ), '' . woocommerce_price( $total ) . '', $category->name ), 'color' => isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[ 0 ], 'highlight_series' => $index ); $index++; } return $legend; } /** * Output the report */ public function output_report() { global $woocommerce, $wpdb, $wp_locale; $ranges = array( 'year' => __( 'Year', 'woocommerce' ), 'last_month' => __( 'Last Month', 'woocommerce' ), 'month' => __( 'This Month', 'woocommerce' ), '7day' => __( 'Last 7 Days', 'woocommerce' ) ); $this->chart_colours = array( '#3498db', '#34495e', '#1abc9c', '#2ecc71', '#f1c40f', '#e67e22', '#e74c3c', '#2980b9', '#8e44ad', '#2c3e50', '#16a085', '#27ae60', '#f39c12', '#d35400', '#c0392b' ); $current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : '7day'; switch ( $current_range ) { case 'custom' : $this->start_date = strtotime( sanitize_text_field( $_GET['start_date'] ) ); $this->end_date = strtotime( '12am + 1 day', strtotime( sanitize_text_field( $_GET['end_date'] ) ) ); if ( ! $this->end_date ) $this->end_date = current_time('timestamp'); $interval = 0; $min_date = $this->start_date; while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) { $interval ++; } // 1 months max for day view if ( $interval > 1 ) $this->chart_groupby = 'month'; else $this->chart_groupby = 'day'; break; case 'year' : $this->start_date = strtotime( 'first day of january', current_time('timestamp') ); $this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) ); $this->chart_groupby = 'month'; break; case 'last_month' : $this->start_date = strtotime( 'first day of last month', current_time('timestamp') ); $this->end_date = strtotime( 'last day of last month', current_time('timestamp') ); $this->chart_groupby = 'day'; break; case 'month' : $this->start_date = strtotime( 'first day of this month', current_time('timestamp') ); $this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) ); $this->chart_groupby = 'day'; break; case '7day' : default : $this->start_date = strtotime( '-6 days', current_time( 'timestamp' ) ); $this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) ); $this->chart_groupby = 'day'; break; } // Group by switch ( $this->chart_groupby ) { case 'day' : $this->group_by_query = 'YEAR(post_date), MONTH(post_date), DAY(post_date)'; $this->chart_interval = max( 0, ( $this->end_date - $this->start_date ) / ( 60 * 60 * 24 ) ); $this->barwidth = 60 * 60 * 24 * 1000; break; case 'month' : $this->group_by_query = 'YEAR(post_date), MONTH(post_date)'; $this->chart_interval = 0; $min_date = $this->start_date; while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) { $this->chart_interval ++; } $this->barwidth = 60 * 60 * 24 * 7 * 4 * 1000; break; } // Get item sales data if ( $this->show_categories ) { $order_items = $this->get_order_report_data( array( 'data' => array( '_product_id' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'product_id' ), '_line_total' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'order_item_amount' ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date' ), ), 'group_by' => 'ID, product_id', 'query_type' => 'get_results', 'filter_range' => true ) ); $this->item_sales = array(); $this->item_sales_and_times = array(); if ( $order_items ) { foreach ( $order_items as $order_item ) { switch ( $this->chart_groupby ) { case 'day' : $time = strtotime( date( 'Ymd', strtotime( $order_item->post_date ) ) ) * 1000; break; case 'month' : $time = strtotime( date( 'Ym', strtotime( $order_item->post_date ) ) . '01' ) * 1000; break; } $this->item_sales_and_times[ $time ][ $order_item->product_id ] = isset( $this->item_sales_and_times[ $time ][ $order_item->product_id ] ) ? $this->item_sales_and_times[ $time ][ $order_item->product_id ] + $order_item->order_item_amount : $order_item->order_item_amount; $this->item_sales[ $order_item->product_id ] = isset( $this->item_sales[ $order_item->product_id ] ) ? $this->item_sales[ $order_item->product_id ] + $order_item->order_item_amount : $order_item->order_item_amount; } } } include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php'); } /** * [get_chart_widgets description] * @return array */ public function get_chart_widgets() { return array( array( 'title' => __( 'Categories', 'woocommerce' ), 'callback' => array( $this, 'category_widget' ) ) ); } /** * Category selection * @return void */ public function category_widget() { $categories = get_terms( 'product_cat', array( 'orderby' => 'name' ) ); ?>
show_categories ) { ?>

show_categories as $category ) { $category = get_term( $category, 'product_cat' ); $term_ids = get_term_children( $category->term_id, 'product_cat' ); $term_ids[] = $category->term_id; $product_ids = array_unique( get_objects_in_term( $term_ids, 'product_cat' ) ); $category_total = 0; $category_chart_data = array(); for ( $i = 0; $i <= $this->chart_interval; $i ++ ) { $interval_total = 0; switch ( $this->chart_groupby ) { case 'day' : $time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $this->start_date ) ) ) * 1000; break; case 'month' : $time = strtotime( date( 'Ym', strtotime( "+{$i} MONTH", $this->start_date ) ) . '01' ) * 1000; break; } foreach ( $product_ids as $id ) { if ( isset( $this->item_sales_and_times[ $time ][ $id ] ) ) { $interval_total += $this->item_sales_and_times[ $time ][ $id ]; $category_total += $this->item_sales_and_times[ $time ][ $id ]; } } $category_chart_data[] = array( $time, $interval_total ); } //if ( ! $category_total ) // continue; $chart_data[ $category->term_id ]['category'] = $category->name; $chart_data[ $category->term_id ]['data'] = $category_chart_data; $index ++; } ?>