woocommerce/includes/admin/reports/class-wc-report-sales-by-ca...

439 lines
12 KiB
PHP
Raw Normal View History

2013-06-27 17:39:55 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-06-27 17:39:55 +00:00
/**
2015-11-03 13:53:50 +00:00
* WC_Report_Sales_By_Category
2014-02-14 13:02:37 +00:00
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/Reports
2014-02-14 13:02:37 +00:00
* @version 2.1.0
2013-06-27 17:39:55 +00:00
*/
class WC_Report_Sales_By_Category extends WC_Admin_Report {
/**
* Chart colours.
*
* @var array
*/
public $chart_colours = array();
/**
* Categories ids.
*
* @var array
*/
public $show_categories = array();
/**
* Item sales.
*
* @var array
*/
private $item_sales = array();
/**
* Item sales and times.
*
* @var array
*/
private $item_sales_and_times = array();
2013-06-27 17:39:55 +00:00
/**
* Constructor.
2013-06-27 17:39:55 +00:00
*/
public function __construct() {
if ( isset( $_GET['show_categories'] ) ) {
$this->show_categories = is_array( $_GET['show_categories'] ) ? array_map( 'absint', $_GET['show_categories'] ) : array( absint( $_GET['show_categories'] ) );
}
2013-06-27 17:39:55 +00:00
}
2014-04-25 09:43:26 +00:00
/**
* Get all product ids in a category (and its children).
*
2014-04-25 09:43:26 +00:00
* @param int $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 ) );
}
2013-06-27 17:39:55 +00:00
/**
* Get the legend for the main chart sidebar.
*
2013-06-27 17:39:55 +00:00
* @return array
*/
public function get_chart_legend() {
2016-06-06 16:24:31 +00:00
if ( empty( $this->show_categories ) ) {
2013-06-27 17:39:55 +00:00
return array();
}
2013-06-27 17:39:55 +00:00
2014-04-25 09:43:26 +00:00
$legend = array();
$index = 0;
2013-06-27 17:39:55 +00:00
foreach ( $this->show_categories as $category ) {
2014-04-25 09:43:26 +00:00
$category = get_term( $category, 'product_cat' );
$total = 0;
$product_ids = $this->get_products_in_category( $category->term_id );
2013-06-27 17:39:55 +00:00
foreach ( $product_ids as $id ) {
2013-06-27 20:34:18 +00:00
if ( isset( $this->item_sales[ $id ] ) ) {
2013-06-27 17:39:55 +00:00
$total += $this->item_sales[ $id ];
}
}
2013-06-27 20:34:18 +00:00
$legend[] = array(
2014-04-25 09:43:26 +00:00
'title' => sprintf( __( '%s sales in %s', 'woocommerce' ), '<strong>' . wc_price( $total ) . '</strong>', $category->name ),
'color' => isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[ 0 ],
2013-07-18 08:20:49 +00:00
'highlight_series' => $index
2013-06-27 20:34:18 +00:00
);
$index++;
}
2013-06-27 17:39:55 +00:00
return $legend;
}
/**
* Output the report.
2013-06-27 17:39:55 +00:00
*/
public function output_report() {
2013-06-27 17:39:55 +00:00
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last Month', 'woocommerce' ),
'month' => __( 'This Month', 'woocommerce' ),
'7day' => __( 'Last 7 Days', 'woocommerce' )
);
2013-07-18 08:20:49 +00:00
$this->chart_colours = array( '#3498db', '#34495e', '#1abc9c', '#2ecc71', '#f1c40f', '#e67e22', '#e74c3c', '#2980b9', '#8e44ad', '#2c3e50', '#16a085', '#27ae60', '#f39c12', '#d35400', '#c0392b' );
2013-06-27 17:39:55 +00:00
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
2013-06-27 17:39:55 +00:00
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
$current_range = '7day';
}
2013-06-27 17:39:55 +00:00
$this->calculate_current_range( $current_range );
2013-06-27 20:34:18 +00:00
2013-06-27 17:39:55 +00:00
// Get item sales data
2016-06-06 16:24:31 +00:00
if ( ! empty( $this->show_categories ) ) {
2013-06-27 17:39:55 +00:00
$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'
2013-06-27 20:34:18 +00:00
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
2013-06-27 17:39:55 +00:00
),
'group_by' => 'ID, product_id, post_date',
2013-06-27 17:39:55 +00:00
'query_type' => 'get_results',
'filter_range' => true
) );
$this->item_sales = array();
2013-06-27 20:34:18 +00:00
$this->item_sales_and_times = array();
2013-06-27 17:39:55 +00:00
if ( is_array( $order_items ) ) {
2013-06-27 17:39:55 +00:00
foreach ( $order_items as $order_item ) {
2013-06-27 20:34:18 +00:00
switch ( $this->chart_groupby ) {
case 'day' :
$time = strtotime( date( 'Ymd', strtotime( $order_item->post_date ) ) ) * 1000;
break;
case 'month' :
default :
2013-06-27 20:34:18 +00:00
$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;
2013-06-27 17:39:55 +00:00
$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' );
2013-06-27 17:39:55 +00:00
}
/**
* Get chart widgets.
*
2013-06-27 17:39:55 +00:00
* @return array
*/
public function get_chart_widgets() {
2013-06-27 17:39:55 +00:00
return array(
array(
'title' => __( 'Categories', 'woocommerce' ),
'callback' => array( $this, 'category_widget' )
)
);
}
/**
* Output category widget.
2013-06-27 17:39:55 +00:00
*/
public function category_widget() {
2013-06-27 17:39:55 +00:00
$categories = get_terms( 'product_cat', array( 'orderby' => 'name' ) );
?>
<form method="GET">
<div>
<select multiple="multiple" data-placeholder="<?php esc_attr_e( 'Select categories&hellip;', 'woocommerce' ); ?>" class="wc-enhanced-select" id="show_categories" name="show_categories[]" style="width: 205px;">
2013-06-27 17:39:55 +00:00
<?php
$r = array();
$r['pad_counts'] = 1;
$r['hierarchical'] = 1;
$r['hide_empty'] = 1;
$r['value'] = 'id';
$r['selected'] = $this->show_categories;
include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php' );
echo wc_walk_category_dropdown_tree( $categories, 0, $r );
2013-06-27 17:39:55 +00:00
?>
</select>
<a href="#" class="select_none"><?php _e( 'None', 'woocommerce' ); ?></a>
<a href="#" class="select_all"><?php _e( 'All', 'woocommerce' ); ?></a>
<input type="submit" class="submit button" value="<?php esc_attr_e( 'Show', 'woocommerce' ); ?>" />
2013-06-27 17:39:55 +00:00
<input type="hidden" name="range" value="<?php if ( ! empty( $_GET['range'] ) ) echo esc_attr( $_GET['range'] ) ?>" />
<input type="hidden" name="start_date" value="<?php if ( ! empty( $_GET['start_date'] ) ) echo esc_attr( $_GET['start_date'] ) ?>" />
<input type="hidden" name="end_date" value="<?php if ( ! empty( $_GET['end_date'] ) ) echo esc_attr( $_GET['end_date'] ) ?>" />
<input type="hidden" name="page" value="<?php if ( ! empty( $_GET['page'] ) ) echo esc_attr( $_GET['page'] ) ?>" />
<input type="hidden" name="tab" value="<?php if ( ! empty( $_GET['tab'] ) ) echo esc_attr( $_GET['tab'] ) ?>" />
<input type="hidden" name="report" value="<?php if ( ! empty( $_GET['report'] ) ) echo esc_attr( $_GET['report'] ) ?>" />
</div>
<script type="text/javascript">
jQuery(function(){
// Select all/none
jQuery( '.chart-widget' ).on( 'click', '.select_all', function() {
jQuery(this).closest( 'div' ).find( 'select option' ).attr( 'selected', 'selected' );
2015-01-09 16:43:35 +00:00
jQuery(this).closest( 'div' ).find('select').change();
2013-06-27 17:39:55 +00:00
return false;
});
jQuery( '.chart-widget').on( 'click', '.select_none', function() {
jQuery(this).closest( 'div' ).find( 'select option' ).removeAttr( 'selected' );
2015-01-09 16:43:35 +00:00
jQuery(this).closest( 'div' ).find('select').change();
2013-06-27 17:39:55 +00:00
return false;
});
});
</script>
</form>
<?php
}
2013-07-18 11:56:12 +00:00
/**
* Output an export link.
2013-07-18 11:56:12 +00:00
*/
public function get_export_button() {
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
2013-07-18 11:56:12 +00:00
?>
<a
href="#"
download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time('timestamp') ); ?>.csv"
2013-07-18 11:56:12 +00:00
class="export_csv"
data-export="chart"
data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>"
2013-07-18 11:56:12 +00:00
data-groupby="<?php echo $this->chart_groupby; ?>"
>
<?php _e( 'Export CSV', 'woocommerce' ); ?>
</a>
<?php
}
2013-06-27 17:39:55 +00:00
/**
* Get the main chart.
*
2013-06-27 17:39:55 +00:00
* @return string
*/
public function get_main_chart() {
global $wp_locale;
2016-06-06 16:24:31 +00:00
if ( empty( $this->show_categories ) ) {
2013-06-27 17:39:55 +00:00
?>
<div class="chart-container">
<p class="chart-prompt"><?php _e( '&larr; Choose a category to view stats', 'woocommerce' ); ?></p>
</div>
<?php
} else {
2015-02-03 14:12:04 +00:00
$chart_data = array();
$index = 0;
2013-06-27 17:39:55 +00:00
foreach ( $this->show_categories as $category ) {
2014-04-25 09:43:26 +00:00
$category = get_term( $category, 'product_cat' );
$product_ids = $this->get_products_in_category( $category->term_id );
2013-06-27 20:34:18 +00:00
$category_chart_data = array();
for ( $i = 0; $i <= $this->chart_interval; $i ++ ) {
2013-06-27 20:34:18 +00:00
$interval_total = 0;
switch ( $this->chart_groupby ) {
case 'day' :
$time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $this->start_date ) ) ) * 1000;
break;
case 'month' :
default :
2013-06-27 20:34:18 +00:00
$time = strtotime( date( 'Ym', strtotime( "+{$i} MONTH", $this->start_date ) ) . '01' ) * 1000;
break;
}
2013-06-27 17:39:55 +00:00
2013-06-27 20:34:18 +00:00
foreach ( $product_ids as $id ) {
2013-06-27 20:34:18 +00:00
if ( isset( $this->item_sales_and_times[ $time ][ $id ] ) ) {
$interval_total += $this->item_sales_and_times[ $time ][ $id ];
}
2013-06-27 17:39:55 +00:00
}
2013-06-27 20:34:18 +00:00
$category_chart_data[] = array( $time, (float) wc_format_decimal( $interval_total, wc_get_price_decimals() ) );
2013-06-27 17:39:55 +00:00
}
2013-06-27 20:34:18 +00:00
$chart_data[ $category->term_id ]['category'] = $category->name;
$chart_data[ $category->term_id ]['data'] = $category_chart_data;
2013-06-27 17:39:55 +00:00
2016-03-04 10:53:48 +00:00
$index++;
2013-06-27 17:39:55 +00:00
}
?>
<div class="chart-container">
<div class="chart-placeholder main"></div>
</div>
<script type="text/javascript">
2013-07-18 11:56:12 +00:00
var main_chart;
2013-06-27 17:39:55 +00:00
jQuery(function(){
2013-07-18 08:20:49 +00:00
var drawGraph = function( highlight ) {
var series = [
2013-06-27 20:34:18 +00:00
<?php
$index = 0;
foreach ( $chart_data as $data ) {
$color = isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[0];
$width = $this->barwidth / sizeof( $chart_data );
$offset = ( $width * $index );
$series = $data['data'];
foreach ( $series as $key => $series_data ) {
2013-06-27 20:34:18 +00:00
$series[ $key ][0] = $series_data[0] + $offset;
}
2013-06-27 20:34:18 +00:00
echo '{
label: "' . esc_js( $data['category'] ) . '",
data: jQuery.parseJSON( "' . json_encode( $series ) . '" ),
color: "' . $color . '",
bars: {
fillColor: "' . $color . '",
fill: true,
show: true,
lineWidth: 1,
align: "center",
barWidth: ' . $width * 0.75 . ',
stack: false
},
' . $this->get_currency_tooltip() . ',
2013-06-27 20:34:18 +00:00
enable_tooltip: true,
prepend_label: true
},';
$index++;
}
?>
2013-07-18 08:20:49 +00:00
];
if ( highlight !== 'undefined' && series[ highlight ] ) {
highlight_series = series[ highlight ];
highlight_series.color = '#9c5d90';
if ( highlight_series.bars ) {
2013-07-18 08:20:49 +00:00
highlight_series.bars.fillColor = '#9c5d90';
}
2013-07-18 08:20:49 +00:00
if ( highlight_series.lines ) {
highlight_series.lines.lineWidth = 5;
}
}
2013-07-18 11:56:12 +00:00
main_chart = jQuery.plot(
2013-07-18 08:20:49 +00:00
jQuery('.chart-placeholder.main'),
series,
{
legend: {
show: false
},
grid: {
color: '#aaa',
borderColor: 'transparent',
borderWidth: 0,
hoverable: true
},
xaxes: [ {
color: '#aaa',
reserveSpace: true,
position: "bottom",
tickColor: 'transparent',
2013-07-18 08:20:49 +00:00
mode: "time",
timeformat: "<?php if ( $this->chart_groupby == 'day' ) echo '%d %b'; else echo '%b'; ?>",
monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ); ?>,
2013-07-18 08:20:49 +00:00
tickLength: 1,
minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],
tickSize: [1, "<?php echo $this->chart_groupby; ?>"],
font: {
color: "#aaa"
}
2013-07-18 08:20:49 +00:00
} ],
yaxes: [
{
min: 0,
tickDecimals: 2,
color: 'transparent',
font: { color: "#aaa" }
}
],
}
);
jQuery('.chart-placeholder').resize();
}
2013-07-18 08:20:49 +00:00
drawGraph();
jQuery('.highlight_series').hover(
function() {
drawGraph( jQuery(this).data('series') );
},
function() {
drawGraph();
}
);
2013-06-27 17:39:55 +00:00
});
</script>
<?php
}
}
}