1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
<?php // phpcs:ignoreFile
namespace AutomateWoo;
if ( ! defined( 'ABSPATH' ) ) exit;
/** * @class Report_Conversions * * @deprecated since 5.6.9 Migrated to Analytics. * @see AutomateWoo\Admin\Analytics * @see AutomateWoo\Admin\Analytics\Rest_API\Conversions\Stats */ class Report_Conversions extends \AW_Report_Abstract_Graph {
/** @var array */ public $chart_colours = [ 'conversion_value' => '#3498db', 'conversion_number' => '#DBE1E3' ];
public $workflow_ids = []; public $workflow_ids_titles = [];
public $conversion_orders = [];
public $conversion_total_value = 0; public $conversion_total_orders = 0;
/** * Constructor */ function __construct() { $this->workflow_ids = $this->get_filtered_workflows(); }
/** * */ function load_chart_data() { $start_date = new DateTime(); $start_date->setTimestamp( $this->start_date );
$end_date = new DateTime(); $end_date->setTimestamp( $this->end_date ); $end_date->modify('+1 days');
$meta_query = [];
if ( $this->workflow_ids ) { $meta_query[] = [ 'key' => '_aw_conversion', 'value' => $this->workflow_ids, ]; } else { $meta_query[] = [ 'key' => '_aw_conversion', 'compare' => 'EXISTS', ]; }
// Get converted orders $orders = new \WP_Query([ 'post_type' => 'shop_order', 'post_status' => array_map( 'aw_add_order_status_prefix', wc_get_is_paid_statuses() ), 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => $meta_query, 'date_query' => [ [ 'column' => 'post_date', 'after' => $start_date->to_mysql_string() ], [ 'column' => 'post_date', 'before' => $end_date->to_mysql_string() ] ] ]);
foreach ( $orders->posts as $order_id ) { $order = wc_get_order( $order_id );
$this->conversion_total_value += $order->get_total();
$order_obj = new \stdClass(); $order_obj->date = $order->get_date_created()->date( Format::MYSQL ); // keep site time for reports $order_obj->total = $order->get_total();
$this->conversion_orders[] = $order_obj; }
$this->conversion_total_orders = $orders->post_count; }
/** * Get the legend for the main chart sidebar * @return array */ function get_chart_legend() {
$this->load_chart_data();
$legend = [];
$legend[] = [ 'title' => sprintf( /* translators: %s Conversion total price. */ __( '%s converted order value', 'automatewoo' ), '<strong>' . wc_price( $this->conversion_total_value ) . '</strong>' ), 'color' => $this->chart_colours['conversion_value'], 'highlight_series' => 1, ];
$legend[] = [ 'title' => sprintf( /* translators: %s Number of converted orders. */ __( '%s converted orders', 'automatewoo' ), '<strong>' . $this->conversion_total_orders . '</strong>' ), 'color' => $this->chart_colours['conversion_number'], 'highlight_series' => 0, ];
return $legend; }
/** * [get_chart_widgets description] * * @return array */ function get_chart_widgets() { $widgets = [];
if ( ! empty( $this->workflow_ids ) ) { $widgets[] = [ 'title' => __( 'Showing reports for:', 'automatewoo' ), 'callback' => [ $this, 'current_filters' ] ]; }
$widgets[] = [ 'title' => '', 'callback' => [ $this, 'output_workflows_widget' ] ];
return $widgets; }
/** * Show current filters */ function current_filters() {
$this->workflow_ids_titles = array();
foreach ( $this->workflow_ids as $workflow_id ) {
$workflow = new Workflow( $workflow_id );
if ( $workflow ) { $this->workflow_ids_titles[] = $workflow->title; } else { $this->workflow_ids_titles[] = '#' . $workflow_id; } }
echo '<p>' . ' <strong>' . implode( ', ', $this->workflow_ids_titles ) . '</strong></p>'; echo '<p><a class="button" href="' . esc_url( remove_query_arg( 'workflow_ids' ) ) . '">' . __( 'Reset', 'automatewoo' ) . '</a></p>'; }
/** * Get the main chart * * @return string */ function get_main_chart() {
global $wp_locale;
// Prepare data for report $conversion_value = $this->prepare_chart_data( $this->conversion_orders, 'date', 'total', $this->chart_interval, $this->start_date, $this->chart_groupby ); $conversion_number = $this->prepare_chart_data( $this->conversion_orders, 'date', false, $this->chart_interval, $this->start_date, $this->chart_groupby );
// Encode in json format $chart_data = wp_json_encode( [ 'conversion_value' => array_values( $conversion_value ), 'conversion_number' => array_values( $conversion_number ), ] );
?> <div class="chart-container"> <div class="chart-placeholder main"></div> </div> <script type="text/javascript"> var main_chart;
jQuery(function($){
var order_data = JSON.parse( decodeURIComponent( '<?php echo rawurlencode( $chart_data ); ?>' ) );
var drawGraph = function( highlight ) {
var series = [ { label: "<?php echo esc_js( __( 'Conversion Number', 'automatewoo' ) ) ?>", data: order_data.conversion_number, yaxis: 1, color: '<?php echo $this->chart_colours['conversion_number']; ?>', bars: { fillColor: '<?php echo $this->chart_colours['conversion_number']; ?>', fill: true, show: true, lineWidth: 0, barWidth: 60 * 60 * 24 * 1000, align: 'center' }, shadowSize: 0, hoverable: false }, { label: "<?php echo esc_js( __( 'Conversion Value', 'automatewoo' ) ) ?>", data: order_data.conversion_value, yaxis: 2, color: '<?php echo $this->chart_colours['conversion_value']; ?>', points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true }, lines: { show: true, lineWidth: 4, fill: false }, shadowSize: 0 }, ];
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; } }
main_chart = jQuery.plot( jQuery('.chart-placeholder.main'), series, { legend: { show: false }, grid: { color: '#aaa', borderColor: 'transparent', borderWidth: 0, hoverable: true }, xaxes: [ { color: '#aaa', position: "bottom", tickColor: 'transparent', mode: "time", timeformat: "<?php if ( $this->chart_groupby == 'day' ) echo '%d %b'; else echo '%b'; ?>", monthNames: JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( array_values( $wp_locale->month_abbrev ) ) ); ?>' ) ), tickLength: 1, minTickSize: [1, "<?php echo $this->chart_groupby; ?>"], font: { color: "#aaa" } } ], yaxes: [ { min: 0, minTickSize: 1, tickDecimals: 0, color: '#d4d9dc', font: { color: "#aaa" } }, { position: "right", min: 0, tickDecimals: 0, alignTicksWithAxis: 0, color: '#eee', font: { color: "#aaa" } } ] } );
jQuery( '.chart-placeholder' ).trigger( 'resize' ); }
drawGraph();
$( document.body ) .on( 'mouseenter', '.highlight_series', function() { drawGraph( $(this).data('series') ); } ) .on( 'mouseleave', '.highlight_series', function() { drawGraph(); } ) ;
}); </script> <?php
}
}
|