WC_Report_Customers

WC_Report_Customers.

Defined (1)

The class is defined in the following location(s).

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