2013-06-26 14:11:33 +00:00
< ? php
2017-11-07 07:55:19 +00:00
/**
* Sales By Product Reporting
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin\Reporting
2017-11-07 07:55:19 +00:00
*/
2016-01-05 11:41:12 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2017-11-07 07:55:19 +00:00
exit ; // Exit if accessed directly.
2016-01-05 11:41:12 +00:00
}
2013-06-26 14:11:33 +00:00
/**
2015-11-03 13:53:50 +00:00
* WC_Report_Sales_By_Product
2014-02-14 13:02:37 +00:00
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin\Reports
2014-02-14 13:02:37 +00:00
* @ version 2.1 . 0
2013-06-26 14:11:33 +00:00
*/
2013-06-27 15:12:17 +00:00
class WC_Report_Sales_By_Product extends WC_Admin_Report {
2016-01-05 11:41:12 +00:00
/**
2016-12-19 11:28:09 +00:00
* Chart colors .
2016-01-05 11:41:12 +00:00
*
* @ var array
*/
2018-03-05 18:59:17 +00:00
public $chart_colours = array ();
2016-01-05 11:41:12 +00:00
/**
* Product ids .
*
* @ var array
*/
2018-03-05 18:59:17 +00:00
public $product_ids = array ();
2016-01-05 11:41:12 +00:00
/**
* Product ids with titles .
*
* @ var array
*/
2015-02-03 14:12:48 +00:00
public $product_ids_titles = array ();
2013-06-26 14:11:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Constructor .
2013-06-26 14:11:33 +00:00
*/
public function __construct () {
2019-01-27 23:27:30 +00:00
// @codingStandardsIgnoreStart
2014-08-31 07:19:13 +00:00
if ( isset ( $_GET [ 'product_ids' ] ) && is_array ( $_GET [ 'product_ids' ] ) ) {
2015-02-16 11:53:37 +00:00
$this -> product_ids = array_filter ( array_map ( 'absint' , $_GET [ 'product_ids' ] ) );
2014-08-31 07:19:13 +00:00
} elseif ( isset ( $_GET [ 'product_ids' ] ) ) {
2015-02-16 11:53:37 +00:00
$this -> product_ids = array_filter ( array ( absint ( $_GET [ 'product_ids' ] ) ) );
2014-08-31 07:19:13 +00:00
}
2019-01-27 23:27:30 +00:00
// @codingStandardsIgnoreEnd
2013-06-26 14:11:33 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get the legend for the main chart sidebar .
2017-11-07 07:55:19 +00:00
*
2013-06-26 14:11:33 +00:00
* @ return array
*/
public function get_chart_legend () {
2014-08-31 07:19:13 +00:00
2016-06-06 16:24:31 +00:00
if ( empty ( $this -> product_ids ) ) {
2013-06-26 14:11:33 +00:00
return array ();
2014-08-31 07:19:13 +00:00
}
2013-06-26 14:11:33 +00:00
2018-03-05 18:59:17 +00:00
$legend = array ();
2013-06-26 14:11:33 +00:00
2018-03-05 18:59:17 +00:00
$total_sales = $this -> get_order_report_data (
array (
'data' => array (
'_line_total' => array (
'type' => 'order_item_meta' ,
'order_item_type' => 'line_item' ,
'function' => 'SUM' ,
'name' => 'order_item_amount' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'where_meta' => array (
'relation' => 'OR' ,
array (
'type' => 'order_item_meta' ,
2019-03-12 14:24:09 +00:00
'meta_key' => array ( '_product_id' , '_variation_id' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $this -> product_ids , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
2018-03-05 18:59:17 +00:00
'operator' => 'IN' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'query_type' => 'get_var' ,
'filter_range' => true ,
2019-02-11 20:34:28 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2018-03-05 18:59:17 +00:00
)
);
$total_items = absint (
$this -> get_order_report_data (
2013-06-26 14:11:33 +00:00
array (
2018-03-05 18:59:17 +00:00
'data' => array (
'_qty' => array (
'type' => 'order_item_meta' ,
'order_item_type' => 'line_item' ,
'function' => 'SUM' ,
'name' => 'order_item_count' ,
),
),
'where_meta' => array (
'relation' => 'OR' ,
array (
'type' => 'order_item_meta' ,
2019-03-12 14:24:09 +00:00
'meta_key' => array ( '_product_id' , '_variation_id' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $this -> product_ids , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
2018-03-05 18:59:17 +00:00
'operator' => 'IN' ,
),
),
'query_type' => 'get_var' ,
'filter_range' => true ,
2019-02-11 20:34:28 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2013-06-26 14:11:33 +00:00
)
2018-03-05 18:59:17 +00:00
)
);
2013-06-26 14:11:33 +00:00
$legend [] = array (
2016-10-29 17:32:38 +00:00
/* translators: %s: total items sold */
2018-03-05 18:59:17 +00:00
'title' => sprintf ( __ ( '%s sales for the selected items' , 'woocommerce' ), '<strong>' . wc_price ( $total_sales ) . '</strong>' ),
'color' => $this -> chart_colours [ 'sales_amount' ],
2016-08-27 01:46:45 +00:00
'highlight_series' => 1 ,
2013-06-26 14:11:33 +00:00
);
2014-08-31 07:19:13 +00:00
2013-06-26 14:11:33 +00:00
$legend [] = array (
2016-10-29 17:32:38 +00:00
/* translators: %s: total items purchased */
2018-03-05 18:59:17 +00:00
'title' => sprintf ( __ ( '%s purchases for the selected items' , 'woocommerce' ), '<strong>' . ( $total_items ) . '</strong>' ),
'color' => $this -> chart_colours [ 'item_count' ],
2016-08-27 01:46:45 +00:00
'highlight_series' => 0 ,
2013-06-26 14:11:33 +00:00
);
return $legend ;
}
/**
2015-11-03 13:31:20 +00:00
* Output the report .
2013-06-26 14:11:33 +00:00
*/
public function output_report () {
2014-08-31 07:19:13 +00:00
2013-06-26 14:11:33 +00:00
$ranges = array (
2018-03-05 18:59:17 +00:00
'year' => __ ( 'Year' , 'woocommerce' ),
'last_month' => __ ( 'Last month' , 'woocommerce' ),
'month' => __ ( 'This month' , 'woocommerce' ),
'7day' => __ ( 'Last 7 days' , 'woocommerce' ),
2013-06-26 14:11:33 +00:00
);
2016-12-20 11:07:31 +00:00
$this -> chart_colours = array (
2013-06-26 14:11:33 +00:00
'sales_amount' => '#3498db' ,
'item_count' => '#d4d9dc' ,
);
2019-12-13 19:58:14 +00:00
$current_range = ! empty ( $_GET [ 'range' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'range' ] ) ) : '7day' ; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
2013-06-26 14:11:33 +00:00
2019-01-27 23:27:30 +00:00
if ( ! in_array ( $current_range , array ( 'custom' , 'year' , 'last_month' , 'month' , '7day' ), true ) ) {
2013-07-30 10:12:42 +00:00
$current_range = '7day' ;
2017-04-18 23:33:25 +00:00
}
2013-06-26 14:11:33 +00:00
2017-04-18 23:33:25 +00:00
$this -> check_current_range_nonce ( $current_range );
2013-07-30 10:12:42 +00:00
$this -> calculate_current_range ( $current_range );
2013-06-26 14:11:33 +00:00
2018-03-05 18:59:17 +00:00
include WC () -> plugin_path () . '/includes/admin/views/html-report-by-date.php' ;
2013-06-26 14:11:33 +00:00
}
/**
2016-01-05 11:41:12 +00:00
* Get chart widgets .
2014-08-31 07:19:13 +00:00
*
2013-06-26 14:11:33 +00:00
* @ return array
*/
public function get_chart_widgets () {
2013-06-27 15:12:17 +00:00
$widgets = array ();
if ( ! empty ( $this -> product_ids ) ) {
$widgets [] = array (
'title' => __ ( 'Showing reports for:' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
'callback' => array ( $this , 'current_filters' ),
2013-06-27 15:12:17 +00:00
);
}
$widgets [] = array (
'title' => '' ,
2016-08-27 01:46:45 +00:00
'callback' => array ( $this , 'products_widget' ),
2013-06-26 14:11:33 +00:00
);
2013-06-27 15:12:17 +00:00
return $widgets ;
2013-06-26 14:11:33 +00:00
}
/**
2016-01-05 11:41:12 +00:00
* Output current filters .
2013-06-26 14:11:33 +00:00
*/
2013-06-27 15:12:17 +00:00
public function current_filters () {
2014-08-31 07:19:13 +00:00
2013-06-27 15:12:17 +00:00
$this -> product_ids_titles = array ();
foreach ( $this -> product_ids as $product_id ) {
2014-08-31 07:19:13 +00:00
2014-08-19 10:09:29 +00:00
$product = wc_get_product ( $product_id );
2014-08-31 07:19:13 +00:00
2014-02-25 14:48:55 +00:00
if ( $product ) {
$this -> product_ids_titles [] = $product -> get_formatted_name ();
} else {
$this -> product_ids_titles [] = '#' . $product_id ;
}
2013-06-27 15:12:17 +00:00
}
2018-01-03 18:09:53 +00:00
echo '<p><strong>' . wp_kses_post ( implode ( ', ' , $this -> product_ids_titles ) ) . '</strong></p>' ;
2017-11-07 07:55:19 +00:00
echo '<p><a class="button" href="' . esc_url ( remove_query_arg ( 'product_ids' ) ) . '">' . esc_html__ ( 'Reset' , 'woocommerce' ) . '</a></p>' ;
2013-06-27 15:12:17 +00:00
}
/**
2016-01-05 11:41:12 +00:00
* Output products widget .
2013-06-27 15:12:17 +00:00
*/
public function products_widget () {
2013-06-26 14:11:33 +00:00
?>
2017-11-07 07:55:19 +00:00
< h4 class = " section_title " >< span >< ? php esc_html_e ( 'Product search' , 'woocommerce' ); ?> </span></h4>
2013-06-27 15:12:17 +00:00
< div class = " section " >
< form method = " GET " >
< div >
2017-11-07 07:55:19 +00:00
< ? php // @codingStandardsIgnoreStart ?>
2016-12-21 13:23:26 +00:00
< select class = " wc-product-search " style = " width:203px; " multiple = " multiple " id = " product_ids " name = " product_ids[] " data - placeholder = " <?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?> " data - action = " woocommerce_json_search_products_and_variations " ></ select >
2017-11-07 07:56:31 +00:00
< button type = " submit " class = " submit button " value = " <?php esc_attr_e( 'Show', 'woocommerce' ); ?> " >< ? php esc_html_e ( 'Show' , 'woocommerce' ); ?> </button>
2017-03-07 20:24:24 +00:00
< input type = " hidden " name = " range " value = " <?php echo ( ! empty( $_GET['range'] ) ) ? esc_attr( $_GET['range'] ) : ''; ?> " />
< input type = " hidden " name = " start_date " value = " <?php echo ( ! empty( $_GET['start_date'] ) ) ? esc_attr( $_GET['start_date'] ) : ''; ?> " />
< input type = " hidden " name = " end_date " value = " <?php echo ( ! empty( $_GET['end_date'] ) ) ? esc_attr( $_GET['end_date'] ) : ''; ?> " />
< input type = " hidden " name = " page " value = " <?php echo ( ! empty( $_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : ''; ?> " />
< input type = " hidden " name = " tab " value = " <?php echo ( ! empty( $_GET['tab'] ) ) ? esc_attr( $_GET['tab'] ) : ''; ?> " />
< input type = " hidden " name = " report " value = " <?php echo ( ! empty( $_GET['report'] ) ) ? esc_attr( $_GET['report'] ) : ''; ?> " />
2017-06-16 10:51:00 +00:00
< ? php wp_nonce_field ( 'custom_range' , 'wc_reports_nonce' , false ); ?>
2017-11-07 07:55:19 +00:00
< ? php // @codingStandardsIgnoreEnd ?>
2013-06-27 15:12:17 +00:00
</ div >
</ form >
</ div >
2017-11-07 07:55:19 +00:00
< h4 class = " section_title " >< span >< ? php esc_html_e ( 'Top sellers' , 'woocommerce' ); ?> </span></h4>
2013-06-27 15:12:17 +00:00
< div class = " section " >
< table cellspacing = " 0 " >
< ? php
2018-03-05 18:59:17 +00:00
$top_sellers = $this -> 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' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'order_by' => 'order_item_qty DESC' ,
'group_by' => 'product_id' ,
'limit' => 12 ,
'query_type' => 'get_results' ,
'filter_range' => true ,
2019-01-27 22:30:38 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2018-03-05 18:59:17 +00:00
)
);
2013-06-26 14:11:33 +00:00
2013-06-27 15:12:17 +00:00
if ( $top_sellers ) {
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreStart
2013-06-27 15:12:17 +00:00
foreach ( $top_sellers as $product ) {
echo '<tr class="' . ( in_array ( $product -> product_id , $this -> product_ids ) ? 'active' : '' ) . ' " >
2017-11-07 07:55:19 +00:00
< td class = " count " > ' . esc_html( $product->order_item_qty ) . ' </ td >
2017-08-15 15:56:35 +00:00
< td class = " name " >< a href = " ' . esc_url( add_query_arg( 'product_ids', $product->product_id ) ) . ' " > ' . esc_html( get_the_title( $product->product_id ) ) . ' </ a ></ td >
2014-11-27 10:36:21 +00:00
< td class = " sparkline " > ' . $this->sales_sparkline( $product->product_id, 7, ' count ' ) . ' </ td >
</ tr > ' ;
}
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreEnd
2014-11-27 10:36:21 +00:00
} else {
2017-11-07 07:55:19 +00:00
echo '<tr><td colspan="3">' . esc_html__ ( 'No products found in range' , 'woocommerce' ) . '</td></tr>' ;
2014-11-27 10:36:21 +00:00
}
?>
</ table >
</ div >
2017-11-07 07:55:19 +00:00
< h4 class = " section_title " >< span >< ? php esc_html_e ( 'Top freebies' , 'woocommerce' ); ?> </span></h4>
2014-11-27 10:36:21 +00:00
< div class = " section " >
< table cellspacing = " 0 " >
< ? php
2018-03-05 18:59:17 +00:00
$top_freebies = $this -> 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' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'where_meta' => array (
array (
'type' => 'order_item_meta' ,
2019-03-12 14:24:09 +00:00
'meta_key' => '_line_subtotal' , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => '0' , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
2018-03-05 18:59:17 +00:00
'operator' => '=' ,
),
2016-08-27 02:08:49 +00:00
),
2018-03-05 18:59:17 +00:00
'order_by' => 'order_item_qty DESC' ,
'group_by' => 'product_id' ,
'limit' => 12 ,
'query_type' => 'get_results' ,
'filter_range' => true ,
)
);
2014-11-27 10:36:21 +00:00
if ( $top_freebies ) {
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreStart
2014-11-27 10:36:21 +00:00
foreach ( $top_freebies as $product ) {
echo '<tr class="' . ( in_array ( $product -> product_id , $this -> product_ids ) ? 'active' : '' ) . ' " >
2017-11-07 07:55:19 +00:00
< td class = " count " > ' . esc_html( $product->order_item_qty ) . ' </ td >
2017-08-15 15:56:35 +00:00
< td class = " name " >< a href = " ' . esc_url( add_query_arg( 'product_ids', $product->product_id ) ) . ' " > ' . esc_html( get_the_title( $product->product_id ) ) . ' </ a ></ td >
2013-07-25 14:00:23 +00:00
< td class = " sparkline " > ' . $this->sales_sparkline( $product->product_id, 7, ' count ' ) . ' </ td >
2013-06-27 15:12:17 +00:00
</ tr > ' ;
}
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreEnd
2013-07-08 15:36:09 +00:00
} else {
2017-11-07 07:55:19 +00:00
echo '<tr><td colspan="3">' . esc_html__ ( 'No products found in range' , 'woocommerce' ) . '</td></tr>' ;
2013-06-26 14:11:33 +00:00
}
2013-06-27 15:12:17 +00:00
?>
</ table >
</ div >
2017-11-07 07:55:19 +00:00
< h4 class = " section_title " >< span >< ? php esc_html_e ( 'Top earners' , 'woocommerce' ); ?> </span></h4>
2013-06-27 15:12:17 +00:00
< div class = " section " >
< table cellspacing = " 0 " >
< ? php
2018-03-05 18:59:17 +00:00
$top_earners = $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_total' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'order_by' => 'order_item_total DESC' ,
'group_by' => 'product_id' ,
'limit' => 12 ,
'query_type' => 'get_results' ,
'filter_range' => true ,
2019-01-27 22:30:38 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2018-03-05 18:59:17 +00:00
)
);
2013-06-27 15:12:17 +00:00
if ( $top_earners ) {
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreStart
2013-06-27 15:12:17 +00:00
foreach ( $top_earners as $product ) {
echo '<tr class="' . ( in_array ( $product -> product_id , $this -> product_ids ) ? 'active' : '' ) . ' " >
2013-11-25 13:34:21 +00:00
< td class = " count " > ' . wc_price( $product->order_item_total ) . ' </ td >
2017-08-15 15:56:35 +00:00
< td class = " name " >< a href = " ' . esc_url( add_query_arg( 'product_ids', $product->product_id ) ) . ' " > ' . esc_html( get_the_title( $product->product_id ) ) . ' </ a ></ td >
2013-07-25 14:00:23 +00:00
< td class = " sparkline " > ' . $this->sales_sparkline( $product->product_id, 7, ' sales ' ) . ' </ td >
2013-06-27 15:12:17 +00:00
</ tr > ' ;
}
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreEnd
2013-07-08 15:36:09 +00:00
} else {
2017-11-07 07:55:19 +00:00
echo '<tr><td colspan="3">' . esc_html__ ( 'No products found in range' , 'woocommerce' ) . '</td></tr>' ;
2013-06-27 15:12:17 +00:00
}
?>
</ table >
</ div >
< script type = " text/javascript " >
2020-12-05 14:11:56 +00:00
jQuery ( '.section_title' ) . on ( 'click' , function () {
var next_section = jQuery ( this ) . next ( '.section' );
2013-06-27 15:12:17 +00:00
2020-12-05 14:11:56 +00:00
if ( jQuery ( next_section ) . is ( ':visible' ) ) {
2013-06-27 15:12:17 +00:00
return false ;
2020-12-05 14:11:56 +00:00
}
2013-06-27 15:12:17 +00:00
2020-12-05 14:11:56 +00:00
jQuery ( '.section:visible' ) . slideUp ();
jQuery ( '.section_title' ) . removeClass ( 'open' );
jQuery ( this ) . addClass ( 'open' ) . next ( '.section' ) . slideDown ();
2013-06-27 15:12:17 +00:00
return false ;
2020-12-05 14:11:56 +00:00
} );
jQuery ( '.section' ) . slideUp ( 100 , function () {
2013-06-27 15:12:17 +00:00
< ? php if ( empty ( $this -> product_ids ) ) : ?>
2021-02-04 20:31:28 +00:00
jQuery ( '.section_title:eq(1)' ) . trigger ( 'click' );
2013-06-27 15:12:17 +00:00
< ? php endif ; ?>
2020-12-05 14:11:56 +00:00
} );
2013-06-27 15:12:17 +00:00
</ script >
2013-06-26 14:11:33 +00:00
< ? php
}
2013-07-18 11:56:12 +00:00
/**
2015-11-03 13:31:20 +00:00
* Output an export link .
2013-07-18 11:56:12 +00:00
*/
public function get_export_button () {
2014-08-31 07:19:13 +00:00
2019-12-13 19:58:14 +00:00
$current_range = ! empty ( $_GET [ 'range' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'range' ] ) ) : '7day' ; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
2013-07-18 11:56:12 +00:00
?>
< a
href = " # "
2017-11-07 07:55:19 +00:00
download = " report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_html( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv "
2013-07-18 11:56:12 +00:00
class = " export_csv "
data - export = " chart "
2015-08-05 18:08:15 +00:00
data - xaxes = " <?php esc_attr_e( 'Date', 'woocommerce' ); ?> "
2017-11-07 07:55:19 +00:00
data - groupby = " <?php echo $this->chart_groupby ; ?> " < ? php // @codingStandardsIgnoreLine ?>
2013-07-18 11:56:12 +00:00
>
2017-11-07 07:55:19 +00:00
< ? php esc_html_e ( 'Export CSV' , 'woocommerce' ); ?>
2013-07-18 11:56:12 +00:00
</ a >
< ? php
}
2013-06-26 14:11:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get the main chart .
2013-06-26 14:11:33 +00:00
*/
public function get_main_chart () {
global $wp_locale ;
2016-06-06 16:24:31 +00:00
if ( empty ( $this -> product_ids ) ) {
2013-06-26 14:11:33 +00:00
?>
< div class = " chart-container " >
2017-11-07 07:55:19 +00:00
< p class = " chart-prompt " >< ? php esc_html_e ( 'Choose a product to view stats' , 'woocommerce' ); ?> </p>
2013-06-26 14:11:33 +00:00
</ div >
< ? php
} else {
2017-11-07 07:55:19 +00:00
// Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date.
2018-03-05 18:59:17 +00:00
$order_item_counts = $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' ,
),
'_product_id' => array (
'type' => 'order_item_meta' ,
'order_item_type' => 'line_item' ,
'function' => '' ,
'name' => 'product_id' ,
),
2016-08-27 01:46:45 +00:00
),
2018-03-05 18:59:17 +00:00
'where_meta' => array (
'relation' => 'OR' ,
array (
'type' => 'order_item_meta' ,
2019-03-12 14:24:09 +00:00
'meta_key' => array ( '_product_id' , '_variation_id' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $this -> product_ids , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
2018-03-05 18:59:17 +00:00
'operator' => 'IN' ,
),
2014-02-24 17:10:04 +00:00
),
2018-03-05 18:59:17 +00:00
'group_by' => 'product_id,' . $this -> group_by_query ,
'order_by' => 'post_date ASC' ,
'query_type' => 'get_results' ,
'filter_range' => true ,
2019-02-11 20:34:28 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2018-03-05 18:59:17 +00:00
)
);
2013-06-26 14:11:33 +00:00
2018-03-05 18:59:17 +00:00
$order_item_amounts = $this -> get_order_report_data (
array (
'data' => array (
'_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' ,
),
'_product_id' => array (
'type' => 'order_item_meta' ,
'order_item_type' => 'line_item' ,
'function' => '' ,
'name' => 'product_id' ,
),
2013-07-25 14:00:23 +00:00
),
2018-03-05 18:59:17 +00:00
'where_meta' => array (
'relation' => 'OR' ,
array (
'type' => 'order_item_meta' ,
2019-03-12 14:24:09 +00:00
'meta_key' => array ( '_product_id' , '_variation_id' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $this -> product_ids , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
2018-03-05 18:59:17 +00:00
'operator' => 'IN' ,
),
2014-02-24 17:10:04 +00:00
),
2018-03-05 18:59:17 +00:00
'group_by' => 'product_id, ' . $this -> group_by_query ,
'order_by' => 'post_date ASC' ,
'query_type' => 'get_results' ,
'filter_range' => true ,
2019-02-11 20:34:28 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
2018-03-05 18:59:17 +00:00
)
);
2013-06-26 14:11:33 +00:00
2017-11-07 07:55:19 +00:00
// Prepare data for report.
2013-06-26 14:11:33 +00:00
$order_item_counts = $this -> prepare_chart_data ( $order_item_counts , 'post_date' , 'order_item_count' , $this -> chart_interval , $this -> start_date , $this -> chart_groupby );
$order_item_amounts = $this -> prepare_chart_data ( $order_item_amounts , 'post_date' , 'order_item_amount' , $this -> chart_interval , $this -> start_date , $this -> chart_groupby );
2017-11-07 07:55:19 +00:00
// Encode in json format.
2019-01-27 23:27:30 +00:00
$chart_data = wp_json_encode (
2018-03-05 18:59:17 +00:00
array (
'order_item_counts' => array_values ( $order_item_counts ),
'order_item_amounts' => array_values ( $order_item_amounts ),
)
);
2013-06-26 14:11:33 +00:00
?>
< div class = " chart-container " >
< div class = " chart-placeholder main " ></ div >
</ div >
2017-11-07 07:55:19 +00:00
< ? php // @codingStandardsIgnoreStart ?>
2013-06-26 14:11:33 +00:00
< script type = " text/javascript " >
2013-07-18 11:56:12 +00:00
var main_chart ;
2013-06-26 14:11:33 +00:00
jQuery ( function (){
2019-02-20 12:00:47 +00:00
var order_data = JSON . parse ( decodeURIComponent ( '<?php echo rawurlencode( $chart_data ); ?>' ) );
2013-06-26 14:11:33 +00:00
2013-07-18 08:20:49 +00:00
var drawGraph = function ( highlight ) {
var series = [
2013-06-26 14:11:33 +00:00
{
label : " <?php echo esc_js( __( 'Number of items sold', 'woocommerce' ) ) ?> " ,
data : order_data . order_item_counts ,
2016-12-20 11:07:31 +00:00
color : '<?php echo $this->chart_colours[' item_count ']; ?>' ,
bars : { fillColor : '<?php echo $this->chart_colours[' item_count ']; ?>' , fill : true , show : true , lineWidth : 0 , barWidth : < ? php echo $this -> barwidth ; ?> * 0.5, align: 'center' },
2013-06-26 14:11:33 +00:00
shadowSize : 0 ,
hoverable : false
},
{
label : " <?php echo esc_js( __( 'Sales amount', 'woocommerce' ) ) ?> " ,
data : order_data . order_item_amounts ,
yaxis : 2 ,
2016-12-20 11:07:31 +00:00
color : '<?php echo $this->chart_colours[' sales_amount ']; ?>' ,
2013-06-26 14:11:33 +00:00
points : { show : true , radius : 5 , lineWidth : 3 , fillColor : '#fff' , fill : true },
lines : { show : true , lineWidth : 4 , fill : false },
shadowSize : 0 ,
2014-09-19 07:23:58 +00:00
< ? php echo $this -> get_currency_tooltip (); ?>
2013-06-26 14:11:33 +00:00
}
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 )
highlight_series . bars . fillColor = '#9c5d90' ;
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
},
2014-08-31 07:19:13 +00:00
grid : {
color : '#aaa' ,
borderColor : 'transparent' ,
borderWidth : 0 ,
hoverable : true
},
xaxes : [ {
color : '#aaa' ,
position : " bottom " ,
tickColor : 'transparent' ,
2013-07-18 08:20:49 +00:00
mode : " time " ,
2016-09-09 00:14:28 +00:00
timeformat : " <?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?> " ,
2019-02-20 12:00:47 +00:00
monthNames : JSON . parse ( decodeURIComponent ( '<?php echo rawurlencode( wp_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 ; ?> " ],
font : {
2014-08-31 07:19:13 +00:00
color : " #aaa "
}
2013-07-18 08:20:49 +00:00
} ],
2014-08-31 07:19:13 +00:00
yaxes : [
{
min : 0 ,
minTickSize : 1 ,
tickDecimals : 0 ,
color : '#ecf0f1' ,
font : { color : " #aaa " }
},
{
position : " right " ,
min : 0 ,
tickDecimals : 2 ,
alignTicksWithAxis : 1 ,
color : 'transparent' ,
font : { color : " #aaa " }
}
],
}
);
2021-02-04 20:31:28 +00:00
jQuery ( '.chart-placeholder' ) . trigger ( 'resize' );
2014-08-31 07:19:13 +00:00
}
2013-07-18 08:20:49 +00:00
drawGraph ();
2021-02-18 14:35:24 +00:00
jQuery ( '.highlight_series' ) . on ( 'mouseenter' ,
2013-07-18 08:20:49 +00:00
function () {
drawGraph ( jQuery ( this ) . data ( 'series' ) );
2021-02-18 14:35:24 +00:00
} ) . on ( 'mouseleave' ,
2013-07-18 08:20:49 +00:00
function () {
drawGraph ();
}
);
2013-06-26 14:11:33 +00:00
});
</ script >
< ? php
2017-11-07 07:55:19 +00:00
// @codingStandardsIgnoreEnd
2013-06-26 14:11:33 +00:00
}
}
2014-08-31 07:19:13 +00:00
}