class-wc-customer-order-csv-export-generator.php
33.2 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
<?php
/**
* WooCommerce Customer/Order CSV Export
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@skyverge.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade WooCommerce Customer/Order CSV Export to newer
* versions in the future. If you wish to customize WooCommerce Customer/Order CSV Export for your
* needs please refer to http://docs.woothemes.com/document/ordercustomer-csv-exporter/
*
* @package WC-Customer-Order-CSV-Export/Generator
* @author SkyVerge
* @copyright Copyright (c) 2012-2016, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
defined( 'ABSPATH' ) or exit;
/**
* Customer/Order CSV Export Generator
*
* Converts customer/order data into CSV
*
* @since 3.0
*/
class WC_Customer_Order_CSV_Export_Generator {
/** @var array order IDs or customer IDs */
public $ids;
/** @var array CSV header fields */
public $headers;
/** @var string order export format */
public $order_format;
/** @var resource output stream containing CSV */
private $stream;
/**
* Setup the IDs to export and CSV options
*
* @since 3.0
* @param $ids
* @return \WC_Customer_Order_CSV_Export_Generator
*/
public function __construct( $ids ) {
/**
* Filter the order/customer IDs
*
* @since 3.9.1
* @param array $id, order IDs or customer IDs
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
$this->ids = apply_filters( 'wc_customer_order_csv_export_ids', $ids, $this );
$this->order_format = get_option( 'wc_customer_order_csv_export_order_format' );
/**
* CSV Delimiter.
*
* Filter the delimiter used for the CSV file
*
* @since 3.0.0
* @param string $delimiter, defaults to comma (,)
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
$this->delimiter = apply_filters( 'wc_customer_order_csv_export_delimiter', ',', $this );
/**
* CSV Enclosure.
*
* Filter the enclosure used for the CSV file
*
* @since 3.0.0
* @param string $enclosure, defaults to double quote (")
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
$this->enclosure = apply_filters( 'wc_customer_order_csv_export_enclosure', '"', $this );
// open output buffer to write CSV to
$this->stream = fopen( 'php://output', 'w' );
ob_start();
/**
* CSV BOM (Byte order mark).
*
* Enable adding a BOM to the exported CSV
*
* @since 3.0
* @param bool $enable_bom true to add the BOM, false otherwise. Defaults to false.
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
if ( apply_filters( 'wc_customer_order_csv_export_enable_bom', false, $this ) ) {
fputs( $this->stream, ( chr(0xEF) . chr(0xBB) . chr(0xBF) ) );
}
}
/**
* Get the CSV for orders
*
* @since 3.0
* @return string CSV data
*/
public function get_orders_csv() {
// order headers
$this->headers = $this->get_orders_csv_headers();
// write headers
$this->write( $this->headers );
$order_data = array();
// iterate through order IDs
foreach ( $this->ids as $order_id ) {
// get data for each order
$data = $this->get_orders_csv_row( $order_id );
$order_data[] = $data;
// data can be an array of arrays when each line item is it's own row
$first_element = reset( $data );
if ( is_array( $first_element ) ) {
// iterate through each line item row and write it
foreach( $data as $row ) {
$this->write( $row );
}
} else {
// otherwise simply write the single order row
$this->write( $data );
}
}
/**
* Filter the generated orders CSV
*
* @since 3.8.0
* @param string $csv_data The CSV data
* @param array $order_data An array of the order data to write to to the CSV
* @param array $order_ids The order ids.
* @param string $order_format The order export format.
*/
return apply_filters( 'wc_customer_order_csv_export_get_orders_csv', $this->get_csv(), $order_data, $this->ids, $this->order_format );
}
/**
* Get the column headers for the orders CSV
*
* Note that the headers are keyed in column_key => column_name format so that plugins can control the output
* format using only the column headers and row data is not required to be in the exact same order, as the row data
* is matched on the column key
*
* @since 3.0
* @return array column headers in column_key => column_name format
*/
private function get_orders_csv_headers() {
$column_headers = array(
'order_id' => 'order_id',
'order_number' => 'order_number',
'order_date' => 'date',
'status' => 'status',
'shipping_total' => 'shipping_total',
'shipping_tax_total' => 'shipping_tax_total',
'fee_total' => 'fee_total',
'fee_tax_total' => 'fee_tax_total',
'tax_total' => 'tax_total',
'discount_total' => 'discount_total',
'order_total' => 'order_total',
'refunded_total' => 'refunded_total',
'order_currency' => 'order_currency',
'payment_method' => 'payment_method',
'shipping_method' => 'shipping_method',
'customer_id' => 'customer_id',
'billing_first_name' => 'billing_first_name',
'billing_last_name' => 'billing_last_name',
'billing_company' => 'billing_company',
'billing_email' => 'billing_email',
'billing_phone' => 'billing_phone',
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'billing_postcode' => 'billing_postcode',
'billing_city' => 'billing_city',
'billing_state' => 'billing_state',
'billing_country' => 'billing_country',
'shipping_first_name' => 'shipping_first_name',
'shipping_last_name' => 'shipping_last_name',
'shipping_address_1' => 'shipping_address_1',
'shipping_address_2' => 'shipping_address_2',
'shipping_postcode' => 'shipping_postcode',
'shipping_city' => 'shipping_city',
'shipping_state' => 'shipping_state',
'shipping_country' => 'shipping_country',
'shipping_company' => 'shipping_company',
'customer_note' => 'customer_note',
);
if ( 'default_one_row_per_item' == $this->order_format ) {
$column_headers['item_name'] = 'item_name';
$column_headers['item_sku'] = 'item_sku';
$column_headers['item_quantity'] = 'item_quantity';
$column_headers['item_tax'] = 'item_tax';
$column_headers['item_total'] = 'item_total';
$column_headers['item_refunded'] = 'item_refunded';
$column_headers['item_refunded_qty'] = 'item_refunded_qty';
$column_headers['item_meta'] = 'item_meta';
} else {
$column_headers['line_items'] = 'line_items';
}
$column_headers['shipping_items'] = 'shipping_items';
$column_headers['fee_items'] = 'fee_items';
$column_headers['tax_items'] = 'tax_items';
$column_headers['coupon_items'] = 'coupons';
$column_headers['order_notes'] = 'order_notes';
$column_headers['download_permissions'] = 'download_permissions_granted';
/**
* CSV Order Export Column Headers.
*
* Filter the column headers for the order export
*
* @since 3.0
* @param array $column_headers {
* column headers in key => name format
* to modify the column headers, ensure the keys match these and set your own values
* }
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
return apply_filters( 'wc_customer_order_csv_export_order_headers', $column_headers, $this );
}
/**
* Get the order data for a single CSV row
*
* Note items are keyed according to the column header keys above so these can be modified using
* the provider filter without needing to worry about the array order
*
* @since 3.0
* @param int $order_id the WC_Order ID
* @return array order data in the format key => content
*/
private function get_orders_csv_row( $order_id ) {
$order = wc_get_order( $order_id );
$is_json = 'import' == $this->order_format;
$line_items = $shipping_items = $fee_items = $tax_items = $coupon_items = array();
// get line items
foreach ( $order->get_items() as $item_id => $item ) {
$product = $order->get_product_from_item( $item );
if ( ! is_object( $product ) ) {
$product = new WC_Product( 0 );
}
$item_meta = new WC_Order_Item_Meta( $item );
if ( $is_json ) {
$meta = array();
foreach ( $item_meta->get_formatted( '_' ) as $meta_key => $formatted_meta ) {
$meta[ $formatted_meta['key'] ] = $formatted_meta['value'];
}
} else {
add_filter( 'woocommerce_attribute_label', array( $this, 'escape_reserved_meta_chars' ) );
add_filter( 'woocommerce_order_item_display_meta_value', array( $this, 'escape_reserved_meta_chars' ) );
$meta = $item_meta->display( true, true );
remove_filter( 'woocommerce_attribute_label', array( $this, 'escape_reserved_meta_chars' ) );
remove_filter( 'woocommerce_order_item_display_meta_value', array( $this, 'escape_reserved_meta_chars' ) );
if ( $meta ) {
// replace key-value sperator (': ') with our own - equals sign (=)
$meta = str_replace( ': ', '=', $meta );
// remove any newlines generated by WC_Order_Item_Meta::display()
$meta = str_replace( array( ", \r\n", ", \r", ", \n" ), ',', $meta );
// re-insert colons and newlines
$meta = str_replace( array( '[INSERT_COLON_HERE]', '[INSERT_NEWLINE_HERE]' ), array( ':', "\n" ), $meta );
}
}
$product = $order->get_product_from_item( $item );
$product_id = null;
$product_sku = null;
// Check if the product exists.
if ( is_object( $product ) ) {
$product_id = ( isset( $product->variation_id ) ) ? $product->variation_id : $product->id;
$product_sku = $product->get_sku();
}
if ( $is_json ) {
$line_item = array(
// fields following WC API conventions, see WC_API_Orders
'id' => $item_id,
'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ),
'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], 2 ),
'total' => wc_format_decimal( $order->get_line_total( $item ), 2 ),
'total_tax' => wc_format_decimal( $order->get_line_tax( $item ), 2 ),
'quantity' => (int) $item['qty'],
'name' => $item['name'],
'product_id' => $product_id,
'sku' => $product_sku,
'meta' => $meta,
'tax_data' => isset( $item['line_tax_data'] ) ? maybe_unserialize( $item['line_tax_data'] ) : '',
);
} else {
$line_item = array(
'name' => html_entity_decode( $item['name'], ENT_NOQUOTES, 'UTF-8' ),
'sku' => $product_sku,
'quantity' => (int) $item['qty'],
'total' => wc_format_decimal( $order->get_line_total( $item ), 2 ),
'refunded' => wc_format_decimal( $order->get_total_refunded_for_item( $item_id ), 2 ),
'refunded_qty' => $order->get_qty_refunded_for_item( $item_id ),
'meta' => is_string( $meta ) ? html_entity_decode( $meta, ENT_NOQUOTES, 'UTF-8' ) : $meta,
);
// add line item tax
$line_tax_data = isset( $item['line_tax_data'] ) ? $item['line_tax_data'] : array();
$tax_data = maybe_unserialize( $line_tax_data );
$line_item['tax'] = isset( $tax_data['total'] ) ? wc_format_decimal( wc_round_tax_total( array_sum( (array) $tax_data['total'] ) ), 2 ) : '';
}
/**
* CSV Order Export Line Item.
*
* Filter the individual line item entry for the default export
*
* @since 3.0.6
* @param array $line_item {
* line item data in key => value format
* the keys are for convenience and not used for exporting. Make
* sure to prefix the values with the desired line item entry name
* }
*
* @param array $item WC order item data
* @param WC_Product $product the product
* @param WC_Order $order the order
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
$line_item = apply_filters( 'wc_customer_order_csv_export_order_line_item', $line_item, $item, $product, $order, $this );
if ( ! in_array( $this->order_format, array( 'default_one_row_per_item', 'import' ) ) && is_array( $line_item ) ) {
foreach ( $line_item as $name => $value ) {
$name = $this->escape_reserved_item_chars( $name );
$value = $this->escape_reserved_item_chars( $value );
$line_item[ $name ] = $name . ':' . $value;
}
$line_item = implode( '|', $line_item );
}
if ( $line_item ) {
$line_items[] = $line_item;
}
}
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
$shipping_item = array(
'id' => $shipping_item_id,
'method_id' => $shipping_item['method_id'],
'method_title' => $shipping_item['name'],
'total' => wc_format_decimal( $shipping_item['cost'], 2 ),
'taxes' => isset( $shipping_item['taxes'] ) ? maybe_unserialize( $shipping_item['taxes'] ) : '',
);
if ( $is_json ) {
$shipping_items[] = $shipping_item;
} else {
$shipping_items[] = implode( '|', array(
'method:' . $this->escape_reserved_item_chars( $shipping_item['method_title'] ),
'total:' . $shipping_item['total'],
) );
}
}
// get fee items & total
$fee_total = 0;
$fee_tax_total = 0;
foreach ( $order->get_fees() as $fee_item_id => $fee_item ) {
$fee = array(
'id' => $fee_item_id,
'title' => $fee_item['name'],
'tax_class' => ( ! empty( $fee_item['tax_class'] ) ) ? $fee_item['tax_class'] : null,
'total' => wc_format_decimal( $order->get_line_total( $fee_item ), 2 ),
'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), 2 ),
'tax_data' => isset( $fee_item['line_tax_data'] ) ? maybe_unserialize( $fee_item['line_tax_data'] ) : '',
);
if ( $is_json ) {
if ( null !== $fee['tax_class'] ) {
$fee['taxable'] = true;
}
$fee_items[] = $fee;
} else {
$fee_items[] = implode( '|', array(
'name:' . $this->escape_reserved_item_chars( $fee['title'] ),
'total:' . $fee['total'],
'tax:' . $fee['total_tax'],
) );
}
$fee_total += $fee_item['line_total'];
$fee_tax_total += $fee_item['line_tax'];
}
// get tax items
foreach ( $order->get_tax_totals() as $tax_code => $tax ) {
$tax_item = array(
'id' => $tax->id,
'rate_id' => $tax->rate_id,
'code' => $tax_code,
'title' => $tax->label,
'total' => wc_format_decimal( $tax->amount, 2 ),
'compound' => (bool) $tax->is_compound,
);
if ( $is_json ) {
$tax_items[] = $tax_item;
} else {
$tax_items[] = implode( '|', array(
'code:' . $this->escape_reserved_item_chars( $tax_code ),
'total:' . $tax->amount,
) );
}
}
// add coupons
foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) {
$coupon_item = array(
'id' => $coupon_item_id,
'code' => $coupon_item['name'],
'amount' => wc_format_decimal( $coupon_item['discount_amount'], 2 ),
);
if ( $is_json ) {
$coupon_items[] = $coupon_item;
} else {
$coupon = new WC_Coupon( $coupon_item['code'] );
$coupon_post = get_post( $coupon->id );
$coupon_items[] = implode( '|', array(
'code:' . $this->escape_reserved_item_chars( $coupon_item['code'] ),
'description:' . $this->escape_reserved_item_chars( is_object( $coupon_post ) ? $coupon_post->post_excerpt : '' ),
'amount:' . $coupon_item['amount'],
) );
}
}
$order_data = array(
'order_id' => $order->id,
'order_number' => $order->get_order_number(),
'order_date' => $order->order_date,
'status' => $order->get_status(),
'shipping_total' => $order->get_total_shipping(),
'shipping_tax_total' => wc_format_decimal( $order->get_shipping_tax(), 2 ),
'fee_total' => wc_format_decimal( $fee_total, 2 ),
'fee_tax_total' => wc_format_decimal( $fee_tax_total, 2 ),
'tax_total' => wc_format_decimal( $order->get_total_tax(), 2 ),
'discount_total' => wc_format_decimal( $order->get_total_discount(), 2 ),
'order_total' => wc_format_decimal( $order->get_total(), 2 ),
'refunded_total' => wc_format_decimal( $order->get_total_refunded(), 2 ),
'order_currency' => $order->get_order_currency(),
'payment_method' => $order->payment_method,
'shipping_method' => $order->get_shipping_method(),
'customer_id' => $order->get_user_id(),
'billing_first_name' => $order->billing_first_name,
'billing_last_name' => $order->billing_last_name,
'billing_company' => $order->billing_company,
'billing_email' => $order->billing_email,
'billing_phone' => $order->billing_phone,
'billing_address_1' => $order->billing_address_1,
'billing_address_2' => $order->billing_address_2,
'billing_postcode' => $order->billing_postcode,
'billing_city' => $order->billing_city,
'billing_state' => $order->billing_state,
'billing_country' => $order->billing_country,
'shipping_first_name' => $order->shipping_first_name,
'shipping_last_name' => $order->shipping_last_name,
'shipping_company' => $order->shipping_company,
'shipping_address_1' => $order->shipping_address_1,
'shipping_address_2' => $order->shipping_address_2,
'shipping_postcode' => $order->shipping_postcode,
'shipping_city' => $order->shipping_city,
'shipping_state' => $order->shipping_state,
'shipping_country' => $order->shipping_country,
'customer_note' => $order->customer_note,
'shipping_items' => $is_json && ! empty( $shipping_items ) ? json_encode( $shipping_items ) : implode( ';', $shipping_items ),
'fee_items' => $is_json && ! empty( $fee_items ) ? json_encode( $fee_items ) : implode( ';', $fee_items ),
'tax_items' => $is_json && ! empty( $tax_items ) ? json_encode( $tax_items ) : implode( ';', $tax_items ),
'coupon_items' => $is_json && ! empty( $coupon_items ) ? json_encode( $coupon_items ) : implode( ';', $coupon_items ),
'order_notes' => implode( '|', $this->get_order_notes( $order ) ),
'download_permissions' => $order->download_permissions_granted ? $order->download_permissions_granted : 0,
);
if ( 'default_one_row_per_item' === $this->order_format ) {
$new_order_data = array();
foreach ( $line_items as $item ) {
$order_data['item_name'] = $item['name'];
$order_data['item_sku'] = $item['sku'];
$order_data['item_quantity'] = $item['quantity'];
$order_data['item_tax'] = $item['tax'];
$order_data['item_total'] = $item['total'];
$order_data['item_refunded'] = $item['refunded'];
$order_data['item_refunded_qty'] = $item['refunded_qty'];
$order_data['item_meta'] = $item['meta'];
/**
* CSV Order Export Row for One Row per Item.
*
* Filter the individual row data for the order export
*
* @since 3.3.0
* @param array $order_data {
* order data in key => value format
* to modify the row data, ensure the key matches any of the header keys and set your own value
* }
* @param array $item
* @param \WC_Order $order WC Order object
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
$new_order_data[] = apply_filters( 'wc_customer_order_csv_export_order_row_one_row_per_item', $order_data, $item, $order, $this );
}
$order_data = $new_order_data;
} else {
$order_data['line_items'] = $is_json ? json_encode( $line_items ) : implode( ';', $line_items );
}
/**
* CSV Order Export Row.
*
* Filter the individual row data for the order export
*
* @since 3.0
* @param array $order_data {
* order data in key => value format
* to modify the row data, ensure the key matches any of the header keys and set your own value
* }
* @param \WC_Order $order WC Order object
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
return apply_filters( 'wc_customer_order_csv_export_order_row', $order_data, $order, $this );
}
/**
* Get the order notes for given order
*
* @since 3.0
* @param WC_Order $order
* @return array order notes
*/
private function get_order_notes( $order ) {
$callback = array( 'WC_Comments', 'exclude_order_comments' );
$args = array(
'post_id' => $order->id,
'approve' => 'approve',
'type' => 'order_note'
);
remove_filter( 'comments_clauses', $callback );
$notes = get_comments( $args );
add_filter( 'comments_clauses', $callback );
$order_notes = array();
foreach ( $notes as $note ) {
$order_notes[] = str_replace( array( "\r", "\n" ), ' ', $note->comment_content );
}
return $order_notes;
}
/**
* Escape reserved meta chars in a string (commas and equals signs)
*
* Will also replace colons and newlines with a placeholder, which should
* be replaced later with actual characters.
*
* @since 3.12.0
* @param string $input Input string
* @return string
*/
public function escape_reserved_meta_chars( $input ) {
// commas delimit meta fields, equals sign delimits key-value pairs,
// colons need to be replaced with a placeholder so that we can safely
// replace the key-value separator (colon + space) with our own (equals sign)
$input = str_replace(
array( '=', ',', ':' ),
array( '\=', '\,', '[INSERT_COLON_HERE]' ),
$input
);
// newlines are legal in CSV, but we want to remove the newlines generated
// by WC_Order_Item_Meta::display(), so we replace them with a placeholder temporarily
return str_replace( array( "\r\n", "\r", "\n" ), '[INSERT_NEWLINE_HERE]', $input );
}
/**
* Escape reserved item chars in a string (semicolons, colons and pipes)
*
* @since 3.12.0
* @param string $input Input string
* @return string
*/
private function escape_reserved_item_chars( $input ) {
// colons separate key-value pairs, pipes separate fields/properties,
// and semicolons separate line items themselves
return str_replace(
array( ':', '|', ';' ),
array( '\:', '\|', '\;' ),
$input
);
}
/**
* Get the CSV for customers
*
* @since 3.0
* @return string CSV data
*/
public function get_customers_csv() {
// customer headers
$this->headers = $this->get_customers_csv_headers();
// write headers
$this->write( $this->headers );
$customers = array();
// get customers to export based on orders
foreach ( $this->ids as $order_id ) {
$billing_email = get_post_meta( $order_id, '_billing_email', true );
// skip orders without a billing email
if ( ! $billing_email ) {
continue;
}
$customers[ $order_id ] = $billing_email;
}
// customer date filtering requires a bit of a hack as WP_User_Query does not support the same `date_query`
// arguments as WP_Query (as of WP 3.8.1) and so must be done with raw SQL
add_action( 'pre_user_query', array( $this, 'modify_user_query' ) );
// get customers to export based on role
$users = get_users( array( 'role' => 'customer' ) );
remove_action( 'pre_user_query', array( $this, 'modify_user_query' ) );
foreach ( $users as $user ) {
if ( ! isset( $user->user_email ) ) {
continue;
}
$customers[ 'user_id_' . $user->ID ] = $user->user_email;
}
// ensure each customer has a unique billing email
$customers = array_unique( $customers );
$customer_data = array();
foreach ( $customers as $order_id => $customer_email ) {
// get data for each customer
$data = $this->get_customers_csv_row( $customer_email, $order_id );
$customer_data[] = $data;
// data can be an array of arrays when customer meta is it's own row
$first_element = reset( $data );
if ( is_array( $first_element ) ) {
// iterate through each customer meta row and write it
foreach( $data as $row ) {
$this->write( $row );
}
} else {
// otherwise simply write the single customer row
$this->write( $data );
}
}
/**
* Filter the generated customers CSV
*
* @since 3.8.0
* @param string $csv_data The CSV data
* @param array $customer_data An array of the customer data to write to the CSV
* @param array $customers An associative array of customers in the format array( $order_id => $customer_email ).
*/
return apply_filters( 'wc_customer_order_csv_export_get_customers_csv', $this->get_csv(), $customer_data, $customers );
}
/**
* Modify the WP_User_Query to support filtering on the date the customer was created
*
* @since 3.0.5
* @param WP_User_Query $query
*/
public function modify_user_query( $query ) {
if ( wc_customer_order_csv_export()->get_admin_instance()->customer_export_start_date ) {
$query->query_where .= sprintf( " AND user_registered >= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i' )", esc_sql( wc_customer_order_csv_export()->get_admin_instance()->customer_export_start_date ) );
}
if ( wc_customer_order_csv_export()->get_admin_instance()->customer_export_end_date ) {
$query->query_where .= sprintf( " AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i' )", esc_sql( wc_customer_order_csv_export()->get_admin_instance()->customer_export_end_date ) );
}
}
/**
* Get the column headers for the customers CSV
*
* Note that the headers are keyed in column_key => column_name format so that plugins can control the output
* format using only the column headers and row data is not required to be in the exact same order, as the row data
* is matched on the column key
*
* @since 3.0
* @return array column headers in column_key => column_name format
*/
public function get_customers_csv_headers() {
$column_headers = array(
'customer_id' => 'customer_id',
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'email',
'date_registered' => 'date_registered',
'billing_first_name' => 'billing_first_name',
'billing_last_name' => 'billing_last_name',
'billing_company' => 'billing_company',
'billing_email' => 'billing_email',
'billing_phone' => 'billing_phone',
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'billing_postcode' => 'billing_postcode',
'billing_city' => 'billing_city',
'billing_state' => 'billing_state',
'billing_country' => 'billing_country',
'shipping_first_name' => 'shipping_first_name',
'shipping_last_name' => 'shipping_last_name',
'shipping_company' => 'shipping_company',
'shipping_address_1' => 'shipping_address_1',
'shipping_address_2' => 'shipping_address_2',
'shipping_postcode' => 'shipping_postcode',
'shipping_city' => 'shipping_city',
'shipping_state' => 'shipping_state',
'shipping_country' => 'shipping_country',
);
/**
* CSV Customer Export Column Headers.
*
* Filter the column headers for the customer export
*
* @since 3.0
* @param array $column_headers {
* column headers in key => name format
* to modify the column headers, ensure the keys match these and set your own values
* }
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
return apply_filters( 'wc_customer_order_csv_export_customer_headers', $column_headers, $this );
}
/**
* Get the customer data for a single CSV row
*
* Note items are keyed according to the column header keys above so these can be modified using
* the provider filter without needing to worry about the array order
*
* @since 3.0
* @param string $customer_email the customer's email
* @param int $order_id a valid order ID for the customer
* @return array customer data in the format key => content
*/
private function get_customers_csv_row( $customer_email, $order_id ) {
$user = get_user_by( 'email', $customer_email );
// guest, get info from order
if ( ! $user && is_numeric( $order_id ) ) {
$order = wc_get_order( $order_id );
// create blank user
$user = new stdClass();
// set properties on user
$user->ID = 0;
$user->first_name = $order->billing_first_name;
$user->last_name = $order->billing_last_name;
$user->user_email = $order->billing_email;
$user->user_registered = $order->order_date;
$user->billing_first_name = $order->billing_first_name;
$user->billing_last_name = $order->billing_last_name;
$user->billing_company = $order->billing_company;
$user->billing_email = $order->billing_email;
$user->billing_phone = $order->billing_phone;
$user->billing_address_1 = $order->billing_address_1;
$user->billing_address_2 = $order->billing_address_2;
$user->billing_postcode = $order->billing_postcode;
$user->billing_city = $order->billing_city;
$user->billing_state = $order->billing_state;
$user->billing_country = $order->billing_country;
$user->shipping_first_name = $order->shipping_first_name;
$user->shipping_last_name = $order->shipping_last_name;
$user->shipping_company = $order->shipping_company;
$user->shipping_address_1 = $order->shipping_address_1;
$user->shipping_address_2 = $order->shipping_address_2;
$user->shipping_postcode = $order->shipping_postcode;
$user->shipping_city = $order->shipping_city;
$user->shipping_state = $order->shipping_state;
$user->shipping_country = $order->shipping_country;
}
$customer_data = array(
'customer_id' => $user->ID,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->user_email,
'date_registered' => $user->user_registered,
'billing_first_name' => $user->billing_first_name,
'billing_last_name' => $user->billing_last_name,
'billing_company' => $user->billing_company,
'billing_email' => $user->billing_email,
'billing_phone' => $user->billing_phone,
'billing_address_1' => $user->billing_address_1,
'billing_address_2' => $user->billing_address_2,
'billing_postcode' => $user->billing_postcode,
'billing_city' => $user->billing_city,
'billing_state' => $user->billing_state,
'billing_country' => $user->billing_country,
'shipping_first_name' => $user->shipping_first_name,
'shipping_last_name' => $user->shipping_last_name,
'shipping_company' => $user->shipping_company,
'shipping_address_1' => $user->shipping_address_1,
'shipping_address_2' => $user->shipping_address_2,
'shipping_postcode' => $user->shipping_postcode,
'shipping_city' => $user->shipping_city,
'shipping_state' => $user->shipping_state,
'shipping_country' => $user->shipping_country,
);
/**
* CSV Customer Export Row.
*
* Filter the individual row data for the customer export
*
* @since 3.0
* @param array $customer_data {
* order data in key => value format
* to modify the row data, ensure the key matches any of the header keys and set your own value
* }
* @param \WP_User $user WP User object
* @param int $order_id an order ID for the customer
* @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance
*/
return apply_filters( 'wc_customer_order_csv_export_customer_row', $customer_data, $user, $order_id, $this );
}
/**
* Write the given row to the CSV
*
* This is abstracted so the provided data can be matched to the CSV headers set and the CSV delimiter and
* enclosure can be controlled from a single method
*
* @since 3.0
* @param array $row
*/
private function write( $row ) {
if ( empty( $row ) ) {
return;
}
$data = array();
foreach ( $this->headers as $header_key => $_ ) {
if ( ! isset( $row[ $header_key ] ) ) {
$row[ $header_key ] = '';
}
// strict string comparison, as values like '0' are valid
$value = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : '';
// escape leading equals sign character with a single quote to prevent CSV injections, see http://www.contextis.com/resources/blog/comma-separated-vulnerabilities/
if ( SV_WC_Helper::str_starts_with( $value, '=' ) ) {
$value = "'" . $value;
}
$data[] = $value;
}
fputcsv( $this->stream, $data, $this->delimiter, $this->enclosure );
}
/**
* Return the CSV data by closing the output buffer and stream
*
* @since 3.0
* @return string CSV
*/
private function get_csv() {
$csv = ob_get_clean();
fclose( $this->stream );
/**
* Allow actors to change the generated CSV, such as removing headers.
*
* @since 3.0.6
* @param string $csv - generated CSV file
* @param \WC_Customer_Order_CSV_Export_Generator $this - generator class instance
*/
return apply_filters( 'wc_customer_order_csv_export_generated_csv', $csv, $this );
}
} //end \WC_Customer_Order_CSV_Export_Generator class