show_categories = is_array( $_GET['show_categories'] ) ? array_map( 'absint', $_GET['show_categories'] ) : array( absint( $_GET['show_categories'] ) ); } } /** * Get all product ids in a category (and its children). * * @param int $category_id Category ID. * @return array */ public function get_products_in_category( $category_id ) { $term_ids = get_term_children( $category_id, 'product_cat' ); $term_ids[] = $category_id; $product_ids = get_objects_in_term( $term_ids, 'product_cat' ); return array_unique( apply_filters( 'woocommerce_report_sales_by_category_get_products_in_category', $product_ids, $category_id ) ); } /** * Get the legend for the main chart sidebar. * * @return array */ public function get_chart_legend() { if ( empty( $this->show_categories ) ) { return array(); } $legend = array(); $index = 0; foreach ( $this->show_categories as $category ) { $category = get_term( $category, 'product_cat' ); $total = 0; $product_ids = $this->get_products_in_category( $category->term_id ); foreach ( $product_ids as $id ) { if ( isset( $this->item_sales[ $id ] ) ) { $total += $this->item_sales[ $id ]; } } $legend[] = array( /* translators: 1: total items sold 2: category name */ 'title' => sprintf( __( '%1$s sales in %2$s', 'woocommerce' ), '' . wc_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() { $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'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day'; if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) { $current_range = '7day'; } $this->check_current_range_nonce( $current_range ); $this->calculate_current_range( $current_range ); // Get item sales data. if ( ! empty( $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' => 'SUM', 'name' => 'order_item_amount', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => 'ID, product_id, post_date', 'query_type' => 'get_results', 'filter_range' => true, ) ); $this->item_sales = array(); $this->item_sales_and_times = array(); if ( is_array( $order_items ) ) { foreach ( $order_items as $order_item ) { switch ( $this->chart_groupby ) { case 'day': $time = strtotime( gmdate( 'Ymd', strtotime( $order_item->post_date ) ) ) * 1000; break; case 'month': default: $time = strtotime( gmdate( '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. * * @return array */ public function get_chart_widgets() { return array( array( 'title' => __( 'Categories', 'woocommerce' ), 'callback' => array( $this, 'category_widget' ), ), ); } /** * Output category widget. */ public function category_widget() { $categories = get_terms( 'product_cat', array( 'orderby' => 'name' ) ); ?>
show_categories ) ) { ?>