diff --git a/admin/class-wc-admin-reports.php b/admin/class-wc-admin-reports.php index 9de68f36192..6f44a1f2d5a 100644 --- a/admin/class-wc-admin-reports.php +++ b/admin/class-wc-admin-reports.php @@ -79,22 +79,11 @@ class WC_Admin_Reports { 'hide_title' => true, 'callback' => array( $this, 'get_report' ) ), - "top_sellers" => array( - 'title' => __( 'Top Sellers', 'woocommerce' ), - 'description' => '', - 'hide_title' => true, - 'callback' => array( $this, 'get_report' ) - ), - "top_earners" => array( - 'title' => __( 'Top Earners', 'woocommerce' ), - 'description' => '', - 'hide_title' => true, - 'callback' => array( $this, 'get_report' ) - ), "sales_by_product" => array( 'title' => __( 'Sales by product', 'woocommerce' ), 'description' => '', - 'callback' => 'woocommerce_product_sales' + 'hide_title' => true, + 'callback' => array( $this, 'get_report' ) ), "sales_by_category" => array( 'title' => __( 'Sales by category', 'woocommerce' ), @@ -218,59 +207,6 @@ new WC_Admin_Reports(); /* -
  • -

    - ' . __( 'Showing reports for:' ) . ' ' . implode( ', ', $product_id_filter_titles ) . '

    '; - - } else { - ?> -
    -
    - - - - - - - -
    - -
    - -
  • - */ /* @@ -408,155 +344,6 @@ $customer_orders = $this->get_order_report_data( array( - -/** - * Output the product sales chart for single products. - * - * @access public - * @return void - */ -function woocommerce_product_sales() { - - global $wpdb, $woocommerce; - - $chosen_product_ids = ( isset( $_POST['product_ids'] ) ) ? array_map( 'absint', (array) $_POST['product_ids'] ) : ''; - - 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(); - - // Get titles and ID's related to product - $chosen_product_titles = array(); - $children_ids = array(); - - 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 = apply_filters( 'woocommerce_reports_product_sales_order_items', $wpdb->get_results( " - SELECT order_item_meta_2.meta_value as product_id, posts.post_date, SUM( order_item_meta.meta_value ) as item_quantity, SUM( order_item_meta_3.meta_value ) as line_total - FROM {$wpdb->prefix}woocommerce_order_items as order_items - - LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id - LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id - LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_3 ON order_items.order_item_id = order_item_meta_3.order_item_id - LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.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 posts.post_type = 'shop_order' - AND order_item_meta_2.meta_value IN ('" . implode( "','", array_merge( $chosen_product_ids, $children_ids ) ) . "') - 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 order_items.order_item_type = 'line_item' - AND order_item_meta.meta_key = '_qty' - AND order_item_meta_2.meta_key = '_product_id' - AND order_item_meta_3.meta_key = '_line_total' - GROUP BY order_items.order_id - ORDER BY posts.post_date ASC - " ), array_merge( $chosen_product_ids, $children_ids ) ); - - $found_products = array(); - - if ( $order_items ) { - foreach ( $order_items as $order_item ) { - - if ( $order_item->line_total == 0 && $order_item->item_quantity == 0 ) - continue; - - // Get date - $date = date( 'Ym', strtotime( $order_item->post_date ) ); - - // Set values - $product_sales[ $date ] = isset( $product_sales[ $date ] ) ? $product_sales[ $date ] + $order_item->item_quantity : $order_item->item_quantity; - $product_totals[ $date ] = isset( $product_totals[ $date ] ) ? $product_totals[ $date ] + $order_item->line_total : $order_item->line_total; - - if ( $product_sales[ $date ] > $max_sales ) - $max_sales = $product_sales[ $date ]; - - if ( $product_totals[ $date ] > $max_totals ) - $max_totals = $product_totals[ $date ]; - } - } - ?> -

    - - - - - - - - - 0 ) { - foreach ( $product_sales as $date => $sales ) { - $width = ($sales>0) ? (round($sales) / round($max_sales)) * 100 : 0; - $width2 = ($product_totals[$date]>0) ? (round($product_totals[$date]) / round($max_totals)) * 100 : 0; - - $orders_link = admin_url( 'edit.php?s&post_status=all&post_type=shop_order&action=-1&s=' . urlencode( implode( ' ', $chosen_product_titles ) ) . '&m=' . date( 'Ym', strtotime( $date . '01' ) ) . '&shop_order_status=' . implode( ",", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) ); - $orders_link = apply_filters( 'woocommerce_reports_order_link', $orders_link, $chosen_product_ids, $chosen_product_titles ); - - echo ' - - '; - } - } else { - echo ''; - } - ?> - -
    ' . date_i18n( 'F', strtotime( $date . '01' ) ) . '' . esc_html( $sales ) . '' . woocommerce_price( $product_totals[ $date ] ) . ' -   -   -
    ' . __( 'No sales :(', 'woocommerce' ) . '
    - -
    -

    - -
    - product_id = isset( $_GET['product_id'] ) ? absint( $_GET['product_id'] ) : ''; + if ( isset( $_GET['product_ids'] ) && is_array( $_GET['product_ids'] ) ) + $this->product_ids = array_map( 'absint', $_GET['product_ids'] ); + elseif ( isset( $_GET['product_ids'] ) ) + $this->product_ids = array( absint( $_GET['product_ids'] ) ); } /** @@ -16,7 +21,7 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { * @return array */ public function get_chart_legend() { - if ( ! $this->product_id ) + if ( ! $this->product_ids ) return array(); $legend = array(); @@ -34,8 +39,8 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { array( 'type' => 'order_item_meta', 'meta_key' => '_product_id', - 'meta_value' => $this->product_id, - 'operator' => '=' + 'meta_value' => $this->product_ids, + 'operator' => 'IN' ) ), 'query_type' => 'get_var', @@ -54,8 +59,8 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { array( 'type' => 'order_item_meta', 'meta_key' => '_product_id', - 'meta_value' => $this->product_id, - 'operator' => '=' + 'meta_value' => $this->product_ids, + 'operator' => 'IN' ) ), 'query_type' => 'get_var', @@ -163,55 +168,177 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { * @return array */ public function get_chart_widgets() { - return array( - array( - 'title' => __( 'Top Sellers', 'woocommerce' ), - 'callback' => array( $this, 'top_seller_widget' ) - ) + + $widgets = array(); + + if ( ! empty( $this->product_ids ) ) { + $widgets[] = array( + 'title' => __( 'Showing reports for:', 'woocommerce' ), + 'callback' => array( $this, 'current_filters' ) + ); + } + + $widgets[] = array( + 'title' => '', + 'callback' => array( $this, 'products_widget' ) ); + + return $widgets; } /** - * Show the list of top sellers + * Show current filters * @return void */ - public function top_seller_widget() { - ?> - - get_order_report_data( array( - 'data' => array( - '_product_id' => array( - 'type' => 'order_item_meta', - 'order_item_type' => 'line_item', - 'function' => '', - 'name' => 'product_id' - ), - '_qty' => array( - 'type' => 'order_item_meta', - 'order_item_type' => 'line_item', - 'function' => 'SUM', - 'name' => 'order_item_qty' - ) - ), - 'order_by' => 'order_item_qty DESC', - 'group_by' => 'product_id', - 'limit' => 10, - 'query_type' => 'get_results', - 'filter_range' => true - ) ); + public function current_filters() { + $this->product_ids_titles = array(); - if ( $top_sellers ) { - foreach ( $top_sellers as $product ) { - echo ' - - - - '; + foreach ( $this->product_ids as $product_id ) { + $this->product_ids_titles[] = get_the_title( $product_id ); + } + + echo '

    ' . ' ' . implode( ', ', $this->product_ids_titles ) . '

    '; + echo '

    ' . __( 'Reset', 'woocommerce' ) . '

    '; + } + + /** + * Product selection + * @return void + */ + public function products_widget() { + ?> +

    +
    +
    +
    + + + + + + + + +
    + + +
    +

    +
    +
    ' . $product->order_item_qty . '' . get_the_title( $product->product_id ) . '' . $this->sales_sparkline( $product->product_id, 14, 'count' ) . '
    + get_order_report_data( array( + 'data' => array( + '_product_id' => array( + 'type' => 'order_item_meta', + 'order_item_type' => 'line_item', + 'function' => '', + 'name' => 'product_id' + ), + '_qty' => array( + 'type' => 'order_item_meta', + 'order_item_type' => 'line_item', + 'function' => 'SUM', + 'name' => 'order_item_qty' + ) + ), + 'order_by' => 'order_item_qty DESC', + 'group_by' => 'product_id', + 'limit' => 12, + 'query_type' => 'get_results', + 'filter_range' => true + ) ); + + if ( $top_sellers ) { + foreach ( $top_sellers as $product ) { + echo ' + + + + '; + } } - } - ?> -
    ' . $product->order_item_qty . '' . get_the_title( $product->product_id ) . '' . $this->sales_sparkline( $product->product_id, 14, 'count' ) . '
    + ?> + + +

    +
    + + 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_total' + ) + ), + 'order_by' => 'order_item_total DESC', + 'group_by' => 'product_id', + 'limit' => 12, + 'query_type' => 'get_results', + 'filter_range' => true + ) ); + + if ( $top_earners ) { + foreach ( $top_earners as $product ) { + echo ' + + + + '; + } + } + ?> +
    ' . woocommerce_price( $product->order_item_total ) . '' . get_the_title( $product->product_id ) . '' . $this->sales_sparkline( $product->product_id, 14, 'sales' ) . '
    +
    + product_id ) { + if ( ! $this->product_ids ) { ?>

    @@ -248,8 +375,8 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { array( 'type' => 'order_item_meta', 'meta_key' => '_product_id', - 'meta_value' => $this->product_id, - 'operator' => '=' + 'meta_value' => $this->product_ids, + 'operator' => 'IN' ) ), 'group_by' => $this->group_by_query, @@ -276,8 +403,8 @@ class WC_Report_Top_Sellers extends WC_Admin_Report { array( 'type' => 'order_item_meta', 'meta_key' => '_product_id', - 'meta_value' => $this->product_id, - 'operator' => '=' + 'meta_value' => $this->product_ids, + 'operator' => 'IN' ) ), 'group_by' => $this->group_by_query, diff --git a/admin/reports/class-wc-report-top-earners.php b/admin/reports/class-wc-report-top-earners.php deleted file mode 100644 index 9f81a3fddd6..00000000000 --- a/admin/reports/class-wc-report-top-earners.php +++ /dev/null @@ -1,66 +0,0 @@ - __( 'Top Earners', 'woocommerce' ), - 'callback' => array( $this, 'top_earner_widget' ) - ) - ); - } - - /** - * Show the list of top eaners - * @return void - */ - public function top_earner_widget() { - ?> - - 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_total' - ) - ), - 'order_by' => 'order_item_total DESC', - 'group_by' => 'product_id', - 'limit' => 10, - 'query_type' => 'get_results', - 'filter_range' => true - ) ); - - if ( $top_earners ) { - foreach ( $top_earners as $product ) { - echo ' - - - - '; - } - } - ?> -
    ' . woocommerce_price( $product->order_item_total ) . '' . get_the_title( $product->product_id ) . '' . $this->sales_sparkline( $product->product_id, 14, 'sales' ) . '
    -
    + $value ) + if ( is_array( $value ) ) + foreach ( $value as $v ) + echo ''; + else + echo ''; + ?> + - - -
    @@ -38,7 +45,7 @@