class-wc-admin-report.php
1 year ago
class-wc-report-coupon-usage.php
5 years ago
class-wc-report-customer-list.php
9 months ago
class-wc-report-customers.php
9 months ago
class-wc-report-downloads.php
5 years ago
class-wc-report-low-in-stock.php
5 years ago
class-wc-report-most-stocked.php
5 years ago
class-wc-report-out-of-stock.php
5 years ago
class-wc-report-sales-by-category.php
2 years ago
class-wc-report-sales-by-date.php
1 year ago
class-wc-report-sales-by-product.php
3 months ago
class-wc-report-stock.php
1 year ago
class-wc-report-taxes-by-code.php
1 year ago
class-wc-report-taxes-by-date.php
1 year ago
class-wc-report-customers.php
425 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Report_Customers file. |
| 4 | * |
| 5 | * @package WooCommerce\Reports |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC_Report_Customers |
| 14 | * |
| 15 | * @package WooCommerce\Admin\Reports |
| 16 | * @version 2.1.0 |
| 17 | */ |
| 18 | class WC_Report_Customers extends WC_Admin_Report { |
| 19 | |
| 20 | /** |
| 21 | * Chart colors. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | public $chart_colours = array(); |
| 26 | |
| 27 | /** |
| 28 | * Customers. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | public $customers = array(); |
| 33 | |
| 34 | /** |
| 35 | * Get the legend for the main chart sidebar. |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public function get_chart_legend() { |
| 40 | $legend = array(); |
| 41 | |
| 42 | $legend[] = array( |
| 43 | /* translators: %s: signups amount */ |
| 44 | 'title' => sprintf( __( '%s signups in this period', 'woocommerce' ), '<strong>' . count( $this->customers ) . '</strong>' ), |
| 45 | 'color' => $this->chart_colours['signups'], |
| 46 | 'highlight_series' => 2, |
| 47 | ); |
| 48 | |
| 49 | return $legend; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get chart widgets. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | public function get_chart_widgets() { |
| 58 | $widgets = array(); |
| 59 | |
| 60 | $widgets[] = array( |
| 61 | 'title' => '', |
| 62 | 'callback' => array( $this, 'customers_vs_guests' ), |
| 63 | ); |
| 64 | |
| 65 | return $widgets; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Output customers vs guests chart. |
| 70 | */ |
| 71 | public function customers_vs_guests() { |
| 72 | |
| 73 | $customer_order_totals = $this->get_order_report_data( |
| 74 | array( |
| 75 | 'data' => array( |
| 76 | 'ID' => array( |
| 77 | 'type' => 'post_data', |
| 78 | 'function' => 'COUNT', |
| 79 | 'name' => 'total_orders', |
| 80 | ), |
| 81 | ), |
| 82 | 'where_meta' => array( |
| 83 | array( |
| 84 | 'meta_key' => '_customer_user', |
| 85 | 'meta_value' => '0', |
| 86 | 'operator' => '>', |
| 87 | ), |
| 88 | ), |
| 89 | 'filter_range' => true, |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | $guest_order_totals = $this->get_order_report_data( |
| 94 | array( |
| 95 | 'data' => array( |
| 96 | 'ID' => array( |
| 97 | 'type' => 'post_data', |
| 98 | 'function' => 'COUNT', |
| 99 | 'name' => 'total_orders', |
| 100 | ), |
| 101 | ), |
| 102 | 'where_meta' => array( |
| 103 | array( |
| 104 | 'meta_key' => '_customer_user', |
| 105 | 'meta_value' => '0', |
| 106 | 'operator' => '=', |
| 107 | ), |
| 108 | ), |
| 109 | 'filter_range' => true, |
| 110 | ) |
| 111 | ); |
| 112 | ?> |
| 113 | <div class="chart-container"> |
| 114 | <div class="chart-placeholder customers_vs_guests pie-chart" style="height:200px"></div> |
| 115 | <ul class="pie-chart-legend"> |
| 116 | <li style="border-color: <?php echo esc_attr( $this->chart_colours['customers'] ); ?>"><?php esc_html_e( 'Customer sales', 'woocommerce' ); ?></li> |
| 117 | <li style="border-color: <?php echo esc_attr( $this->chart_colours['guests'] ); ?>"><?php esc_html_e( 'Guest sales', 'woocommerce' ); ?></li> |
| 118 | </ul> |
| 119 | </div> |
| 120 | <script type="text/javascript"> |
| 121 | jQuery(function(){ |
| 122 | jQuery.plot( |
| 123 | jQuery('.chart-placeholder.customers_vs_guests'), |
| 124 | [ |
| 125 | { |
| 126 | label: '<?php esc_html_e( 'Customer orders', 'woocommerce' ); ?>', |
| 127 | data: "<?php echo esc_html( $customer_order_totals->total_orders ); ?>", |
| 128 | color: '<?php echo esc_html( $this->chart_colours['customers'] ); ?>' |
| 129 | }, |
| 130 | { |
| 131 | label: '<?php esc_html_e( 'Guest orders', 'woocommerce' ); ?>', |
| 132 | data: "<?php echo esc_html( $guest_order_totals->total_orders ); ?>", |
| 133 | color: '<?php echo esc_html( $this->chart_colours['guests'] ); ?>' |
| 134 | } |
| 135 | ], |
| 136 | { |
| 137 | grid: { |
| 138 | hoverable: true |
| 139 | }, |
| 140 | series: { |
| 141 | pie: { |
| 142 | show: true, |
| 143 | radius: 1, |
| 144 | innerRadius: 0.6, |
| 145 | label: { |
| 146 | show: false |
| 147 | } |
| 148 | }, |
| 149 | enable_tooltip: true, |
| 150 | append_tooltip: "<?php echo esc_html( ' ' . __( 'orders', 'woocommerce' ) ); ?>", |
| 151 | }, |
| 152 | legend: { |
| 153 | show: false |
| 154 | } |
| 155 | } |
| 156 | ); |
| 157 | |
| 158 | jQuery('.chart-placeholder.customers_vs_guests').trigger( 'resize' ); |
| 159 | }); |
| 160 | </script> |
| 161 | <?php |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Output the report. |
| 166 | */ |
| 167 | public function output_report() { |
| 168 | |
| 169 | $ranges = array( |
| 170 | 'year' => __( 'Year', 'woocommerce' ), |
| 171 | 'last_month' => __( 'Last month', 'woocommerce' ), |
| 172 | 'month' => __( 'This month', 'woocommerce' ), |
| 173 | '7day' => __( 'Last 7 days', 'woocommerce' ), |
| 174 | ); |
| 175 | |
| 176 | $this->chart_colours = array( |
| 177 | 'signups' => '#3498db', |
| 178 | 'customers' => '#1abc9c', |
| 179 | 'guests' => '#8fdece', |
| 180 | ); |
| 181 | |
| 182 | $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day'; |
| 183 | |
| 184 | if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ), true ) ) { |
| 185 | $current_range = '7day'; |
| 186 | } |
| 187 | |
| 188 | $this->check_current_range_nonce( $current_range ); |
| 189 | $this->calculate_current_range( $current_range ); |
| 190 | |
| 191 | $privileged_users = new WP_User_Query( |
| 192 | array( |
| 193 | 'fields' => 'ID', |
| 194 | 'role__in' => array( 'administrator', 'shop_manager' ), |
| 195 | ), |
| 196 | ); |
| 197 | $users_query = new WP_User_Query( |
| 198 | apply_filters( |
| 199 | 'woocommerce_admin_report_customers_user_query_args', |
| 200 | array( |
| 201 | 'fields' => array( 'user_registered' ), |
| 202 | 'exclude' => $privileged_users->get_results(), |
| 203 | ) |
| 204 | ) |
| 205 | ); |
| 206 | |
| 207 | $this->customers = $users_query->get_results(); |
| 208 | |
| 209 | foreach ( $this->customers as $key => $customer ) { |
| 210 | if ( strtotime( $customer->user_registered ) < $this->start_date || strtotime( $customer->user_registered ) > $this->end_date ) { |
| 211 | unset( $this->customers[ $key ] ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | include WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php'; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Output an export link. |
| 220 | */ |
| 221 | public function get_export_button() { |
| 222 | |
| 223 | $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day'; |
| 224 | ?> |
| 225 | <a |
| 226 | href="#" |
| 227 | download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_attr( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv" |
| 228 | class="export_csv" |
| 229 | data-export="chart" |
| 230 | data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>" |
| 231 | data-groupby="<?php echo esc_attr( $this->chart_groupby ); ?>" |
| 232 | > |
| 233 | <?php esc_html_e( 'Export CSV', 'woocommerce' ); ?> |
| 234 | </a> |
| 235 | <?php |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Output the main chart. |
| 240 | */ |
| 241 | public function get_main_chart() { |
| 242 | global $wp_locale; |
| 243 | |
| 244 | $customer_orders = $this->get_order_report_data( |
| 245 | array( |
| 246 | 'data' => array( |
| 247 | 'ID' => array( |
| 248 | 'type' => 'post_data', |
| 249 | 'function' => 'COUNT', |
| 250 | 'name' => 'total_orders', |
| 251 | ), |
| 252 | 'post_date' => array( |
| 253 | 'type' => 'post_data', |
| 254 | 'function' => '', |
| 255 | 'name' => 'post_date', |
| 256 | ), |
| 257 | ), |
| 258 | 'where_meta' => array( |
| 259 | array( |
| 260 | 'meta_key' => '_customer_user', |
| 261 | 'meta_value' => '0', |
| 262 | 'operator' => '>', |
| 263 | ), |
| 264 | ), |
| 265 | 'group_by' => $this->group_by_query, |
| 266 | 'order_by' => 'post_date ASC', |
| 267 | 'query_type' => 'get_results', |
| 268 | 'filter_range' => true, |
| 269 | ) |
| 270 | ); |
| 271 | |
| 272 | $guest_orders = $this->get_order_report_data( |
| 273 | array( |
| 274 | 'data' => array( |
| 275 | 'ID' => array( |
| 276 | 'type' => 'post_data', |
| 277 | 'function' => 'COUNT', |
| 278 | 'name' => 'total_orders', |
| 279 | ), |
| 280 | 'post_date' => array( |
| 281 | 'type' => 'post_data', |
| 282 | 'function' => '', |
| 283 | 'name' => 'post_date', |
| 284 | ), |
| 285 | ), |
| 286 | 'where_meta' => array( |
| 287 | array( |
| 288 | 'meta_key' => '_customer_user', |
| 289 | 'meta_value' => '0', |
| 290 | 'operator' => '=', |
| 291 | ), |
| 292 | ), |
| 293 | 'group_by' => $this->group_by_query, |
| 294 | 'order_by' => 'post_date ASC', |
| 295 | 'query_type' => 'get_results', |
| 296 | 'filter_range' => true, |
| 297 | ) |
| 298 | ); |
| 299 | |
| 300 | $signups = $this->prepare_chart_data( $this->customers, 'user_registered', '', $this->chart_interval, $this->start_date, $this->chart_groupby ); |
| 301 | $customer_orders = $this->prepare_chart_data( $customer_orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby ); |
| 302 | $guest_orders = $this->prepare_chart_data( $guest_orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby ); |
| 303 | |
| 304 | $chart_data = wp_json_encode( |
| 305 | array( |
| 306 | 'signups' => array_values( $signups ), |
| 307 | 'customer_orders' => array_values( $customer_orders ), |
| 308 | 'guest_orders' => array_values( $guest_orders ), |
| 309 | ) |
| 310 | ); |
| 311 | ?> |
| 312 | <div class="chart-container"> |
| 313 | <div class="chart-placeholder main"></div> |
| 314 | </div> |
| 315 | <script type="text/javascript"> |
| 316 | var main_chart; |
| 317 | |
| 318 | jQuery(function(){ |
| 319 | var chart_data = JSON.parse( decodeURIComponent( '<?php echo rawurlencode( $chart_data ); ?>' ) ); |
| 320 | |
| 321 | var drawGraph = function( highlight ) { |
| 322 | var series = [ |
| 323 | { |
| 324 | label: "<?php echo esc_js( __( 'Customer orders', 'woocommerce' ) ); ?>", |
| 325 | data: chart_data.customer_orders, |
| 326 | color: '<?php echo esc_html( $this->chart_colours['customers'] ); ?>', |
| 327 | bars: { fillColor: '<?php echo esc_html( $this->chart_colours['customers'] ); ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo esc_html( $this->barwidth ); ?> * 0.5, align: 'center' }, |
| 328 | shadowSize: 0, |
| 329 | enable_tooltip: true, |
| 330 | append_tooltip: "<?php echo esc_html( ' ' . __( 'customer orders', 'woocommerce' ) ); ?>", |
| 331 | stack: true, |
| 332 | }, |
| 333 | { |
| 334 | label: "<?php echo esc_js( __( 'Guest orders', 'woocommerce' ) ); ?>", |
| 335 | data: chart_data.guest_orders, |
| 336 | color: '<?php echo esc_html( $this->chart_colours['guests'] ); ?>', |
| 337 | bars: { fillColor: '<?php echo esc_html( $this->chart_colours['guests'] ); ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo esc_html( $this->barwidth ); ?> * 0.5, align: 'center' }, |
| 338 | shadowSize: 0, |
| 339 | enable_tooltip: true, |
| 340 | append_tooltip: "<?php echo esc_html( ' ' . __( 'guest orders', 'woocommerce' ) ); ?>", |
| 341 | stack: true, |
| 342 | }, |
| 343 | { |
| 344 | label: "<?php echo esc_js( __( 'Signups', 'woocommerce' ) ); ?>", |
| 345 | data: chart_data.signups, |
| 346 | color: '<?php echo esc_html( $this->chart_colours['signups'] ); ?>', |
| 347 | points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true }, |
| 348 | lines: { show: true, lineWidth: 4, fill: false }, |
| 349 | shadowSize: 0, |
| 350 | enable_tooltip: true, |
| 351 | append_tooltip: "<?php echo esc_html( ' ' . __( 'new users', 'woocommerce' ) ); ?>", |
| 352 | stack: false |
| 353 | }, |
| 354 | ]; |
| 355 | |
| 356 | if ( highlight !== 'undefined' && series[ highlight ] ) { |
| 357 | highlight_series = series[ highlight ]; |
| 358 | |
| 359 | highlight_series.color = '#9c5d90'; |
| 360 | |
| 361 | if ( highlight_series.bars ) |
| 362 | highlight_series.bars.fillColor = '#9c5d90'; |
| 363 | |
| 364 | if ( highlight_series.lines ) { |
| 365 | highlight_series.lines.lineWidth = 5; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | main_chart = jQuery.plot( |
| 370 | jQuery('.chart-placeholder.main'), |
| 371 | series, |
| 372 | { |
| 373 | legend: { |
| 374 | show: false |
| 375 | }, |
| 376 | grid: { |
| 377 | color: '#aaa', |
| 378 | borderColor: 'transparent', |
| 379 | borderWidth: 0, |
| 380 | hoverable: true |
| 381 | }, |
| 382 | xaxes: [ { |
| 383 | color: '#aaa', |
| 384 | position: "bottom", |
| 385 | tickColor: 'transparent', |
| 386 | mode: "time", |
| 387 | timeformat: "<?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?>", |
| 388 | monthNames: JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( array_values( $wp_locale->month_abbrev ) ) ); ?>' ) ), |
| 389 | tickLength: 1, |
| 390 | minTickSize: [1, "<?php echo esc_html( $this->chart_groupby ); ?>"], |
| 391 | tickSize: [1, "<?php echo esc_html( $this->chart_groupby ); ?>"], |
| 392 | font: { |
| 393 | color: "#aaa" |
| 394 | } |
| 395 | } ], |
| 396 | yaxes: [ |
| 397 | { |
| 398 | min: 0, |
| 399 | minTickSize: 1, |
| 400 | tickDecimals: 0, |
| 401 | color: '#ecf0f1', |
| 402 | font: { color: "#aaa" } |
| 403 | } |
| 404 | ], |
| 405 | } |
| 406 | ); |
| 407 | jQuery('.chart-placeholder').trigger( 'resize' ); |
| 408 | } |
| 409 | |
| 410 | drawGraph(); |
| 411 | |
| 412 | jQuery('.highlight_series').on( 'mouseenter', |
| 413 | function() { |
| 414 | drawGraph( jQuery(this).data('series') ); |
| 415 | } ).on( 'mouseleave', |
| 416 | function() { |
| 417 | drawGraph(); |
| 418 | } |
| 419 | ); |
| 420 | }); |
| 421 | </script> |
| 422 | <?php |
| 423 | } |
| 424 | } |
| 425 |