Reports
This commit is contained in:
parent
9523333026
commit
0afc0c02ff
|
@ -70,8 +70,8 @@ class WC_Admin_Reports {
|
|||
*/
|
||||
public function get_reports() {
|
||||
$reports = array(
|
||||
'sales' => array(
|
||||
'title' => __( 'Sales', 'woocommerce' ),
|
||||
'orders' => array(
|
||||
'title' => __( 'Orders', 'woocommerce' ),
|
||||
'reports' => array(
|
||||
"sales_by_date" => array(
|
||||
'title' => __( 'Sales by date', 'woocommerce' ),
|
||||
|
|
|
@ -947,6 +947,8 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
|
|||
}
|
||||
|
||||
delete_transient( 'woocommerce_processing_order_count' );
|
||||
|
||||
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_report_%') OR `option_name` LIKE ('_transient_timeout_wc_report_%')" );
|
||||
}
|
||||
|
||||
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order_meta', 10, 2 );
|
||||
|
|
|
@ -45,7 +45,8 @@ class WC_Admin_Report {
|
|||
'group_by' => '',
|
||||
'order_by' => '',
|
||||
'limit' => '',
|
||||
'filter_range' => false
|
||||
'filter_range' => false,
|
||||
'nocache' => false
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
@ -63,21 +64,21 @@ class WC_Admin_Report {
|
|||
if ( isset( $value['distinct'] ) )
|
||||
$distinct = 'DISTINCT';
|
||||
|
||||
if ( $value['function'] ) {
|
||||
if ( $value['type'] == 'meta' )
|
||||
$select[] = "{$value['function']}({$distinct} meta_{$key}.meta_value) as {$value['name']}";
|
||||
elseif( $value['type'] == 'post_data' )
|
||||
$select[] = "{$value['function']}({$distinct} posts.{$key}) as {$value['name']}";
|
||||
elseif( $value['type'] == 'order_item_meta' )
|
||||
$select[] = "{$value['function']}({$distinct} order_item_meta_{$key}.meta_value) as {$value['name']}";
|
||||
} else {
|
||||
if ( $value['type'] == 'meta' )
|
||||
$select[] = "{$distinct} meta_{$key}.meta_value as {$value['name']}";
|
||||
elseif( $value['type'] == 'post_data' )
|
||||
$select[] = "{$distinct} posts.{$key} as {$value['name']}";
|
||||
elseif( $value['type'] == 'order_item_meta' )
|
||||
$select[] = "{$distinct} order_item_meta_{$key}.meta_value as {$value['name']}";
|
||||
}
|
||||
if ( $value['type'] == 'meta' )
|
||||
$get_key = "meta_{$key}.meta_value";
|
||||
elseif( $value['type'] == 'post_data' )
|
||||
$get_key = "posts.{$key}";
|
||||
elseif( $value['type'] == 'order_item_meta' )
|
||||
$get_key = "order_item_meta_{$key}.meta_value";
|
||||
elseif( $value['type'] == 'order_item' )
|
||||
$get_key = "order_items.{$key}";
|
||||
|
||||
if ( $value['function'] )
|
||||
$get = "{$value['function']}({$distinct} {$get_key})";
|
||||
else
|
||||
$get = "{$distinct} {$get_key}";
|
||||
|
||||
$select[] = "{$get} as {$value['name']}";
|
||||
}
|
||||
|
||||
$query['select'] = "SELECT " . implode( ',', $select );
|
||||
|
@ -96,8 +97,12 @@ class WC_Admin_Report {
|
|||
|
||||
} elseif ( $value['type'] == 'order_item_meta' ) {
|
||||
|
||||
$joins["order_items_{$key}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items_{$key} ON posts.ID = order_items_{$key}.order_id";
|
||||
$joins["order_item_meta_{$key}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items_{$key}.order_item_id = order_item_meta_{$key}.order_item_id";
|
||||
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id";
|
||||
$joins["order_item_meta_{$key}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items.order_item_id = order_item_meta_{$key}.order_item_id";
|
||||
|
||||
} elseif ( $value['type'] == 'order_item' ) {
|
||||
|
||||
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id";
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -106,8 +111,8 @@ class WC_Admin_Report {
|
|||
foreach ( $where_meta as $value ) {
|
||||
if ( isset( $value['type'] ) && $value['type'] == 'order_item_meta' ) {
|
||||
|
||||
$joins["order_items_{$value['meta_key']}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items_{$value['meta_key']} ON posts.ID = order_items_{$value['meta_key']}.order_id";
|
||||
$joins["order_item_meta_{$value['meta_key']}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$value['meta_key']} ON order_items_{$value['meta_key']}.order_item_id = order_item_meta_{$value['meta_key']}.order_item_id";
|
||||
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id";
|
||||
$joins["order_item_meta_{$value['meta_key']}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$value['meta_key']} ON order_items.order_item_id = order_item_meta_{$value['meta_key']}.order_item_id";
|
||||
|
||||
} else {
|
||||
// If we have a where clause for meta, join the postmeta table
|
||||
|
@ -139,7 +144,7 @@ class WC_Admin_Report {
|
|||
|
||||
} elseif ( $value['type'] == 'order_item_meta' ) {
|
||||
|
||||
$query['where'] .= " AND order_items_{$key}.order_item_type = '{$value['order_item_type']}'";
|
||||
$query['where'] .= " AND order_items.order_item_type = '{$value['order_item_type']}'";
|
||||
$query['where'] .= " AND order_item_meta_{$key}.meta_key = '{$key}'";
|
||||
|
||||
}
|
||||
|
@ -194,7 +199,7 @@ class WC_Admin_Report {
|
|||
$query = implode( ' ', $query );
|
||||
$query_hash = md5( $query_type . $query );
|
||||
|
||||
if ( false === ( $result = get_transient( 'wc_report_' . $query_hash ) ) ) {
|
||||
if ( $nocache || ( false === ( $result = get_transient( 'wc_report_' . $query_hash ) ) ) ) {
|
||||
$result = apply_filters( 'woocommerce_reports_get_order_report_data', $wpdb->$query_type( $query ), $data );
|
||||
|
||||
if ( $filter_range ) {
|
||||
|
|
|
@ -40,8 +40,8 @@ class WC_Report_Sales_By_Category extends WC_Admin_Report {
|
|||
}
|
||||
}
|
||||
|
||||
if ( ! $total )
|
||||
continue;
|
||||
//if ( ! $total )
|
||||
// continue;
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s sales in %s', 'woocommerce' ), '<strong>' . woocommerce_price( $total ) . '</strong>', $category->name ),
|
||||
|
@ -300,8 +300,8 @@ class WC_Report_Sales_By_Category extends WC_Admin_Report {
|
|||
$category_chart_data[] = array( $time, $interval_total );
|
||||
}
|
||||
|
||||
if ( ! $category_total )
|
||||
continue;
|
||||
//if ( ! $category_total )
|
||||
// continue;
|
||||
|
||||
$chart_data[ $category->term_id ]['category'] = $category->name;
|
||||
$chart_data[ $category->term_id ]['data'] = $category_chart_data;
|
||||
|
|
|
@ -18,6 +18,11 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'function' => 'SUM',
|
||||
'name' => 'total_sales'
|
||||
),
|
||||
'_order_shipping' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_shipping'
|
||||
),
|
||||
'ID' => array(
|
||||
'type' => 'post_data',
|
||||
'function' => 'COUNT',
|
||||
|
@ -26,8 +31,9 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
),
|
||||
'filter_range' => true
|
||||
) );
|
||||
$total_sales = $order_totals->total_sales;
|
||||
$total_orders = absint( $order_totals->total_orders );
|
||||
$total_sales = $order_totals->total_sales;
|
||||
$total_shipping = $order_totals->total_shipping;
|
||||
$total_orders = absint( $order_totals->total_orders );
|
||||
$total_items = absint( $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_qty' => array(
|
||||
|
@ -40,6 +46,26 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'query_type' => 'get_var',
|
||||
'filter_range' => true
|
||||
) ) );
|
||||
// Get discount amounts in range
|
||||
$total_coupons = $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'discount_amount' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'coupon',
|
||||
'function' => 'SUM',
|
||||
'name' => 'discount_amount'
|
||||
)
|
||||
),
|
||||
'where' => array(
|
||||
array(
|
||||
'key' => 'order_item_type',
|
||||
'value' => 'coupon',
|
||||
'operator' => '='
|
||||
)
|
||||
),
|
||||
'query_type' => 'get_var',
|
||||
'filter_range' => true
|
||||
) );
|
||||
|
||||
$this->average_sales = $total_sales / ( $this->chart_interval + 1 );
|
||||
|
||||
|
@ -68,6 +94,14 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'title' => sprintf( __( '%s items purchased', 'woocommerce' ), '<strong>' . $total_items . '</strong>' ),
|
||||
'color' => $this->chart_colours['item_count']
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . woocommerce_price( $total_shipping ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['shipping_amount']
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s worth of coupons used', 'woocommerce' ), '<strong>' . woocommerce_price( $total_coupons ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['coupon_amount']
|
||||
);
|
||||
|
||||
return $legend;
|
||||
}
|
||||
|
@ -90,6 +124,8 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'average' => '#9bcced',
|
||||
'order_count' => '#d4d9dc',
|
||||
'item_count' => '#ecf0f1',
|
||||
'coupon_amount' => '#e67e22',
|
||||
'shipping_amount' => '#1abc9c'
|
||||
);
|
||||
|
||||
$current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : '7day';
|
||||
|
@ -173,11 +209,10 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'function' => 'SUM',
|
||||
'name' => 'total_sales'
|
||||
),
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => 'SUM',
|
||||
'name' => 'order_item_count'
|
||||
'_order_shipping' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_shipping'
|
||||
),
|
||||
'ID' => array(
|
||||
'type' => 'post_data',
|
||||
|
@ -197,16 +232,81 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'filter_range' => true
|
||||
) );
|
||||
|
||||
// Order items
|
||||
$order_items = $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => 'SUM',
|
||||
'name' => 'order_item_count'
|
||||
),
|
||||
'post_date' => array(
|
||||
'type' => 'post_data',
|
||||
'function' => '',
|
||||
'name' => 'post_date'
|
||||
),
|
||||
),
|
||||
'where' => array(
|
||||
array(
|
||||
'key' => 'order_item_type',
|
||||
'value' => 'line_item',
|
||||
'operator' => '='
|
||||
)
|
||||
),
|
||||
'group_by' => $this->group_by_query,
|
||||
'order_by' => 'post_date ASC',
|
||||
'query_type' => 'get_results',
|
||||
'filter_range' => true
|
||||
) );
|
||||
|
||||
// Get discount amounts in range
|
||||
$coupons = $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'order_item_name' => array(
|
||||
'type' => 'order_item',
|
||||
'function' => '',
|
||||
'name' => 'order_item_name'
|
||||
),
|
||||
'discount_amount' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'coupon',
|
||||
'function' => 'SUM',
|
||||
'name' => 'discount_amount'
|
||||
),
|
||||
'post_date' => array(
|
||||
'type' => 'post_data',
|
||||
'function' => '',
|
||||
'name' => 'post_date'
|
||||
),
|
||||
),
|
||||
'where' => array(
|
||||
array(
|
||||
'key' => 'order_item_type',
|
||||
'value' => 'coupon',
|
||||
'operator' => '='
|
||||
)
|
||||
),
|
||||
'group_by' => $this->group_by_query . ', order_item_name',
|
||||
'order_by' => 'post_date ASC',
|
||||
'query_type' => 'get_results',
|
||||
'filter_range' => true
|
||||
) );
|
||||
|
||||
// Prepare data for report
|
||||
$order_counts = $this->prepare_chart_data( $orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_item_counts = $this->prepare_chart_data( $orders, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_item_counts = $this->prepare_chart_data( $order_items, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$coupon_amounts = $this->prepare_chart_data( $coupons, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$shipping_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
|
||||
// Encode in json format
|
||||
$chart_data = json_encode( array(
|
||||
'order_counts' => array_values( $order_counts ),
|
||||
'order_item_counts' => array_values( $order_item_counts ),
|
||||
'order_amounts' => array_values( $order_amounts )
|
||||
'order_amounts' => array_values( $order_amounts ),
|
||||
'coupon_amounts' => array_values( $coupon_amounts ),
|
||||
'shipping_amounts' => array_values( $shipping_amounts )
|
||||
) );
|
||||
?>
|
||||
<div class="chart-container">
|
||||
|
@ -241,10 +341,30 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
yaxis: 2,
|
||||
color: '<?php echo $this->chart_colours['average']; ?>',
|
||||
points: { show: false },
|
||||
lines: { show: true, lineWidth: 1, fill: false },
|
||||
lines: { show: true, lineWidth: 2, fill: false },
|
||||
shadowSize: 0,
|
||||
hoverable: false
|
||||
},
|
||||
{
|
||||
label: "<?php echo esc_js( __( 'Coupon amount', 'woocommerce' ) ) ?>",
|
||||
data: order_data.coupon_amounts,
|
||||
yaxis: 2,
|
||||
color: '<?php echo $this->chart_colours['coupon_amount']; ?>',
|
||||
points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
|
||||
lines: { show: true, lineWidth: 4, fill: false },
|
||||
shadowSize: 0,
|
||||
prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
|
||||
},
|
||||
{
|
||||
label: "<?php echo esc_js( __( 'Shipping amount', 'woocommerce' ) ) ?>",
|
||||
data: order_data.shipping_amounts,
|
||||
yaxis: 2,
|
||||
color: '<?php echo $this->chart_colours['shipping_amount']; ?>',
|
||||
points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
|
||||
lines: { show: true, lineWidth: 4, fill: false },
|
||||
shadowSize: 0,
|
||||
prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
|
||||
},
|
||||
{
|
||||
label: "<?php echo esc_js( __( 'Sales amount', 'woocommerce' ) ) ?>",
|
||||
data: order_data.order_amounts,
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -2587,8 +2587,13 @@ img.ui-datepicker-trigger { vertical-align: middle; margin-top: -1px; cursor: po
|
|||
zoom: 1;
|
||||
}
|
||||
.postbox {
|
||||
overflow: hidden;
|
||||
zoom: 1;
|
||||
&:after {
|
||||
content: ".";
|
||||
display: block;
|
||||
height: 0;
|
||||
clear: both;
|
||||
visibility: hidden;
|
||||
}
|
||||
h3 {
|
||||
cursor: default !important;
|
||||
}
|
||||
|
@ -2735,7 +2740,7 @@ img.ui-datepicker-trigger { vertical-align: middle; margin-top: -1px; cursor: po
|
|||
background: #fff;
|
||||
color: #aaa;
|
||||
padding: 1em 1.5em;
|
||||
border-right: 4px solid #aaa;
|
||||
border-right: 5px solid #aaa;
|
||||
display: block;
|
||||
margin: 0 0 8px 0;
|
||||
strong {
|
||||
|
|
Loading…
Reference in New Issue