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