PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.2.2
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.2.2
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / libraries / XmlExportWooCommerceOrder.php
wp-all-export / libraries Last commit date
VariableProductTitle 7 years ago .gitkeep 7 years ago WpaeInvalidPhpException.php 7 years ago WpaeInvalidStringException.php 7 years ago WpaeMethodNotFoundException.php 7 years ago WpaePhpInterpreterErrorHandler.php 7 years ago WpaeString.php 7 years ago WpaeTooMuchRecursionException.php 7 years ago WpaeXmlProcessor.php 7 years ago XmlCsvExport.php 7 years ago XmlExportACF.php 7 years ago XmlExportComment.php 7 years ago XmlExportCpt.php 7 years ago XmlExportEngine.php 7 years ago XmlExportFiltering.php 7 years ago XmlExportMediaGallery.php 7 years ago XmlExportTaxonomy.php 7 years ago XmlExportUser.php 7 years ago XmlExportWooCommerce.php 7 years ago XmlExportWooCommerceCoupon.php 7 years ago XmlExportWooCommerceOrder.php 7 years ago XmlGoogleMerchants.php 7 years ago XmlSpec.php 7 years ago
XmlExportWooCommerceOrder.php
1705 lines
1 <?php
2
3 if ( ! class_exists('XmlExportWooCommerceOrder') )
4 {
5 final class XmlExportWooCommerceOrder
6 {
7 public static $is_active = true;
8
9 public static $order_sections = array();
10 public static $order_items_per_line = false;
11 public static $orders_data = null;
12 public static $exportQuery = null;
13
14 private $init_fields = array(
15 array(
16 'name' => 'Order ID',
17 'type' => 'woo_order',
18 'options' => 'order',
19 'label' => 'ID'
20 ),
21 array(
22 'name' => 'Order Key',
23 'type' => 'woo_order',
24 'options' => 'order',
25 'label' => '_order_key'
26 ),
27 array(
28 'name' => 'Title',
29 'type' => 'woo_order',
30 'options' => 'order',
31 'label' => 'post_title'
32 )
33 );
34
35 private $order_core_fields = array();
36
37 public function __construct()
38 {
39 $this->order_core_fields = array('_prices_include_tax', '_customer_ip_address', '_customer_user_agent', '_created_via', '_order_version', '_payment_method', '_cart_discount_tax', '_order_shipping_tax', '_recorded_sales', '_order_stock_reduced', '_recorded_coupon_usage_counts', '_transaction_id');
40
41 if ( ! class_exists('WooCommerce')
42 or ( XmlExportEngine::$exportOptions['export_type'] == 'specific' and ! in_array('shop_order', XmlExportEngine::$post_types) )
43 or ( XmlExportEngine::$exportOptions['export_type'] == 'advanced' and strpos(XmlExportEngine::$exportOptions['wp_query'], 'shop_order') === false ) ) {
44 self::$is_active = false;
45 return;
46 }
47
48 self::$is_active = true;
49
50 if ( empty(PMXE_Plugin::$session) ) // if cron execution
51 {
52 $id = $_GET['export_id'];
53 $export = new PMXE_Export_Record();
54 $export->getById($id);
55 if ( ! $export->isEmpty() and $export->options['export_to'] == 'csv'){
56 $this->init_additional_data();
57 }
58 }
59 else
60 {
61 $this->init_additional_data();
62 }
63
64 add_filter("wp_all_export_available_sections", array( &$this, "filter_available_sections" ), 10, 1);
65 add_filter("wp_all_export_csv_rows", array( &$this, "filter_csv_rows"), 10, 2);
66 add_filter("wp_all_export_init_fields", array( &$this, "filter_init_fields"), 10, 1);
67
68 self::$order_sections = $this->available_sections();
69
70 }
71
72 // [FILTERS]
73
74 /**
75 *
76 * Filter Init Fields
77 *
78 */
79 public function filter_init_fields($init_fields){
80 return $this->init_fields;
81 }
82
83 /**
84 *
85 * Filter Sections in Available Data
86 *
87 */
88 public function filter_available_sections($sections){
89 return array();
90 }
91
92 // [\FILTERS]
93
94 public function init( & $existing_meta_keys = array() ){
95
96 if ( ! self::$is_active ) return;
97
98 if ( ! empty($existing_meta_keys) )
99 {
100 foreach (self::$order_sections as $slug => $section) :
101
102 foreach ($section['meta'] as $cur_meta_key => $cur_meta_label)
103 {
104 foreach ($existing_meta_keys as $key => $record_meta_key)
105 {
106 if ( $record_meta_key == $cur_meta_key )
107 {
108 unset($existing_meta_keys[$key]);
109 break;
110 }
111 }
112 }
113
114 endforeach;
115
116 foreach ( $this->order_core_fields as $core_field ):
117
118 foreach ($existing_meta_keys as $key => $record_meta_key)
119 {
120 if ( $record_meta_key == $core_field )
121 {
122 unset($existing_meta_keys[$key]);
123 break;
124 }
125 }
126
127 endforeach;
128
129 foreach ($existing_meta_keys as $key => $record_meta_key)
130 {
131 self::$order_sections['cf']['meta'][$record_meta_key] = array(
132 'name' => $record_meta_key,
133 'label' => $record_meta_key,
134 'options' => '',
135 'type' => 'cf'
136 );
137 }
138 }
139
140 global $wpdb;
141 $table_prefix = $wpdb->prefix;
142
143 $product_data = $this->available_order_default_product_data();
144
145 $meta_keys = $wpdb->get_results("SELECT DISTINCT {$table_prefix}woocommerce_order_itemmeta.meta_key FROM {$table_prefix}woocommerce_order_itemmeta");
146 if ( ! empty($meta_keys)){
147 foreach ($meta_keys as $meta_key) {
148 if (strpos($meta_key->meta_key, "pa_") !== 0 and empty(self::$order_sections['cf']['meta'][$meta_key->meta_key]) and empty($product_data[$meta_key->meta_key]))
149 self::$order_sections['other']['meta'][$meta_key->meta_key] = $this->fix_titles(array(
150 'name' => $meta_key->meta_key,
151 'label' => $meta_key->meta_key,
152 'options' => 'items',
153 'type' => 'woo_order'
154 ));
155 }
156 }
157
158 foreach ( $this->order_core_fields as $core_field ):
159
160 self::$order_sections['other']['meta'][$core_field] = $this->fix_titles(array(
161 'name' => $core_field,
162 'label' => $core_field,
163 'options' => '',
164 'type' => 'cf'
165 ));
166
167 endforeach;
168 }
169
170 /**
171 *
172 * Helper method to fix fields title
173 *
174 */
175 protected function fix_titles($field)
176 {
177 if (is_array($field))
178 {
179 $field['name'] = $this->fix_title($field['name']);
180 }
181 else
182 {
183 $field = $this->fix_title($field);
184 }
185 return $field;
186 }
187 /**
188 *
189 * Helper method to fix single title
190 *
191 */
192 protected function fix_title($title)
193 {
194 $uc_title = ucwords(trim(str_replace("_", " ", $title)));
195
196 return stripos($uc_title, "width") === false ? str_ireplace(array('id', 'url', 'sku'), array('ID', 'URL', 'SKU'), $uc_title) : $uc_title;
197 }
198
199 public function init_additional_data(){
200
201 if ( ! self::$is_active || empty(XmlExportEngine::$exportQuery)) return;
202
203 global $wpdb;
204
205 $table_prefix = $wpdb->prefix;
206
207 $in_orders = preg_replace("%(SQL_CALC_FOUND_ROWS|LIMIT.*)%", "", XmlExportEngine::$exportQuery->request);
208 $in_orders = str_replace("{$table_prefix}posts.*", "{$table_prefix}posts.ID", $in_orders);
209
210 if ( ! empty($in_orders) ){
211
212 if ( empty(self::$orders_data['line_items_max_count']) ){
213 self::$orders_data['line_items_max_count'] = $wpdb->get_var($wpdb->prepare("SELECT max(cnt) as line_items_count FROM (
214 SELECT order_id, COUNT(*) as cnt FROM {$table_prefix}woocommerce_order_items
215 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") GROUP BY order_id) AS T3", 'line_item'));
216 }
217
218 if ( empty(self::$orders_data['taxes'])){
219 self::$orders_data['taxes'] = $wpdb->get_results($wpdb->prepare("SELECT order_item_id, order_id, order_item_name FROM {$table_prefix}woocommerce_order_items
220 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") GROUP BY order_item_name", 'tax'));
221 }
222
223 if ( empty(self::$orders_data['coupons'])){
224 self::$orders_data['coupons'] = $wpdb->get_results($wpdb->prepare("SELECT order_item_id, order_id, order_item_name FROM {$table_prefix}woocommerce_order_items
225 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") GROUP BY order_item_name", 'coupon'));
226 }
227
228 if ( empty(self::$orders_data['fees'])){
229 self::$orders_data['fees'] = $wpdb->get_results($wpdb->prepare("SELECT order_item_id, order_id, order_item_name FROM {$table_prefix}woocommerce_order_items
230 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") GROUP BY order_item_name", 'fee'));
231 }
232
233 if ( empty(self::$orders_data['variations'])){
234 self::$orders_data['variations'] = $wpdb->get_results($wpdb->prepare("SELECT meta_key FROM {$table_prefix}woocommerce_order_itemmeta
235 WHERE {$table_prefix}woocommerce_order_itemmeta.meta_key LIKE %s AND {$table_prefix}woocommerce_order_itemmeta.order_item_id IN (
236 SELECT {$table_prefix}woocommerce_order_items.order_item_id FROM {$table_prefix}woocommerce_order_items
237 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") ) GROUP BY meta_key", 'pa_%', 'line_item'));
238 }
239
240 if ( ! empty(PMXE_Plugin::$session) )
241 {
242 PMXE_Plugin::$session->set('orders_data', self::$orders_data);
243 PMXE_Plugin::$session->save_data();
244 }
245 }
246 }
247
248 private $order_items = null;
249 private $order_taxes = null;
250 private $order_shipping = null;
251 private $order_coupons = null;
252 private $order_surcharge = null;
253 private $order_refunds = null;
254 private $__total_fee_amount = null;
255 private $__coupons_used = null;
256 private $order_id = null;
257
258 // csv headers
259 private $woo = array();
260 private $woo_order = array();
261 private $acfs = array();
262 private $taxes = array();
263 private $attributes = array();
264 private $articles = array();
265 private $exclude_from_filling = array();
266
267 protected function prepare_export_data( $record, $options, $elId, $preview ){
268
269 // an array with data to export
270 $data = array(
271 'items' => array(),
272 'taxes' => array(),
273 'shipping' => array(),
274 'coupons' => array(),
275 'surcharge' => array()
276 );
277
278 global $wpdb;
279 $table_prefix = $wpdb->prefix;
280
281 $implode_delimiter = XmlExportEngine::$implode;
282
283 if ( empty($this->order_id) or $this->order_id != $record->ID)
284 {
285 $this->__coupons_used = array();
286
287 $this->order_id = $record->ID;
288
289 $all_order_items = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_items WHERE order_id = {$record->ID}");
290
291 if ( ! empty($all_order_items) )
292 {
293 foreach ($all_order_items as $item)
294 {
295 switch ($item->order_item_type)
296 {
297 case 'line_item':
298 $this->order_items[] = $item;
299 break;
300 case 'tax':
301 $this->order_taxes[] = $item;
302 break;
303 case 'shipping':
304 $this->order_shipping[] = $item;
305 break;
306 case 'coupon':
307 $this->order_coupons[] = $item;
308 break;
309 case 'fee':
310 $this->order_surcharge[] = $item;
311 break;
312 }
313 }
314 }
315
316 $this->order_refunds = $wpdb->get_results("SELECT * FROM {$table_prefix}posts WHERE post_parent = {$record->ID} AND post_type = 'shop_order_refund'");
317 }
318
319 if ( ! empty($options['cc_value'][$elId]) ){
320
321 $is_items_in_list = false;
322 $is_item_data_in_list = false;
323 foreach ($options['ids'] as $ID => $value)
324 {
325 if ($options['cc_options'][$ID] == 'items')
326 {
327 $is_items_in_list = true;
328 }
329 if ( strpos($options['cc_label'][$ID], "item_data__") !== false )
330 {
331 $is_item_data_in_list = true;
332 }
333 }
334
335 $fieldSnipped = ( ! empty($options['cc_php'][$elId]) and ! empty($options['cc_code'][$elId]) ) ? $options['cc_code'][$elId] : false;
336
337 if ( ! $is_items_in_list and $is_item_data_in_list )
338 {
339 if ( ! empty($this->order_items)){
340
341 foreach ($this->order_items as $n => $order_item) {
342
343 $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item->order_item_id}", ARRAY_A);
344
345 $item_data = array();
346
347 foreach ($options['ids'] as $subID => $subvalue)
348 {
349 if ( strpos($options['cc_label'][$subID], 'item_data__') !== false )
350 {
351 $product_id = '';
352 $variation_id = '';
353 foreach ($meta_data as $meta) {
354 if ($meta['meta_key'] == '_variation_id' and ! empty($meta['meta_value'])){
355 $variation_id = $meta['meta_value'];
356 }
357 if ($meta['meta_key'] == '_product_id' and ! empty($meta['meta_value'])){
358 $product_id = $meta['meta_value'];
359 }
360 }
361
362 $_product_id = empty($variation_id) ? $product_id : $variation_id;
363
364 $_product = get_post($_product_id);
365
366 // do not export anything if product doesn't exist
367 if ( ! empty($_product) )
368 {
369 $item_add_data = XmlExportCpt::prepare_data( $_product, XmlExportEngine::$exportOptions, false, $this->acfs, $this->woo, $this->woo_order, ",", $preview, true, $subID );
370
371 if ( ! empty($item_add_data))
372 {
373 foreach ($item_add_data as $item_add_data_key => $item_add_data_value) {
374 if ( ! isset($item_data[$item_add_data_key])) $item_data[$item_add_data_key] = $item_add_data_value;
375 }
376 }
377 }
378 }
379 }
380
381 if ( ! empty($item_data)) $data['items'][] = $item_data;
382
383 }
384
385 $this->order_items = null;
386 }
387 }
388
389 switch ($options['cc_options'][$elId]) {
390
391 case 'order':
392 case 'customer':
393
394 $data[$options['cc_name'][$elId]] = ( strpos($options['cc_value'][$elId], "_") === 0 ) ? get_post_meta($record->ID, $options['cc_value'][$elId], true) : $record->{$options['cc_value'][$elId]};
395
396 switch ($options['cc_value'][$elId]){
397 case 'post_title':
398 $data[$options['cc_name'][$elId]] = str_replace("&ndash;", '-', $data[$options['cc_name'][$elId]]);
399 break;
400 case 'post_date':
401 $data[$options['cc_name'][$elId]] = prepare_date_field_value($options['cc_settings'][$elId], get_post_time('U', true, $record->ID), "Ymd");
402 break;
403 case '_completed_date':
404 $_completed_date = get_post_meta($record->ID, '_completed_date', true);
405 $_completed_date_unix = empty($_completed_date) ? '' : strtotime($_completed_date);
406 $data[$options['cc_name'][$elId]] = empty($_completed_date_unix) ? '' : prepare_date_field_value($options['cc_settings'][$elId], $_completed_date_unix, "Ymd");
407 break;
408 case '_customer_user_email':
409 $customer_user_id = get_post_meta($record->ID, '_customer_user', true);
410 if ( $customer_user_id ){
411 $user = get_user_by( 'id', $customer_user_id );
412 if ($user){
413 $data[$options['cc_name'][$elId]] = $user->user_email;
414 }
415 }
416 if (empty($data[$options['cc_name'][$elId]])) $data[$options['cc_name'][$elId]] = get_post_meta($record->ID, '_billing_email', true);
417 break;
418 }
419
420 $data[$options['cc_name'][$elId]] = pmxe_filter( $data[$options['cc_name'][$elId]], $fieldSnipped);
421
422 break;
423 }
424
425 }
426
427 return $data;
428 }
429
430 private $additional_articles = array();
431
432 public function export_csv( & $article, & $titles, $record, $options, $elId, $preview ){
433
434 if ( ! self::$is_active ) return;
435
436 $data_to_export = $this->prepare_export_data( $record, $options, $elId, $preview );
437
438 $implode_delimiter = XmlExportEngine::$implode;
439
440 foreach ($data_to_export as $key => $data) {
441
442 if ( in_array($key, array('items', 'taxes', 'shipping', 'coupons', 'surcharge', 'refunds')) )
443 {
444 if ( ! empty($data))
445 {
446 if ( $key == 'items' and ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom'))
447 {
448 foreach ($data as $item) {
449 $additional_article = array();
450 if ( ! empty($item) ){
451 foreach ($item as $item_key => $item_value) {
452 $final_key = preg_replace("%\s#\d*%", "", $item_key);
453 $additional_article[$final_key] = $item_value;
454 if ( ! empty($this->exclude_from_filling[$item_key])){
455 unset($this->exclude_from_filling[$item_key]);
456 $this->exclude_from_filling[$final_key] = 1;
457 }
458 }
459 }
460
461 if ( ! empty($additional_article) )
462 {
463 if ( empty($this->additional_articles) )
464 {
465 foreach ($additional_article as $item_key => $item_value) {
466 $article[$item_key] = $item_value;
467 }
468 }
469 $this->additional_articles[] = $additional_article;
470 }
471 }
472 }
473 else
474 {
475 foreach ($data as $n => $item)
476 {
477 if ( ! empty($item))
478 {
479 foreach ($item as $item_key => $item_value)
480 {
481 $final_key = (strpos($item_key, "#") === false and ! in_array($key, array('taxes', 'shipping', 'coupons', 'surcharge', 'refunds'))) ? $item_key . " #" . ($n + 1) : $item_key;
482
483 if ( ! isset($article[$final_key]))
484 {
485 $article[$final_key] = $item_value;
486 }
487 else
488 {
489 $article[$final_key] .= $implode_delimiter . $item_value;
490 }
491 // if ( ! in_array($final_key, $titles) ) $titles[] = $final_key;
492 }
493 }
494 }
495 }
496 }
497 }
498 else
499 {
500 // $article[$key] = $data;
501 wp_all_export_write_article( $article, $key, $data );
502 // if ( ! in_array($key, $titles) ) $titles[] = $key;
503 }
504 }
505 }
506
507 public function filter_csv_rows($articles, $options){
508
509 if ( ! empty($this->additional_articles) and ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom') )
510 {
511 $base_article = $articles[count($articles) - 1];
512 array_shift($this->additional_articles);
513 if ( ! empty($this->additional_articles ) ){
514 foreach ($this->additional_articles as $article) {
515 if ($options['order_item_fill_empty_columns'] and $options['export_to'] == 'csv')
516 {
517 if (!empty($this->exclude_from_filling)){
518 foreach ($this->exclude_from_filling as $key => $value) {
519 $article[$key] = isset($article[$key]) ? $article[$key] : '';
520 }
521 }
522 foreach ($article as $key => $value) {
523 unset($base_article[$key]);
524 }
525 $articles[] = @array_merge($base_article, $article);
526 }
527 else
528 {
529 $articles[] = $article;
530 }
531 }
532 $this->additional_articles = array();
533 }
534 }
535
536 return $articles;
537 }
538
539 public function get_element_header( & $headers, $options, $element_key ){
540
541 switch ($options['cc_value'][$element_key])
542 {
543 // Rate Code (per tax)
544 case 'tax_order_item_name':
545 // Rate Percentage (per tax)
546 case 'tax_rate':
547 // Amount (per tax)
548 case 'tax_amount':
549
550 if ( ! empty(self::$orders_data['taxes']))
551 {
552 foreach ( self::$orders_data['taxes'] as $tax) {
553 // $friendly_name = str_replace("per tax", $this->get_rate_friendly_name($tax->order_item_id), $options['cc_name'][$element_key]);
554 $friendly_name = str_replace(" (per tax)", "", $options['cc_name'][$element_key]);
555 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
556 }
557 }
558 else{
559 $friendly_name = str_replace(" (per tax)", "", $options['cc_name'][$element_key]);
560 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
561 }
562
563 break;
564 // Discount Amount (per coupon)
565 case 'discount_amount':
566
567 if ( ! empty(self::$orders_data['coupons']))
568 {
569 foreach ( self::$orders_data['coupons'] as $coupon) {
570 // $friendly_name = str_replace("per coupon", $coupon->order_item_name, $options['cc_name'][$element_key]);
571 $friendly_name = str_replace("(per coupon)", "", $options['cc_name'][$element_key]);
572 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
573 if ( ! in_array("Coupon Code", $headers)) $headers[] = "Coupon Code";
574 }
575 }
576 else{
577 $friendly_name = str_replace("(per coupon)", "", $options['cc_name'][$element_key]);
578 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
579 if ( ! in_array("Coupon Code", $headers)) $headers[] = "Coupon Code";
580 }
581
582 break;
583 // Fee Amount (per surcharge)
584 case 'fee_line_total':
585
586 if ( ! empty(self::$orders_data['fees']))
587 {
588 foreach ( self::$orders_data['fees'] as $fee) {
589 // $friendly_name = str_replace("Amount (per surcharge)", "(" . $fee->order_item_name . ")", $options['cc_name'][$element_key]);
590 $friendly_name = str_replace(" (per surcharge)", "", $options['cc_name'][$element_key]);
591 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
592 if ( ! in_array("Fee Name", $headers)) $headers[] = "Fee Name";
593 }
594 }
595 else{
596 $friendly_name = str_replace(" (per surcharge)", "", $options['cc_name'][$element_key]);
597 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
598 if ( ! in_array("Fee Name", $headers)) $headers[] = "Fee Name";
599 }
600
601 break;
602
603 // Product Variation Details
604 case '__product_variation':
605
606 if ( ! empty(self::$orders_data['line_items_max_count']) and ! empty(self::$orders_data['variations']))
607 {
608 if ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom'){
609 foreach ( self::$orders_data['variations'] as $variation) {
610 $friendly_name = $options['cc_name'][$element_key] . " (" . sanitize_title(str_replace("pa_", "", $variation->meta_key)) . ")";
611 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
612 }
613 }
614 else{
615 for ($i = 1; $i <= self::$orders_data['line_items_max_count']; $i++){
616 foreach ( self::$orders_data['variations'] as $variation) {
617 $friendly_name = $options['cc_name'][$element_key] . " #" . $i . " (" . sanitize_title(str_replace("pa_", "", $variation->meta_key)) . ")";
618 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
619 }
620 }
621 }
622 }
623
624 break;
625
626 default:
627
628 switch ($options['cc_options'][$element_key])
629 {
630 // Order's product basic data headers
631 case 'items':
632
633 if ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom')
634 {
635 if ( ! in_array($options['cc_name'][$element_key], $headers)) $headers[] = $options['cc_name'][$element_key];
636 }
637 else
638 {
639 if ( ! empty(self::$orders_data['line_items_max_count'])){
640 for ($i = 1; $i <= self::$orders_data['line_items_max_count']; $i++){
641 $friendly_name = $options['cc_name'][$element_key] . " #" . $i;
642 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
643 }
644 }
645 }
646
647 break;
648
649 default:
650
651 // Order's product advanced data headers
652 if ( strpos($options['cc_label'][$element_key], 'item_data__') !== false)
653 {
654 $element_label = str_replace("item_data__", "", $options['cc_label'][$element_key]);
655
656 $element_headers = array();
657
658 $element_name = ( ! empty($options['cc_name'][$element_key]) ) ? $options['cc_name'][$element_key] : 'untitled_' . $element_key;
659
660 switch ($options['cc_type'][$element_key])
661 {
662 case 'woo':
663
664 XmlExportEngine::$woo_export->get_element_header( $element_headers, $options, $element_key );
665
666 break;
667
668 case 'acf':
669
670 if ( ! empty($this->acfs) ){
671 $single_acf_field = array_shift($this->acfs);
672 if ( is_array($single_acf_field))
673 {
674 foreach ($single_acf_field as $acf_header) {
675 if ( ! in_array($acf_header, $element_headers)) $element_headers[] = $acf_header;
676 }
677 }
678 else
679 {
680 if ( ! in_array($single_acf_field, $element_headers)) $element_headers[] = $single_acf_field;
681 }
682 }
683
684 break;
685
686 default:
687
688 if ( ! in_array($element_name, $element_headers))
689 {
690 $element_headers[] = $element_name;
691 }
692 else
693 {
694 $is_added = false;
695 $i = 0;
696 do
697 {
698 $new_element_name = $element_name . '_' . md5($i);
699
700 if ( ! in_array($new_element_name, $element_headers) )
701 {
702 $element_headers[] = $new_element_name;
703 $is_added = true;
704 }
705
706 $i++;
707 }
708 while ( ! $is_added );
709 }
710
711 // $element_headers[] = $element_name;
712
713 break;
714 }
715
716 if ( ! empty($element_headers) )
717 {
718 foreach ($element_headers as $header)
719 {
720 if ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom')
721 {
722 if ( ! in_array($header, $headers)) $headers[] = $header;
723 }
724 else
725 {
726 if ( ! empty(self::$orders_data['line_items_max_count'])){
727 for ($i = 1; $i <= self::$orders_data['line_items_max_count']; $i++){
728 $friendly_name = $header . " #" . $i;
729 if ( ! in_array($friendly_name, $headers)) $headers[] = $friendly_name;
730 }
731 }
732 }
733 }
734 }
735 }
736 else
737 {
738 if ( ! in_array($options['cc_name'][$element_key], $headers))
739 {
740 $headers[] = $options['cc_name'][$element_key];
741 }
742 else
743 {
744 $is_added = false;
745 $i = 0;
746 do
747 {
748 $new_element_name = $options['cc_name'][$element_key] . '_' . md5($i);
749
750 if ( ! in_array($new_element_name, $headers) )
751 {
752 $headers[] = $new_element_name;
753 $is_added = true;
754 }
755
756 $i++;
757 }
758 while ( ! $is_added );
759 }
760 }
761
762 break;
763 }
764
765 break;
766
767 }
768
769 }
770
771 public function get_rate_friendly_name( $order_item_id ){
772
773 global $wpdb;
774 $table_prefix = $wpdb->prefix;
775
776 $rate_details = null;
777 $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item_id}", ARRAY_A);
778 foreach ($meta_data as $meta) {
779 if ($meta['meta_key'] == 'rate_id'){
780 $rate_id = $meta['meta_value'];
781 $rate_details = $wpdb->get_row("SELECT * FROM {$table_prefix}woocommerce_tax_rates WHERE tax_rate_id = {$rate_id}");
782 break;
783 }
784 }
785
786 return $rate_details ? $rate_details->tax_rate_name : '';
787
788 }
789
790 public function export_xml( & $xmlWriter, $record, $options, $elId, $preview ){
791
792 if ( ! self::$is_active ) return;
793
794 $data_to_export = $this->prepare_export_data( $record, $options, $elId, $preview );
795
796 foreach ($data_to_export as $key => $data) {
797
798 if ( in_array($key, array('items', 'taxes', 'shipping', 'coupons', 'surcharge', 'refunds')) )
799 {
800 if ( ! empty($data)){
801 $xmlWriter->startElement('Order' . ucfirst($key));
802 foreach ($data as $item) {
803 if ( ! empty($item)){
804 $xmlWriter->startElement(preg_replace("%(s|es)$%", "", ucfirst($key)));
805 foreach ($item as $item_key => $item_value) {
806 $element_name_ns = '';
807 $element_name = str_replace("-", "_", preg_replace('/[^a-z0-9:_]/i', '', preg_replace("%#\d*%", "", $item_key)));
808 if (strpos($element_name, ":") !== false)
809 {
810 $element_name_parts = explode(":", $element_name);
811 $element_name_ns = (empty($element_name_parts[0])) ? '' : $element_name_parts[0];
812 $element_name = (empty($element_name_parts[1])) ? 'untitled_' . $ID : $element_name_parts[1];
813 }
814 $xmlWriter->beginElement($element_name_ns, $element_name, null);
815 $xmlWriter->writeData($item_value, $element_name);
816 $xmlWriter->closeElement();
817 // $xmlWriter->writeElement(str_replace("-", "_", sanitize_title(preg_replace("%#\d%", "", $item_key))), $item_value);
818 }
819 $xmlWriter->closeElement();
820 }
821 }
822 $xmlWriter->closeElement();
823 }
824 }
825 else
826 {
827 $element_name_ns = '';
828 $element_name = str_replace("-", "_", preg_replace('/[^a-z0-9:_]/i', '', $key));
829 if (strpos($element_name, ":") !== false)
830 {
831 $element_name_parts = explode(":", $element_name);
832 $element_name_ns = (empty($element_name_parts[0])) ? '' : $element_name_parts[0];
833 $element_name = (empty($element_name_parts[1])) ? 'untitled_' . $ID : $element_name_parts[1];
834 }
835
836 $xmlWriter = apply_filters('wp_all_export_add_before_element', $xmlWriter, $element_name, XmlExportEngine::$exportID, $record->ID);
837
838 $xmlWriter->beginElement($element_name_ns, $element_name, null);
839 $xmlWriter->writeData($data, $element_name);
840 $xmlWriter->closeElement();
841
842 $xmlWriter = apply_filters('wp_all_export_add_after_element', $xmlWriter, $element_name, XmlExportEngine::$exportID, $record->ID);
843 }
844 }
845 }
846
847 public static function prepare_child_exports( $export, $is_cron = false )
848 {
849 $queue_exports = array();
850
851 $exportList = new PMXE_Export_List();
852
853 global $wpdb;
854
855 $table_prefix = $wpdb->prefix;
856 $pmxe_prefix = PMXE_Plugin::getInstance()->getTablePrefix();
857
858 $in_orders = $wpdb->prepare("SELECT DISTINCT post_id FROM {$pmxe_prefix}posts WHERE export_id = %d AND iteration = %d", $export->id, $export->iteration - 1);
859
860 foreach ($exportList->getBy('parent_id', $export->id)->convertRecords() as $child_export)
861 {
862
863 $whereClause = "";
864
865 switch ($child_export->export_post_type)
866 {
867 case 'product':
868
869 if ( $export->options['order_include_poducts'])
870 {
871 $queue_exports[] = $child_export->id;
872
873 if ( ! $export->options['order_include_all_poducts'] )
874 {
875
876 $in_products = $wpdb->prepare("SELECT order_item_meta.meta_value as product_id FROM {$table_prefix}posts as posts INNER JOIN {$pmxe_prefix}posts AS order_export ON posts.ID = post_id INNER JOIN {$table_prefix}woocommerce_order_items AS order_items ON posts.ID = order_id INNER JOIN {$table_prefix}woocommerce_order_itemmeta AS order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id WHERE order_export.export_id = %d AND order_export.iteration = %d AND order_items.order_item_type = 'line_item' AND order_item_meta.meta_key = '_product_id' GROUP BY product_id", $export->id, $export->iteration - 1);
877
878 $whereClause = " AND ({$table_prefix}posts.ID IN (". $in_products .") OR {$table_prefix}posts.post_parent IN (". $in_products ."))";
879
880 }
881 }
882
883 break;
884
885 case 'shop_coupon':
886
887 if ( $export->options['order_include_coupons'])
888 {
889 $queue_exports[] = $child_export->id;
890
891 if ( ! $export->options['order_include_all_coupons'] )
892 {
893 $whereClause = " AND {$table_prefix}posts.post_title IN (". $wpdb->prepare("SELECT order_item_name FROM {$table_prefix}woocommerce_order_items
894 WHERE {$table_prefix}woocommerce_order_items.order_item_type = %s AND {$table_prefix}woocommerce_order_items.order_id IN (". $in_orders .") GROUP BY order_item_name", 'coupon') .")";
895 }
896 }
897
898 break;
899
900 case 'shop_customer':
901
902 if ( $export->options['order_include_customers'])
903 {
904 $queue_exports[] = $child_export->id;
905
906 if ( ! $export->options['order_include_all_customers'] )
907 {
908 $whereClause = " AND {$table_prefix}users.ID IN (" . $wpdb->prepare("SELECT meta_value FROM {$table_prefix}postmeta WHERE meta_key = %s AND post_id IN (". $in_orders .") GROUP BY meta_value", "_customer_user") . ")";
909 }
910 }
911
912 break;
913
914 default:
915 # code...
916 break;
917 }
918 $child_export_options = $child_export->options;
919 $child_export_options['whereclause'] = $whereClause;
920 $child_export->set(array(
921 'triggered' => $is_cron ? 1 : 0,
922 'processing' => 0,
923 'exported' => 0,
924 'executing' => $is_cron ? 0 : 1,
925 'canceled' => 0,
926 'options' => $child_export_options
927 ))->save();
928 }
929 return $queue_exports;
930 }
931
932 public function get_fields_options( &$fields, $field_keys = array() ){
933
934 if ( ! self::$is_active ) return;
935
936 foreach (self::$order_sections as $slug => $section) :
937 if ( ! empty($section['meta'])):
938 foreach ($section['meta'] as $cur_meta_key => $field) {
939
940 $field_key = (is_array($field)) ? $field['name'] : $field;
941
942 if ( ! in_array($field_key, $field_keys) ) continue;
943
944 $fields['ids'][] = 1;
945 $fields['cc_label'][] = (is_array($field)) ? $field['label'] : $cur_meta_key;
946 $fields['cc_php'][] = '';
947 $fields['cc_code'][] = '';
948 $fields['cc_sql'][] = '';
949 $fields['cc_options'][] = (is_array($field)) ? $field['options'] : $slug;
950 $fields['cc_type'][] = (is_array($field)) ? $field['type'] : 'woo_order';
951 $fields['cc_value'][] = (is_array($field)) ? $field['label'] : $cur_meta_key;
952 $fields['cc_name'][] = $field_key;
953 $fields['cc_settings'][] = '';
954 }
955
956 endif;
957 if ( ! empty($section['additional']) )
958 {
959 foreach ($section['additional'] as $sub_slug => $sub_section)
960 {
961 foreach ($sub_section['meta'] as $field) {
962
963 $field_key = (is_array($field)) ? $field['name'] : $field;
964
965 if ( ! in_array($field_key, $field_keys) ) continue;
966
967 $fields['ids'][] = 1;
968 $fields['cc_label'][] = 'item_data__' . ((is_array($field)) ? $field['label'] : $field);
969 $fields['cc_php'][] = '';
970 $fields['cc_code'][] = '';
971 $fields['cc_sql'][] = '';
972 $fields['cc_options'][] = 'item_data';
973 $fields['cc_type'][] = (is_array($field)) ? $field['type'] : $sub_slug;
974 $fields['cc_value'][] = 'item_data__' . ((is_array($field)) ? $field['label'] : $field);
975 $fields['cc_name'][] = $field_key;
976 $fields['cc_settings'][] = '';
977 }
978 }
979 }
980 endforeach;
981
982 }
983
984 public function render( & $i ){
985
986 if ( ! self::$is_active ) return;
987
988 foreach (self::$order_sections as $slug => $section) :
989 if ( ! empty($section['meta']) or ! empty($section['additional']) ):
990 ?>
991 <p class="wpae-available-fields-group"><?php echo $section['title']; ?><span class="wpae-expander">+</span></p>
992 <div class="wpae-custom-field">
993 <?php if ( ! in_array($slug, array('order', 'customer', 'cf', 'other'))) : ?>
994 <div class="wpallexport-free-edition-notice">
995 <a class="upgrade_link" target="_blank" href="https://www.wpallimport.com/checkout/?edd_action=add_to_cart&download_id=118611&edd_options%5Bprice_id%5D=1&utm_source=wordpress.org&utm_medium=wooco+orders&utm_campaign=free+wp+all+export+plugin"><?php _e('Upgrade to the Pro edition of WP All Export to Export Order Data','wp_all_export_plugin');?></a>
996 </div>
997 <?php endif; ?>
998 <ul>
999 <?php if ( ! empty($section['meta']) ): ?>
1000 <li <?php if ( ! in_array($slug, array('order', 'customer', 'cf', 'other'))) : ?>class="wpallexport_disabled"<?php endif; ?>>
1001 <div class="default_column" rel="">
1002 <label class="wpallexport-element-label"><?php echo __("All", "wp_all_export_plugin") . ' ' . $section['title'] . ' ' . __("Data", "wp_all_export_plugin"); ?></label>
1003 <input type="hidden" name="rules[]" value="pmxe_<?php echo $slug;?>"/>
1004 </div>
1005 </li>
1006 <?php
1007 foreach ($section['meta'] as $cur_meta_key => $field) {
1008 $is_auto_field = ( ! empty($field['auto']) or XmlExportEngine::$is_auto_generate_enabled and 'specific' != $this->post['export_type']);
1009 ?>
1010 <li class="pmxe_<?php echo $slug; ?> <?php if ( ! in_array($slug, array('order', 'customer', 'cf', 'other'))) : ?>wpallexport_disabled<?php endif;?>">
1011 <div class="custom_column" rel="<?php echo ($i + 1);?>">
1012 <label class="wpallexport-xml-element"><?php echo (is_array($field)) ? $field['name'] : $field; ?></label>
1013 <input type="hidden" name="ids[]" value="1"/>
1014 <input type="hidden" name="cc_label[]" value="<?php echo (is_array($field)) ? $field['label'] : $cur_meta_key; ?>"/>
1015 <input type="hidden" name="cc_php[]" value=""/>
1016 <input type="hidden" name="cc_code[]" value=""/>
1017 <input type="hidden" name="cc_sql[]" value=""/>
1018 <input type="hidden" name="cc_options[]" value="<?php echo (is_array($field)) ? $field['options'] : $slug;?>"/>
1019 <input type="hidden" name="cc_type[]" value="<?php echo (is_array($field)) ? $field['type'] : 'woo_order'; ?>"/>
1020 <input type="hidden" name="cc_value[]" value="<?php echo (is_array($field)) ? $field['label'] : $cur_meta_key; ?>"/>
1021 <input type="hidden" name="cc_name[]" value="<?php echo (is_array($field)) ? $field['name'] : $field;?>"/>
1022 <input type="hidden" name="cc_settings[]" value=""/>
1023 </div>
1024 </li>
1025 <?php
1026 $i++;
1027 }
1028 endif;
1029
1030 if ( ! empty($section['additional']) )
1031 {
1032 foreach ($section['additional'] as $sub_slug => $sub_section)
1033 {
1034 ?>
1035 <li class="available_sub_section">
1036 <p class="wpae-available-fields-group"><?php echo $sub_section['title']; ?><span class="wpae-expander">+</span></p>
1037 <div class="wpae-custom-field">
1038 <ul>
1039 <li class="wpallexport_disabled">
1040 <div class="default_column" rel="">
1041 <label class="wpallexport-element-label"><?php echo __("All", "wp_all_export_plugin") . ' ' . $sub_section['title']; ?></label>
1042 <input type="hidden" name="rules[]" value="pmxe_<?php echo $slug;?>_<?php echo $sub_slug;?>"/>
1043 </div>
1044 </li>
1045 <?php
1046 foreach ($sub_section['meta'] as $field) {
1047 ?>
1048 <li class="pmxe_<?php echo $slug; ?>_<?php echo $sub_slug;?> wpallexport_disabled">
1049 <div class="custom_column" rel="<?php echo ($i + 1);?>">
1050 <label class="wpallexport-xml-element"><?php echo (is_array($field)) ? $field['name'] : $field; ?></label>
1051 <input type="hidden" name="ids[]" value="1"/>
1052 <input type="hidden" name="cc_label[]" value="item_data__<?php echo (is_array($field)) ? $field['label'] : $field; ?>"/>
1053 <input type="hidden" name="cc_php[]" value=""/>
1054 <input type="hidden" name="cc_code[]" value=""/>
1055 <input type="hidden" name="cc_sql[]" value=""/>
1056 <input type="hidden" name="cc_options[]" value="item_data"/>
1057 <input type="hidden" name="cc_type[]" value="<?php echo (is_array($field)) ? $field['type'] : $sub_slug; ?>"/>
1058 <input type="hidden" name="cc_value[]" value="item_data__<?php echo (is_array($field)) ? $field['label'] : $field; ?>"/>
1059 <input type="hidden" name="cc_name[]" value="<?php echo (is_array($field)) ? $field['name'] : $field;?>"/>
1060 <input type="hidden" name="cc_settings[]" value=""/>
1061 </div>
1062 </li>
1063 <?php
1064 $i++;
1065 }
1066 ?>
1067 </ul>
1068 </li>
1069 <?php
1070 }
1071 }
1072
1073 ?>
1074 </ul>
1075 </div>
1076 <?php
1077 endif;
1078 endforeach;
1079 }
1080
1081 public function render_new_field(){
1082
1083 if ( ! self::$is_active ) return;
1084
1085 ?>
1086 <select class="wp-all-export-chosen-select" name="column_value_type" style="width:350px;">
1087 <?php
1088 foreach (self::$order_sections as $slug => $section) : //if ( in_array($slug, array('taxes', 'fees', 'items', 'notes', 'refunds'))) continue;
1089 ?>
1090 <optgroup label="<?php echo $section['title']; ?>">
1091 <?php
1092 foreach ($section['meta'] as $cur_meta_key => $field)
1093 {
1094 $field_label = is_array($field) ? $field['label'] : $cur_meta_key;
1095 $field_type = is_array($field) ? $field['type'] : 'woo_order';
1096 $field_name = is_array($field) ? $field['name'] : $field;
1097 $field_options = is_array($field) ? $field['options'] : $slug;
1098 ?>
1099 <option
1100 value="<?php echo $field_type;?>"
1101 label="<?php echo $field_label;?>"
1102 options="<?php echo $field_options; ?>"><?php echo $field_name;?></option>
1103 <?php
1104 }
1105 ?>
1106 </optgroup>
1107 <?php
1108 if ( ! empty($section['additional']) )
1109 {
1110 foreach ($section['additional'] as $sub_slug => $sub_section)
1111 {
1112 ?>
1113 <optgroup label="<?php echo $sub_section['title']; ?>">
1114
1115 <?php foreach ($sub_section['meta'] as $field): ?>
1116
1117 <?php
1118 $field_label = 'item_data__' . ( is_array($field) ? $field['label'] : $field );
1119 $field_type = is_array($field) ? $field['type'] : $sub_slug;
1120 $field_name = is_array($field) ? $field['name'] : $field;
1121 $field_options = 'item_data';
1122 ?>
1123 <option
1124 value="<?php echo $field_type;?>"
1125 label="<?php echo $field_label;?>"
1126 options="<?php echo $field_options; ?>"><?php echo $field_name;?></option>
1127
1128 <?php endforeach; ?>
1129
1130 </optgroup>
1131 <?php
1132 }
1133 }
1134
1135 endforeach;
1136
1137 // Render Available ACF
1138 XmlExportEngine::$acf_export->render_new_field();
1139
1140 ?>
1141
1142 <optgroup label="Advanced">
1143 <option value="sql" label="sql"><?php _e("SQL Query", "wp_all_export_plugin"); ?></option>
1144 </optgroup>
1145
1146 </select>
1147 <?php
1148 }
1149
1150 public function render_filters(){
1151
1152 if ( ! self::$is_active ) return;
1153
1154 foreach (self::$order_sections as $slug => $section) :
1155 ?>
1156 <optgroup label="<?php echo $section['title']; ?>">
1157 <?php
1158 foreach ($section['meta'] as $cur_meta_key => $field)
1159 {
1160 $field_label = is_array($field) ? $field['label'] : $cur_meta_key;
1161 $field_type = is_array($field) ? $field['type'] : 'woo_order';
1162 $field_name = is_array($field) ? $field['name'] : $field;
1163 $field_options = is_array($field) ? $field['options'] : $slug;
1164
1165 switch ($field_options)
1166 {
1167 case 'order':
1168 case 'customer':
1169 if ( strpos($field_label, '_') === 0):
1170 ?>
1171 <option value="<?php echo 'cf_' . $field_label; ?>"><?php echo $field_name; ?></option>
1172 <?php
1173 else:
1174 ?>
1175 <option value="<?php echo $field_label; ?>"><?php echo $field_name; ?></option>
1176 <?php
1177 endif;
1178 break;
1179 case 'notes':
1180 case 'items':
1181 case 'taxes':
1182 case 'fees':
1183 case 'refunds':
1184 break;
1185 default:
1186 switch ($field_type)
1187 {
1188 case 'cf':
1189 ?>
1190 <option value="<?php echo 'cf_' . $field_label; ?>"><?php echo $field_name; ?></option>
1191 <?php
1192 break;
1193 case 'cats':
1194 case 'attr':
1195 ?>
1196 <option value="<?php echo 'tx_' . $field_label; ?>"><?php echo $field_name; ?></option>
1197 <?php
1198 break;
1199 default:
1200 ?>
1201 <option value="<?php echo $field_label; ?>"><?php echo $field_name; ?></option>
1202 <?php
1203 break;
1204 }
1205 break;
1206 }
1207 }
1208 ?>
1209 </optgroup>
1210 <?php
1211
1212 endforeach;
1213
1214 }
1215
1216 public function available_sections(){
1217
1218 $sections = array(
1219 'order' => array(
1220 'title' => __('Order', 'wp_all_export_plugin'),
1221 'meta' => $this->available_order_data()
1222 ),
1223 'customer' => array(
1224 'title' => __('Customer', 'wp_all_export_plugin'),
1225 'meta' => $this->available_customer_data()
1226 ),
1227 'items' => array(
1228 'title' => __('Items', 'wp_all_export_plugin'),
1229 'meta' => $this->available_order_default_product_data(),
1230 'additional' => $this->available_order_items_data()
1231 ),
1232 'taxes' => array(
1233 'title' => __('Taxes & Shipping', 'wp_all_export_plugin'),
1234 'meta' => $this->available_order_taxes_data()
1235 ),
1236 'fees' => array(
1237 'title' => __('Fees & Discounts', 'wp_all_export_plugin'),
1238 'meta' => $this->available_order_fees_data()
1239 ),
1240 'notes' => array(
1241 'title' => __('Notes', 'wp_all_export_plugin'),
1242 'meta' => array(
1243 'comment_content' => __('Note Content', 'wp_all_export_plugin'),
1244 'comment_date' => __('Note Date', 'wp_all_export_plugin'),
1245 'visibility' => __('Note Visibility', 'wp_all_export_plugin'),
1246 'comment_author' => __('Note User Name', 'wp_all_export_plugin'),
1247 'comment_author_email' => __('Note User Email', 'wp_all_export_plugin')
1248 )
1249 ),
1250 'refunds' => array(
1251 'title' => __('Refunds', 'wp_all_export_plugin'),
1252 'meta' => array(
1253 '_refund_total' => __('Refund Total', 'wp_all_export_plugin'),
1254 'refund_id' => __('Refund ID', 'wp_all_export_plugin'),
1255 'refund_amount' => __('Refund Amounts', 'wp_all_export_plugin'),
1256 'refund_reason' => __('Refund Reason', 'wp_all_export_plugin'),
1257 'refund_date' => __('Refund Date', 'wp_all_export_plugin'),
1258 'refund_author_email' => __('Refund Author Email', 'wp_all_export_plugin')
1259 )
1260 ),
1261 'cf' => array(
1262 'title' => __('Custom Fields', 'wp_all_export_plugin'),
1263 'meta' => array()
1264 ),
1265 'other' => array(
1266 'title' => __('Other', 'wp_all_export_plugin'),
1267 'meta' => array()
1268 )
1269 );
1270
1271 return apply_filters('wp_all_export_available_order_sections_filter', $sections);
1272
1273 }
1274
1275 /*
1276 * Define the keys for orders informations to export
1277 */
1278 public function available_order_data()
1279 {
1280 $data = array(
1281 'ID' => __('Order ID', 'wp_all_export_plugin'),
1282 '_order_key' => __('Order Key', 'wp_all_export_plugin'),
1283 'post_date' => __('Order Date', 'wp_all_export_plugin'),
1284 '_completed_date' => __('Completed Date', 'wp_all_export_plugin'),
1285 'post_title' => __('Title', 'wp_all_export_plugin'),
1286 'post_status' => __('Order Status', 'wp_all_export_plugin'),
1287 '_order_currency' => __('Order Currency', 'wp_all_export_plugin'),
1288 '_payment_method_title' => __('Payment Method Title', 'wp_all_export_plugin'),
1289 '_order_total' => __('Order Total', 'wp_all_export_plugin')
1290 );
1291
1292 return apply_filters('wp_all_export_available_order_data_filter', $data);
1293 }
1294
1295 /*
1296 * Define the keys for general product informations to export
1297 */
1298 public function available_order_default_product_data()
1299 {
1300
1301 $data = array(
1302 '_product_id' => __('Product ID', 'wp_all_export_plugin'),
1303 '__product_sku' => __('SKU', 'wp_all_export_plugin'),
1304 '__product_title' => __('Product Name', 'wp_all_export_plugin'),
1305 '__product_variation' => __('Product Variation Details', 'wp_all_export_plugin'),
1306 '_qty' => __('Quantity', 'wp_all_export_plugin'),
1307 '_line_subtotal' => __('Item Cost', 'wp_all_export_plugin'),
1308 '_line_total' => __('Item Total', 'wp_all_export_plugin'),
1309 '_line_subtotal_tax' => __('Item Tax', 'wp_all_export_plugin'),
1310 '_line_tax' => __('Item Tax Total', 'wp_all_export_plugin'),
1311 '_line_tax_data' => __('Item Tax Data', 'wp_all_export_plugin'),
1312 '__line_item_id' => __('Order Line ID', 'wp_all_export_plugin'),
1313 '__line_item_title' => __('Order Line Title', 'wp_all_export_plugin'),
1314 );
1315
1316 return apply_filters('wp_all_export_available_order_default_product_data_filter', $data);
1317 }
1318
1319 public function available_order_items_data()
1320 {
1321
1322 $data = XmlExportEngine::$woo_export->get_all_fields_for_order_items();
1323
1324 return apply_filters('wp_all_export_available_order_additional_product_data_filter', $data);
1325 }
1326
1327 public function available_order_taxes_data(){
1328
1329 $data = array(
1330 'tax_order_item_name' => __('Rate Code (per tax)', 'wp_all_export_plugin'),
1331 'tax_rate' => __('Rate Percentage (per tax)', 'wp_all_export_plugin'),
1332 'tax_amount' => __('Amount (per tax)', 'wp_all_export_plugin'),
1333 '_order_tax' => __('Total Tax Amount', 'wp_all_export_plugin'),
1334 'shipping_order_item_name' => __('Shipping Method', 'wp_all_export_plugin'),
1335 '_order_shipping' => __('Shipping Cost', 'wp_all_export_plugin'),
1336 '_order_shipping_taxes' => __('Shipping Taxes', 'wp_all_export_plugin')
1337 );
1338
1339 return apply_filters('wp_all_export_available_order_default_taxes_data_filter', $data);
1340 }
1341
1342 public function available_order_fees_data(){
1343
1344 $data = array(
1345 'discount_amount' => __('Discount Amount (per coupon)', 'wp_all_export_plugin'),
1346 '__coupons_used' => __('Coupons Used', 'wp_all_export_plugin'),
1347 '_cart_discount' => __('Total Discount Amount', 'wp_all_export_plugin'),
1348 'fee_line_total' => __('Fee Amount (per surcharge)', 'wp_all_export_plugin'),
1349 '__total_fee_amount' => __('Total Fee Amount', 'wp_all_export_plugin'),
1350 '__fee_tax_data' => __('Fee Taxes', 'wp_all_export_plugin'),
1351 );
1352
1353 return apply_filters('wp_all_export_available_order_fees_data_filter', $data);
1354 }
1355
1356 public function available_customer_data()
1357 {
1358
1359 $main_fields = array(
1360 '_customer_user' => __('Customer User ID', 'wp_all_export_plugin'),
1361 'post_excerpt' => __('Customer Note', 'wp_all_export_plugin')
1362 );
1363
1364 $data = array_merge($main_fields, $this->available_billing_information_data(), $this->available_shipping_information_data());
1365
1366 return apply_filters('wp_all_export_available_user_data_filter', $data);
1367
1368 }
1369
1370 public function available_billing_information_data()
1371 {
1372
1373 $keys = array(
1374 '_billing_first_name', '_billing_last_name', '_billing_company',
1375 '_billing_address_1', '_billing_address_2', '_billing_city',
1376 '_billing_postcode', '_billing_country', '_billing_state',
1377 '_billing_email', '_customer_user_email', '_billing_phone'
1378 );
1379
1380 $data = $this->generate_friendly_titles($keys, 'billing');
1381
1382 return apply_filters('wp_all_export_available_billing_information_data_filter', $data);
1383
1384 }
1385
1386 public function available_shipping_information_data()
1387 {
1388
1389 $keys = array(
1390 '_shipping_first_name', '_shipping_last_name', '_shipping_company',
1391 '_shipping_address_1', '_shipping_address_2', '_shipping_city',
1392 '_shipping_postcode', '_shipping_country', '_shipping_state'
1393 );
1394
1395 $data = $this->generate_friendly_titles($keys, 'shipping');
1396
1397 return apply_filters('wp_all_export_available_shipping_information_data_filter', $data);
1398
1399 }
1400
1401 public function generate_friendly_titles($keys, $keyword = ''){
1402 $data = array();
1403 foreach ($keys as $key) {
1404
1405 $key1 = $this->fix_titles(str_replace('_', ' ', $key));
1406 $key2 = '';
1407
1408 if(strpos($key1, $keyword)!== false)
1409 {
1410 $key1 = str_replace($keyword, '', $key1);
1411 $key2 = ' ('.__($keyword, 'wp_all_export_plugin').')';
1412 }
1413
1414 $data[$key] = __(trim($key1), 'wp_all_export_plugin').$key2;
1415
1416 if ( '_billing_email' == $key ) $data[$key] = __('Billing Email Address', 'wp_all_export_plugin');
1417 if ( '_customer_user_email' == $key) $data[$key] = __('Customer Account Email Address', 'wp_all_export_plugin');
1418
1419 }
1420 return $data;
1421 }
1422
1423 public static function prepare_import_template( $exportOptions, &$templateOptions, $element_name, $ID )
1424 {
1425
1426 if ( ! self::$is_active ) return;
1427
1428 $options = $exportOptions;
1429
1430 $element_type = $options['cc_value'][$ID];
1431
1432 $is_xml_template = $options['export_to'] == 'xml';
1433
1434 $implode_delimiter = XmlExportEngine::$implode;
1435
1436 $billing_keys = array(
1437 '_billing_first_name', '_billing_last_name', '_billing_company',
1438 '_billing_address_1', '_billing_address_2', '_billing_city',
1439 '_billing_postcode', '_billing_country', '_billing_state',
1440 '_billing_email', '_billing_phone'
1441 );
1442
1443 $shipping_keys = array(
1444 '_shipping_first_name', '_shipping_last_name', '_shipping_company',
1445 '_shipping_address_1', '_shipping_address_2', '_shipping_city',
1446 '_shipping_postcode', '_shipping_country', '_shipping_state'
1447 );
1448
1449 switch ($element_type)
1450 {
1451 case 'ID':
1452 $templateOptions['unique_key'] = '{'. $element_name .'[1]}';
1453 $templateOptions['tmp_unique_key'] = '{'. $element_name .'[1]}';
1454 break;
1455
1456 case 'post_title':
1457 $templateOptions['title'] = '{'. $element_name .'[1]}';
1458 $templateOptions['is_update_title'] = 1;
1459 break;
1460
1461 case 'post_status':
1462 $templateOptions['is_update_status'] = 1;
1463 $templateOptions['pmwi_order']['status'] = 'xpath';
1464 $templateOptions['pmwi_order']['status_xpath'] = '{'. $element_name .'[1]}';
1465 break;
1466
1467 case 'post_date':
1468 $templateOptions['is_update_dates'] = 1;
1469 $templateOptions['pmwi_order']['date'] = '{'. $element_name .'[1]}';
1470 break;
1471
1472 case '_customer_user_email':
1473 $templateOptions['pmwi_order']['billing_source_match_by'] = 'email';
1474 $templateOptions['pmwi_order']['billing_source_email'] = '{'. $element_name .'[1]}';
1475 $templateOptions['pmwi_order']['is_update_billing_details'] = 1;
1476 $templateOptions['pmwi_order']['is_update_shipping_details'] = 1;
1477 //$templateOptions['pmwi_order']['guest' . $element_type] = '{'. $element_name .'[1]}';
1478 break;
1479
1480 case 'post_excerpt':
1481 $templateOptions['is_update_excerpt'] = 1;
1482 $templateOptions['pmwi_order']['customer_provided_note'] = '{'. $element_name .'[1]}';
1483 break;
1484
1485 case '_transaction_id':
1486 $templateOptions['pmwi_order']['transaction_id'] = '{'. $element_name .'[1]}';
1487 break;
1488
1489 case '_payment_method':
1490 $templateOptions['pmwi_order']['payment_method'] = 'xpath';
1491 $templateOptions['pmwi_order']['payment_method_xpath'] = '{'. $element_name .'[1]}';
1492 $templateOptions['pmwi_order']['is_update_payment'] = 1;
1493 break;
1494
1495 case '__product_sku':
1496 $templateOptions['pmwi_order']['is_update_products'] = 1;
1497 $templateOptions['pmwi_order']['products_repeater_mode'] = $options['export_to'];
1498 if ($is_xml_template)
1499 {
1500 $templateOptions['pmwi_order']['products_repeater_mode_foreach'] = '{OrderItems[1]/Item}';
1501 $templateOptions['pmwi_order']['products'][0]['sku'] = '{'. $element_name .'[1]}';
1502 }
1503 else
1504 {
1505 $templateOptions['pmwi_order']['products_repeater_mode_separator'] = $implode_delimiter;
1506
1507 if ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom') {
1508 $templateOptions['pmwi_order']['products'][0]['sku'] = '{'. $element_name .'[1]}';
1509 }
1510 else{
1511 $templateOptions['pmwi_order']['products'][0]['sku'] = '{'. $element_name .'1[1]}';
1512 }
1513 }
1514 break;
1515
1516 case '_qty':
1517
1518 if ($is_xml_template)
1519 {
1520 $templateOptions['pmwi_order']['products'][0]['qty'] = '{'. $element_name .'[1]}';
1521 }
1522 else
1523 {
1524 $templateOptions['pmwi_order']['products_repeater_mode_separator'] = $implode_delimiter;
1525
1526 if ( $options['order_item_per_row'] or $options['xml_template_type'] == 'custom') {
1527 $templateOptions['pmwi_order']['products'][0]['qty'] = '{'. $element_name .'[1]}';
1528 }
1529 else{
1530 $templateOptions['pmwi_order']['products'][0]['qty'] = '{'. $element_name .'1[1]}';
1531 }
1532 }
1533 break;
1534
1535 // prepare template for fee line items
1536 case 'fee_line_total':
1537 $templateOptions['pmwi_order']['is_update_fees'] = 1;
1538 $templateOptions['pmwi_order']['fees_repeater_mode'] = $options['export_to'];
1539 if ($is_xml_template)
1540 {
1541 $templateOptions['pmwi_order']['fees_repeater_mode_foreach'] = '{OrderSurcharge[1]/Surcharge}';
1542 $templateOptions['pmwi_order']['fees'][0]['name'] = '{FeeName[1]}';
1543 $templateOptions['pmwi_order']['fees'][0]['amount'] = '{'.$element_name.'[1]}';
1544 }
1545 else
1546 {
1547 $templateOptions['pmwi_order']['fees_repeater_mode_separator'] = $implode_delimiter;
1548 $templateOptions['pmwi_order']['fees'][0]['name'] = '{feename[1]}';
1549 $templateOptions['pmwi_order']['fees'][0]['amount'] = '{'.$element_name.'[1]}';
1550 }
1551 break;
1552
1553 // prepare template for coupon line items
1554 case 'discount_amount':
1555 $templateOptions['pmwi_order']['is_update_coupons'] = 1;
1556 $templateOptions['pmwi_order']['coupons_repeater_mode'] = $options['export_to'];
1557 $templateOptions['pmwi_order']['coupons'][0]['amount_tax'] = '';
1558 if ($is_xml_template)
1559 {
1560 $templateOptions['pmwi_order']['coupons_repeater_mode_foreach'] = '{OrderCoupons[1]/Coupon}';
1561 $templateOptions['pmwi_order']['coupons'][0]['code'] = '{CouponCode[1]}';
1562 $templateOptions['pmwi_order']['coupons'][0]['amount'] = '[str_replace("-","",{'.$element_name.'[1]})]';
1563 }
1564 else
1565 {
1566 $templateOptions['pmwi_order']['coupons_repeater_mode_separator'] = $implode_delimiter;
1567 $templateOptions['pmwi_order']['coupons'][0]['code'] = '{couponcode[1]}';
1568 $templateOptions['pmwi_order']['coupons'][0]['amount'] = '[str_replace("-","",{'.$element_name.'[1]})]';
1569 }
1570 break;
1571
1572 // prepare template for shipping line items
1573 case 'shipping_order_item_name':
1574 $templateOptions['pmwi_order']['is_update_shipping'] = 1;
1575 $templateOptions['pmwi_order']['shipping_repeater_mode'] = $options['export_to'];
1576 $templateOptions['pmwi_order']['shipping'][0]['name'] = '{'. $element_name.'[1]}';
1577 $templateOptions['pmwi_order']['shipping'][0]['class'] = 'xpath';
1578 $templateOptions['pmwi_order']['shipping'][0]['class_xpath'] = '{'. $element_name .'[1]}';
1579
1580 if ($is_xml_template)
1581 {
1582 $templateOptions['pmwi_order']['shipping_repeater_mode_foreach'] = '{OrderShipping[1]/Shipping}';
1583 }
1584 else
1585 {
1586 $templateOptions['pmwi_order']['shipping_repeater_mode_separator'] = $implode_delimiter;
1587 }
1588 break;
1589 // shipping cost
1590 case '_order_shipping':
1591 $templateOptions['pmwi_order']['shipping'][0]['amount'] = '{'. $element_name .'[1]}';
1592 break;
1593
1594 // prepare template for tax line items
1595 case 'tax_order_item_name':
1596 $templateOptions['pmwi_order']['is_update_taxes'] = 1;
1597 $templateOptions['pmwi_order']['taxes_repeater_mode'] = $options['export_to'];
1598 $templateOptions['pmwi_order']['taxes'][0]['shipping_tax_amount'] = '';
1599 $templateOptions['pmwi_order']['taxes'][0]['code'] = 'xpath';
1600 $templateOptions['pmwi_order']['taxes'][0]['code_xpath'] = '{'. str_replace("pertax", "", $element_name) .'[1]}';
1601
1602 if ($is_xml_template)
1603 {
1604 $templateOptions['pmwi_order']['taxes_repeater_mode_foreach'] = '{OrderTaxes[1]/Tax}';
1605 }
1606 else
1607 {
1608 $templateOptions['pmwi_order']['taxes_repeater_mode_separator'] = $implode_delimiter;
1609 }
1610 break;
1611
1612 case 'tax_rate':
1613 $templateOptions['pmwi_order']['taxes'][0]['tax_code'] = 'xpath';
1614 $templateOptions['pmwi_order']['taxes'][0]['tax_code_xpath'] = '{ratename[1]}';
1615 break;
1616
1617 case 'tax_amount':
1618 $templateOptions['pmwi_order']['taxes'][0]['tax_amount'] = '{'. str_replace("pertax", "", $element_name).'[1]}';
1619 break;
1620
1621 // order notes
1622 case 'comment_content':
1623 case 'comment_date':
1624 $templateOptions['pmwi_order']['is_update_notes'] = 1;
1625 $templateOptions['pmwi_order']['notes_repeater_mode'] = 'csv';
1626 $templateOptions['pmwi_order']['notes_repeater_mode_separator'] = $implode_delimiter;
1627 $templateOptions['pmwi_order']['notes'][0][str_replace('comment_', '', $element_type)] = '{'. $element_name .'[1]}';
1628 break;
1629 case 'visibility':
1630 $templateOptions['pmwi_order']['notes'][0]['visibility'] = 'xpath';
1631 $templateOptions['pmwi_order']['notes'][0]['visibility_xpath'] = '{'. $element_name .'[1]}';
1632 break;
1633 case 'comment_author':
1634 $templateOptions['pmwi_order']['notes'][0]['username'] = '{'. $element_name .'[1]}';
1635 break;
1636 case 'comment_author_email':
1637 $templateOptions['pmwi_order']['notes'][0]['email'] = '{'. $element_name .'[1]}';
1638 break;
1639 // Order Total
1640 case '_order_total':
1641 $templateOptions['pmwi_order']['order_total_logic'] = 'manually';
1642 $templateOptions['pmwi_order']['order_total_xpath'] = '{'. $element_name .'[1]}';
1643 break;
1644 // Order Refunds
1645 case 'refund_amount':
1646 case 'refund_reason':
1647 case 'refund_date':
1648 $templateOptions['pmwi_order']['is_update_refunds'] = 1;
1649 if ($is_xml_template)
1650 {
1651 $templateOptions['pmwi_order']['order_' . $element_type] = '{OrderRefunds[1]/Refund/'.$element_name.'[1]}';
1652 }
1653 else
1654 {
1655 $templateOptions['pmwi_order']['order_' . $element_type] = '{'. $element_name .'[1]}';
1656 }
1657 break;
1658 case 'refund_author_email':
1659 $templateOptions['pmwi_order']['order_refund_issued_email'] = '{'. $element_name .'[1]}';
1660 $templateOptions['pmwi_order']['order_refund_issued_match_by'] = 'email';
1661 if ($is_xml_template)
1662 {
1663 $templateOptions['pmwi_order']['order_refund_issued_email'] = '{OrderRefunds[1]/Refund/'.$element_name.'[1]}';
1664 }
1665 else
1666 {
1667 $templateOptions['pmwi_order']['order_refund_issued_email'] = '{'. $element_name .'[1]}';
1668 }
1669 break;
1670 default:
1671 if ( in_array($element_type, $billing_keys)){
1672 $templateOptions['pmwi_order']['guest' . $element_type] = '{'. $element_name .'[1]}';
1673 }
1674 if ( in_array($element_type, $shipping_keys)){
1675 $templateOptions['pmwi_order'][ltrim($element_type, '_')] = '{'. $element_name .'[1]}';
1676 }
1677 break;
1678 }
1679
1680 }
1681
1682 /**
1683 * __get function.
1684 *
1685 * @access public
1686 * @param mixed $key
1687 * @return mixed
1688 */
1689 public function __get( $key ) {
1690 return $this->get( $key );
1691 }
1692
1693 /**
1694 * Get a session variable
1695 *
1696 * @param string $key
1697 * @param mixed $default used if the session variable isn't set
1698 * @return mixed value of session variable
1699 */
1700 public function get( $key, $default = null ) {
1701 return isset( $this->{$key} ) ? $this->{$key} : $default;
1702 }
1703
1704 }
1705 }