PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.5.0
Order Tracking – WordPress Status Tracking Plugin v3.5.0
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / OrderManager.class.php

OrderManager.class.php in Order Tracking – WordPress Status Tracking Plugin 3.5.0, at includes/OrderManager.class.php

759 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to handle all order database interactions for the Order Tracking plugin
4 */
5
6 if ( !defined( 'ABSPATH' ) )
7 exit;
8
9 if ( !class_exists( 'ewdotpOrderManager' ) ) {
10 class ewdotpOrderManager {
11
12 // The name of the orders table, set in the constructor
13 public $orders_table_name;
14
15 // The name of the orders statuses table, set in the constructor
16 public $order_statuses_table_name;
17
18 // The name of the meta table, set in the constructor
19 public $meta_table_name;
20
21 // Array containing the arguments for the query
22 public $args = array();
23
24 // Array containing retrieved order objects
25 public $orders = array();
26
27 public function __construct() {
28 global $wpdb;
29
30 $this->orders_table_name = $wpdb->prefix . "EWD_OTP_Orders";
31 $this->order_statuses_table_name = $wpdb->prefix . "EWD_OTP_Order_Statuses";
32 $this->meta_table_name = $wpdb->prefix . "EWD_OTP_Fields_Meta";
33
34 if ( get_transient( 'ewd-otp-update-tables' ) ) {
35
36 add_action( 'plugins_loaded', array( $this, 'create_tables' ) );
37 }
38 }
39
40 /**
41 * Creates the tables used to store orders and their meta information
42 * @since 3.0.0
43 */
44 public function create_tables() {
45
46 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
47
48 $sql = "CREATE TABLE $this->orders_table_name (
49 Order_ID mediumint(9) NOT NULL AUTO_INCREMENT,
50 Order_Name text DEFAULT '' NOT NULL,
51 Order_Number text DEFAULT '' NOT NULL,
52 Order_Status text DEFAULT '' NOT NULL,
53 Order_External_Status text DEFAULT '' NOT NULL,
54 Order_Location text DEFAULT '' NOT NULL,
55 Order_Notes_Public text DEFAULT '' NOT NULL,
56 Order_Notes_Private text DEFAULT '' NOT NULL,
57 Order_Customer_Notes text DEFAULT '' NOT NULL,
58 Order_Email text DEFAULT '' NOT NULL,
59 Order_Phone_Number text DEFAULT '' NOT NULL,
60 Sales_Rep_ID mediumint(9) DEFAULT 0 NOT NULL,
61 Customer_ID mediumint(9) DEFAULT 0 NOT NULL,
62 WooCommerce_ID mediumint(9) DEFAULT 0 NOT NULL,
63 Zendesk_ID mediumint(9) DEFAULT 0 NOT NULL,
64 Order_Status_Updated datetime DEFAULT '0000-00-00 00:00:00' NULL,
65 Order_Display text DEFAULT '' NOT NULL,
66 Order_Payment_Price text DEFAULT '' NOT NULL,
67 Order_Payment_Completed text DEFAULT '' NOT NULL,
68 Order_PayPal_Receipt_Number text DEFAULT '' NOT NULL,
69 Order_View_Count mediumint(9) DEFAULT 0 NOT NULL,
70 Order_Tracking_Link_Clicked text DEFAULT '' NOT NULL,
71 Order_Tracking_Link_Code text DEFAULT '' NOT NULL,
72 UNIQUE KEY id (Order_ID)
73 )
74 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
75
76 dbDelta($sql);
77
78
79 $sql = "CREATE TABLE $this->order_statuses_table_name (
80 Order_Status_ID mediumint(9) NOT NULL AUTO_INCREMENT,
81 Order_ID mediumint(9) DEFAULT 0 NOT NULL,
82 Order_Status text DEFAULT '' NOT NULL,
83 Order_Location text DEFAULT '' NOT NULL,
84 Order_Internal_Status text DEFAULT '' NOT NULL,
85 Order_Status_Created datetime DEFAULT '0000-00-00 00:00:00' NULL,
86 UNIQUE KEY id (Order_Status_ID)
87 )
88 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
89
90 dbDelta($sql);
91
92 $sql = "CREATE TABLE $this->meta_table_name (
93 Meta_ID mediumint(9) NOT NULL AUTO_INCREMENT,
94 Field_ID mediumint(9) DEFAULT '0',
95 Order_ID mediumint(9) DEFAULT '0',
96 Customer_ID mediumint(9) DEFAULT '0',
97 Sales_Rep_ID mediumint(9) DEFAULT '0',
98 Meta_Value text DEFAULT '' NOT NULL,
99 UNIQUE KEY id (Meta_ID)
100 )
101 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
102
103 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
104
105 dbDelta($sql);
106 }
107
108 /**
109 * Returns a single order given its order ID
110 * @since 3.0.0
111 */
112 public function get_order_from_id( $order_id ) {
113 global $wpdb;
114
115 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
116
117 return $db_order;
118 }
119
120 /**
121 * Returns a single order given its Zendesk ID
122 * @since 3.0.0
123 */
124 public function get_order_from_zendesk_id( $zendesk_id ) {
125 global $wpdb;
126
127 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Zendesk_ID=%d", $zendesk_id ) );
128
129 return ! empty( $db_order ) ? $db_order : null;
130 }
131
132 /**
133 * Returns a single order given its WooCommerce ID
134 * @since 3.0.0
135 */
136 public function get_order_from_woocommerce_id( $post_id ) {
137 global $wpdb;
138
139 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE WooCommerce_ID=%d", $post_id ) );
140
141 return ! empty( $db_order ) ? $db_order : null;
142 }
143
144 /**
145 * Returns a single order given its order number
146 * @since 3.0.0
147 */
148 public function get_order_from_tracking_number( $order_number ) {
149 global $wpdb;
150
151 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_Number=%s", $order_number ) );
152
153 return ! empty( $db_order ) ? $db_order : null;
154 }
155
156 /**
157 * Returns orders matching the arguments supplied
158 * @since 3.0.0
159 */
160 public function get_matching_orders( $args ) {
161
162 $this->orders = array();
163
164 $defaults = array(
165 'orders_per_page' => 20,
166 'order' => 'ASC',
167 'paged' => 1,
168 'date_range' => '',
169 );
170
171 $this->args = wp_parse_args( $args, $defaults );
172
173 $this->prepare_args();
174
175 $this->run_query();
176
177 return $this->orders;
178 }
179
180 /**
181 * Return the counts for orders being displayed on the admin orders page
182 * @since 3.0.0
183 */
184 public function get_order_counts( $args ) {
185 global $wpdb;
186 global $ewd_otp_controller;
187
188 $this->args = $args;
189
190 $this->prepare_args();
191
192 $args = $this->args;
193
194 $query_string = "SELECT Order_Status, count( * ) AS num_orders
195 FROM $this->orders_table_name
196 WHERE 1=%d
197 ";
198
199 $query_args = array(1);
200
201 if ( ! empty( $args['number'] ) ) {
202
203 $query_string .= " AND Order_Number=%s";
204 $query_args[] = sanitize_text_field( $args['number'] );
205 }
206
207 if ( ! empty( $args['display'] ) ) {
208
209 $query_string .= " AND Order_Display=%s";
210 $query_args[] = 'Yes';
211 }
212
213 if ( ! empty( $args['after'] ) ) {
214
215 $query_string .= " AND Order_Status_Updated>=%s";
216 $query_args[] = $args['after'];
217 }
218
219 if ( ! empty( $args['before'] ) ) {
220
221 $query_string .= " AND Order_Status_Updated<=%s";
222 $query_args[] = $args['before'];
223 }
224
225 if ( ! empty( $args['date'] ) ) {
226
227 $query_string .= " AND DATE(Order_Status_Updated)=%s";
228 $query_args[] = $args['date'];
229 }
230
231 $query_string .= " GROUP BY Order_Status";
232
233 $count_results = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) );
234
235 $statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
236
237 $counts = array();
238
239 foreach( $statuses as $status ) {
240
241 $counts[ sanitize_title( $status->status, '', 'ewd_otp' ) ] = 0;
242 }
243
244 foreach ( $count_results as $count ) {
245
246 $counts[ sanitize_title( $count->Order_Status, '', 'ewd_otp' ) ] = $count->num_orders;
247 }
248
249 $counts['total'] = array_sum( $counts );
250
251 return $counts;
252 }
253
254 /**
255 * Prepares the arguments before the query is run
256 * @since 3.0.0
257 */
258 public function prepare_args() {
259
260 $args = $this->args;
261
262 if ( is_string( $args['date_range'] ) ) {
263
264 if ( !empty( $args['start_date'] ) || !empty( $args['end_date'] ) ) {
265
266 if ( !empty( $args['start_date'] ) ) {
267 $args['after'] = sanitize_text_field( $args['start_date'] ) . ( ( isset( $args['start_time'] ) and $args['start_time'] ) ? $args['start_time'] : '' );
268 }
269
270 if ( !empty( $args['end_date'] ) ) {
271 $args['before'] = sanitize_text_field( $args['end_date'] ) . ( ( isset( $args['end_time'] ) and $args['end_time'] ) ? $args['end_time'] : ' 23:59' );
272 }
273 } elseif ( $args['date_range'] === 'today' ) {
274
275 $args['after'] = date( 'Y-m-d H:i:s', strtotime( 'today midnight' ) );
276 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'tomorrow midnight' ) );
277
278 } elseif ( $args['date_range'] === 'week' ) {
279
280 $args['after'] = date( 'Y-m-d H:i:s', strtotime( 'monday this week' ) );
281 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'sunday this week' ) );
282 } elseif ( $args['date_range'] === 'past' ) {
283
284 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
285 }
286 }
287
288 $this->args = $args;
289
290 return $this->args;
291 }
292
293 /**
294 * Create and run the SQL query based on the arguments received
295 * @since 3.0.0
296 */
297 public function run_query() {
298 global $wpdb;
299
300 $args = $this->args;
301
302 $query_string = "SELECT * FROM $this->orders_table_name WHERE 1=%d";
303
304 $query_args = array( 1 );
305
306 if ( ! empty( $args['id'] ) ) {
307
308 $query_string .= " AND Order_ID=%d";
309 $query_args[] = intval( $args['id'] );
310 }
311
312 if ( ! empty( $args['name'] ) ) {
313
314 $query_string .= " AND Order_Name=%s";
315 $query_args[] = $args['name'];
316 }
317
318 if ( ! empty( $args['number'] ) ) {
319
320 $query_string .= " AND Order_Number=%s";
321 $query_args[] = $args['number'];
322 }
323
324 if ( ! empty( $args['location'] ) ) {
325
326 $query_string .= " AND Order_Location=%s";
327 $query_args[] = $args['location'];
328 }
329
330 if ( ! empty( $args['status'] ) ) {
331
332 if( is_array( $args['status'] ) ) {
333 $status_placeholder = implode( ', ', array_fill( 0, count( $args['status'] ), '%s' ) );
334 $query_string .= " AND Order_Status IN ( $status_placeholder )";
335 $query_args = array_merge( $query_args, $args['status'] );
336 }
337 else {
338 $query_string .= " AND Order_Status=%s";
339 $query_args[] = $args['status'];
340 }
341 }
342
343 if ( ! empty( $args['location'] ) ) {
344
345 if( is_array( $args['location'] ) ) {
346 $location_placeholder = implode( ', ', array_fill( 0, count( $args['location'] ), '%s' ) );
347 $query_string .= " AND Order_Location IN ( $location_placeholder )";
348 $query_args = array_merge( $query_args, $args['location'] );
349 }
350 else {
351 $query_string .= " AND Order_Location=%s";
352 $query_args[] = $args['location'];
353 }
354 }
355
356 if ( ! empty( $args['email'] ) ) {
357
358 $query_string .= " AND Order_Email=%s";
359 $query_args[] = $args['email'];
360 }
361
362 if ( ! empty( $args['customer'] ) ) {
363
364 if( is_array( $args['customer'] ) ) {
365 $cstm_plchldr = implode( ', ', array_fill( 0, count( $args['customer'] ), '%d' ) );
366 $query_string .= " AND Customer_ID IN ( $cstm_plchldr )";
367 $query_args = array_merge( $query_args, $args['customer'] );
368 }
369 else {
370 $query_string .= " AND Customer_ID=%d";
371 $query_args[] = intval( $args['customer'] );
372 }
373 }
374
375 if ( ! empty( $args['sales_rep'] ) ) {
376
377 if( is_array( $args['sales_rep'] ) ) {
378 $sls_rp_plchldr = implode( ', ', array_fill( 0, count( $args['sales_rep'] ), '%d' ) );
379 $query_string .= " AND Sales_Rep_ID IN ( $sls_rp_plchldr )";
380 $query_args = array_merge( $query_args, $args['sales_rep'] );
381 }
382 else {
383 $query_string .= " AND Sales_Rep_ID=%d";
384 $query_args[] = intval( $args['sales_rep'] );
385 }
386 }
387
388 if ( ! empty( $args['display'] ) ) {
389
390 if ( strtolower( $args['display'] ) == 'no' ){
391
392 $query_string .= " AND Order_Display=%s";
393 $query_args[] = 'No';
394 }
395 else {
396
397 $query_string .= " AND Order_Display=%s";
398 $query_args[] = 'Yes';
399 }
400 }
401
402 if ( ! empty( $args['payment_completed'] ) ) {
403
404 $query_string .= " AND Order_Payment_Completed=%s";
405 $query_args[] = $args['payment_completed'];
406 }
407
408 if ( ! empty( $args['after'] ) ) {
409
410 $query_string .= " AND Order_Status_Updated>=%s";
411 $query_args[] = $args['after'];
412 }
413
414 if ( ! empty( $args['before'] ) ) {
415
416 $query_string .= " AND Order_Status_Updated<=%s";
417 $query_args[] = $args['before'];
418 }
419
420 if ( ! empty( $args['date'] ) ) {
421
422 $query_string .= " AND DATE(Order_Status_Updated)=%s";
423 $query_args[] = $args['date'];
424 }
425
426 if ( ! empty( $args['orderby'] ) ) {
427
428 $args['orderby'] = $args['orderby'] == 'date' ? 'Order_Status_Updated' : $args['orderby'];
429
430 $query_string .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' );
431 }
432
433 if ( $args['orders_per_page'] > 0 ) {
434
435 $query_string .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['orders_per_page'] ) . ', ' . intval( $args['orders_per_page'] );
436 }
437
438 $db_orders = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) );
439
440 foreach ( $db_orders as $db_order ) {
441
442 $order = new ewdotpOrder();
443
444 $order->load_order( $db_order );
445
446 $this->orders[] = $order;
447 }
448 }
449
450 /**
451 * Returns the value for a given field/order id pair
452 * @since 3.0.0
453 */
454 public function get_order_status_history( $order_id ) {
455 global $wpdb;
456
457 return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $this->order_statuses_table_name WHERE Order_ID=%d ORDER BY Order_Status_Created", $order_id ) );
458 }
459
460 /**
461 * Returns the value for a given field/order id pair
462 * @since 3.0.0
463 */
464 public function get_order_field( $field, $order_id ) {
465 global $wpdb;
466
467 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
468
469 $order = new ewdotpOrder();
470 $order->load_order( $db_order );
471
472 return ! empty( $order->$field ) ? $order->$field : '';
473 }
474
475 /**
476 * Returns the value for a given custom_field/order pair
477 * @since 3.0.0
478 */
479 public function get_field_value( $custom_field_id, $order_id ) {
480 global $wpdb;
481
482 return $wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value FROM $this->meta_table_name WHERE Field_ID=%d AND Order_ID=%d ORDER BY Meta_ID DESC", $custom_field_id, $order_id ) );
483 }
484
485 /**
486 * Accepts an order object, inserts it into the database, and returns the ID of the newly inserted order
487 * @since 3.0.0
488 */
489 public function insert_order( $order ) {
490 global $wpdb;
491 global $ewd_otp_controller;
492
493 $query_args = array(
494 'Order_Name' => ! empty( $order->name ) ? $order->name : '',
495 'Order_Number' => ! empty( $order->number ) ? $order->number : '',
496 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
497 'Order_External_Status' => ! empty( $order->external_status ) ? $order->external_status : '',
498 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
499 'Order_Notes_Public' => ! empty( $order->notes_public ) ? $order->notes_public : '',
500 'Order_Notes_Private' => ! empty( $order->notes_private ) ? $order->notes_private : '',
501 'Order_Customer_Notes' => ! empty( $order->customer_notes ) ? $order->customer_notes : '',
502 'Order_Email' => ! empty( $order->email ) ? $order->email : '',
503 'Order_Phone_Number' => ! empty( $order->phone_number ) ? $order->phone_number : '',
504 'Customer_ID' => ! empty( $order->customer ) ? $order->customer : 0,
505 'Sales_Rep_ID' => ! empty( $order->sales_rep ) ? $order->sales_rep : 0,
506 'WooCommerce_ID' => ! empty( $order->woocommerce_id ) ? $order->woocommerce_id : 0,
507 'Zendesk_ID' => ! empty( $order->zendesk_id ) ? $order->zendesk_id : 0,
508 'Order_Status_Updated' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
509 'Order_Display' => ! empty( $order->display ) ? 'Yes' : 'No',
510 'Order_Payment_Price' => ! empty( $order->payment_price ) ? $order->payment_price : '',
511 'Order_Payment_Completed' => ! empty( $order->payment_completed ) ? $order->payment_completed : '',
512 'Order_PayPal_Receipt_Number' => ! empty( $order->paypal_receipt_number ) ? $order->paypal_receipt_number : '',
513 'Order_View_Count' => ! empty( $order->views ) ? $order->views : 0,
514 'Order_Tracking_Link_Clicked' => ! empty( $order->tracking_link_clicked ) ? 'Yes' : 'No',
515 'Order_Tracking_Link_Code' => ! empty( $order->tracking_link_code ) ? $order->tracking_link_code : '',
516 );
517
518 $wpdb->insert(
519 $this->orders_table_name,
520 $query_args
521 );
522
523 $order_id = $wpdb->insert_id;
524
525 if ( ! $order_id ) { return $order_id; }
526
527 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
528
529 foreach ( $custom_fields as $custom_field ) {
530
531 if ( empty( $order->custom_fields[ $custom_field->id ] ) ) { continue; }
532
533 $query_args = array(
534 'Field_ID' => $custom_field->id,
535 'Order_ID' => $order_id,
536 'Meta_Value' => $order->custom_fields[ $custom_field->id ]
537 );
538
539 $wpdb->insert(
540 $this->meta_table_name,
541 $query_args
542 );
543 }
544
545 return $order_id;
546 }
547
548 /**
549 * Accepts an order object, updates it in the database, and returns the ID if successful or false otherwise
550 * @since 3.0.0
551 */
552 public function update_order( $order ) {
553 global $wpdb;
554 global $ewd_otp_controller;
555
556 if ( empty( $order->id ) ) { return false; }
557
558 $query_args = array(
559 'Order_Name' => ! empty( $order->name ) ? $order->name : '',
560 'Order_Number' => ! empty( $order->number ) ? $order->number : '',
561 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
562 'Order_External_Status' => ! empty( $order->external_status ) ? $order->external_status : '',
563 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
564 'Order_Notes_Public' => ! empty( $order->notes_public ) ? $order->notes_public : '',
565 'Order_Notes_Private' => ! empty( $order->notes_private ) ? $order->notes_private : '',
566 'Order_Customer_Notes' => ! empty( $order->customer_notes ) ? $order->customer_notes : '',
567 'Order_Email' => ! empty( $order->email ) ? $order->email : '',
568 'Order_Phone_Number' => ! empty( $order->phone_number ) ? $order->phone_number : '',
569 'Customer_ID' => ! empty( $order->customer ) ? $order->customer : 0,
570 'Sales_Rep_ID' => ! empty( $order->sales_rep ) ? $order->sales_rep : 0,
571 'WooCommerce_ID' => ! empty( $order->woocommerce_id ) ? $order->woocommerce_id : 0,
572 'Zendesk_ID' => ! empty( $order->zendesk_id ) ? $order->zendesk_id : 0,
573 'Order_Status_Updated' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
574 'Order_Display' => ! empty( $order->display ) ? 'Yes' : 'No',
575 'Order_Payment_Price' => ! empty( $order->payment_price ) ? $order->payment_price : '',
576 'Order_Payment_Completed' => ! empty( $order->payment_completed ) ? 'Yes' : 'No',
577 'Order_PayPal_Receipt_Number' => ! empty( $order->paypal_receipt_number ) ? $order->paypal_receipt_number : '',
578 'Order_View_Count' => ! empty( $order->views ) ? $order->views : 0,
579 'Order_Tracking_Link_Clicked' => ! empty( $order->tracking_link_clicked ) ? 'Yes' : 'No',
580 'Order_Tracking_Link_Code' => ! empty( $order->tracking_link_code ) ? $order->tracking_link_code : '',
581 );
582
583 $wpdb->update(
584 $this->orders_table_name,
585 $query_args,
586 array( 'Order_ID' => $order->id )
587 );
588
589 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
590
591 foreach ( $custom_fields as $custom_field ) {
592
593 $wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value from $this->meta_table_name WHERE Field_ID=%d AND Order_ID=%d ORDER BY Meta_ID DESC", $custom_field->id, $order->id ) );
594
595 $update = $wpdb->num_rows ? true : false;
596
597 if ( empty( $order->custom_fields[ $custom_field->id ] ) ) {
598
599 $where_args = array(
600 'Field_ID' => $custom_field->id,
601 'Order_ID' => $order->id,
602 );
603
604 $wpdb->delete(
605 $this->meta_table_name,
606 $where_args
607 );
608 }
609 elseif ( $update ) {
610
611 $query_args = array(
612 'Meta_Value' => $order->custom_fields[ $custom_field->id ]
613 );
614
615 $where_args = array(
616 'Field_ID' => $custom_field->id,
617 'Order_ID' => $order->id,
618 );
619
620 $wpdb->update(
621 $this->meta_table_name,
622 $query_args,
623 $where_args
624 );
625 }
626 else {
627
628 $query_args = array(
629 'Meta_Value' => $order->custom_fields[ $custom_field->id ],
630 'Field_ID' => $custom_field->id,
631 'Order_ID' => $order->id,
632 );
633
634 $wpdb->insert(
635 $this->meta_table_name,
636 $query_args
637 );
638 }
639 }
640
641 return $order->id;
642 }
643
644 /**
645 * Adds a new order status to the database
646 * @since 3.0.0
647 */
648 public function update_order_status( $order ) {
649 global $wpdb;
650
651 $query_args = array(
652 'Order_ID' => $order->id,
653 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
654 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
655 'Order_Internal_Status' => ! empty( $order->status ) ? $order->status : '',
656 'Order_Status_Created' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
657 );
658
659 $wpdb->insert(
660 $this->order_statuses_table_name,
661 $query_args
662 );
663 }
664
665 /**
666 * Removs an existing order status
667 * @since 3.0.1
668 */
669 public function delete_order_status( $order_status_id ) {
670 global $wpdb;
671
672 $wpdb->delete(
673 $this->order_statuses_table_name,
674 array( 'Order_Status_ID' => $order_status_id)
675 );
676 }
677
678 /**
679 * Accepts an order id, deletes the corresponding order
680 * @since 3.0.0
681 */
682 public function delete_order( $order_id ) {
683 global $wpdb;
684
685 $wpdb->delete(
686 $this->orders_table_name,
687 array( 'Order_ID' => $order_id )
688 );
689
690 $wpdb->delete(
691 $this->order_statuses_table_name,
692 array( 'Order_ID' => $order_id )
693 );
694
695 $wpdb->delete(
696 $this->meta_table_name,
697 array( 'Order_ID' => $order_id )
698 );
699 }
700
701 /**
702 * Accepts an order id, sets the payment_received field for the corresponding order to Yes
703 * @since 3.0.0
704 */
705 public function set_order_paid( $order_id ) {
706 global $wpdb;
707
708 $query_args = array(
709 'Order_Payment_Completed' => 'Yes'
710 );
711
712 $wpdb->update(
713 $this->orders_table_name,
714 $query_args,
715 array( 'Order_ID' => $order_id )
716 );
717 }
718
719 /**
720 * Accepts an order id, updates the status of the corresponding order
721 * @since 3.0.0
722 */
723 public function set_order_status( $order_id, $status ) {
724 global $wpdb;
725
726 $order = new ewdotpOrder();
727 $order->load_order_from_id( $order_id );
728
729 $order->set_status( $status );
730 }
731
732 /**
733 * Increment the views for a given order_id
734 * @since 3.0.4
735 */
736 public function increase_order_views( $order_id ) {
737 global $wpdb;
738
739 $wpdb->query( $wpdb->prepare( "UPDATE $this->orders_table_name SET Order_View_Count=Order_View_Count+1 WHERE Order_ID=%d", $order_id ) );
740 }
741
742 /**
743 * Accepts an email and order id, and verify that they match the saved data,
744 * @since 3.0.0
745 */
746 public function verify_order_email( $email, $order_id ) {
747 global $wpdb;
748
749 $order_email = $wpdb->get_var( $wpdb->prepare( "SELECT Order_Email FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
750
751 if ( $order_email == $email ) {
752
753 return true;
754 }
755
756 return false;
757 }
758 }
759 }