Order Export & Order Import for WooCommerce - Version 1.3.2

Version Description

  • Bug fix.
Download this release

Release Info

Developer mujeebur
Plugin Icon 128x128 Order Export & Order Import for WooCommerce
Version 1.3.2
Comparing to
See all releases

Code changes from version 1.3.1 to 1.3.2

includes/exporter/class-wf-orderimpexpcsv-exporter.php CHANGED
@@ -249,6 +249,7 @@ class WF_OrderImpExpCsv_Exporter {
249
  'shipping_method' => $order->get_shipping_method(),
250
  'customer_id' => $order->get_user_id(),
251
  'customer_user' => $order->get_user_id(),
 
252
  'billing_first_name' => $order->billing_first_name,
253
  'billing_last_name' => $order->billing_last_name,
254
  'billing_company' => $order->billing_company,
@@ -299,6 +300,7 @@ class WF_OrderImpExpCsv_Exporter {
299
  'shipping_method' => $order->get_shipping_method(),
300
  'customer_id' => $order->get_user_id(),
301
  'customer_user' => $order->get_user_id(),
 
302
  'billing_first_name' => $order->get_billing_first_name(),
303
  'billing_last_name' => $order->get_billing_last_name(),
304
  'billing_company' => $order->get_billing_company(),
249
  'shipping_method' => $order->get_shipping_method(),
250
  'customer_id' => $order->get_user_id(),
251
  'customer_user' => $order->get_user_id(),
252
+ 'customer_email' => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : '',
253
  'billing_first_name' => $order->billing_first_name,
254
  'billing_last_name' => $order->billing_last_name,
255
  'billing_company' => $order->billing_company,
300
  'shipping_method' => $order->get_shipping_method(),
301
  'customer_id' => $order->get_user_id(),
302
  'customer_user' => $order->get_user_id(),
303
+ 'customer_email' => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : '',
304
  'billing_first_name' => $order->get_billing_first_name(),
305
  'billing_last_name' => $order->get_billing_last_name(),
306
  'billing_company' => $order->get_billing_company(),
includes/exporter/data/data-wf-post-columns.php CHANGED
@@ -24,6 +24,7 @@ return apply_filters('woocommerce_csv_order_post_columns', array(
24
  'shipping_method' => 'shipping_method',
25
  'customer_id' => 'customer_id',
26
  'customer_user' => 'customer_user',
 
27
  'billing_first_name' => 'billing_first_name',
28
  'billing_last_name' => 'billing_last_name',
29
  'billing_company' => 'billing_company',
24
  'shipping_method' => 'shipping_method',
25
  'customer_id' => 'customer_id',
26
  'customer_user' => 'customer_user',
27
+ 'customer_email' => 'customer_email',
28
  'billing_first_name' => 'billing_first_name',
29
  'billing_last_name' => 'billing_last_name',
30
  'billing_company' => 'billing_company',
includes/importer/class-wf-csv-parser.php CHANGED
@@ -1,798 +1,802 @@
1
  <?php
 
2
  /**
3
  * WooCommerce CSV Importer class for managing parsing of CSV files.
4
  */
5
  class WF_CSV_Parser {
6
 
7
- var $row;
8
- var $post_type;
9
- var $posts = array();
10
- var $processed_posts = array();
11
- var $file_url_import_enabled = true;
12
- var $log;
13
- var $merged = 0;
14
- var $skipped = 0;
15
- var $imported = 0;
16
- var $errored = 0;
17
- var $id;
18
- var $file_url;
19
- var $delimiter;
20
-
21
- /**
22
- * Constructor
23
- */
24
- public function __construct( $post_type = 'shop_order' ) {
25
- $this->post_type = $post_type;
26
-
27
- $this->order_meta_fields = array(
28
- "order_tax",
29
- "order_shipping",
30
- "order_shipping_tax",
31
-
32
- "shipping_total",
33
- "shipping_tax_total",
34
- "fee_total",
35
- "fee_tax_total",
36
- "tax_total",
37
- "discount_total",
38
- "refunded_total",
39
-
40
- "customer_user",
41
- "cart_discount",
42
- "order_discount",
43
- "order_total",
44
- "order_currency",
45
- "payment_method",
46
- "customer_id",
47
- "billing_first_name",
48
- "billing_last_name",
49
- "billing_company",
50
- "billing_address_1",
51
- "billing_address_2",
52
- "billing_city",
53
- "billing_state",
54
- "billing_postcode",
55
- "billing_country",
56
- "billing_email",
57
- "billing_phone",
58
- "shipping_first_name",
59
- "shipping_last_name",
60
- "shipping_company",
61
- "shipping_address_1",
62
- "shipping_address_2",
63
- "shipping_city",
64
- "shipping_state",
65
- "shipping_postcode",
66
- "shipping_country",
67
- "shipping_method",
68
- "Download Permissions Granted",
69
- );
70
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
- /**
74
- * Format data from the csv file
75
- * @param string $data
76
- * @param string $enc
77
- * @return string
78
- */
79
- public function format_data_from_csv( $data, $enc ) {
80
- return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
81
- }
82
-
83
- /**
84
- * Parse the data
85
- * @param string $file [description]
86
- * @param string $delimiter [description]
87
- * @param integer $start_pos [description]
88
- * @param integer $end_pos [description]
89
- * @return array
90
- */
91
- public function parse_data( $file, $delimiter, $start_pos = 0, $end_pos = null ) {
92
- // Set locale
93
- $enc = mb_detect_encoding( $file, 'UTF-8, ISO-8859-1', true );
94
- if ( $enc )
95
- setlocale( LC_ALL, 'en_US.' . $enc );
96
- @ini_set( 'auto_detect_line_endings', true );
97
 
98
- $parsed_data = array();
99
- $raw_headers = array();
100
 
101
- // Put all CSV data into an associative array
102
- if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
- $header = fgetcsv( $handle, 0, $delimiter , '"', '"' );
105
- if ( $start_pos != 0 )
106
- fseek( $handle, $start_pos );
107
 
108
- while ( ( $postmeta = fgetcsv( $handle, 0, $delimiter , '"', '"' ) ) !== FALSE ) {
109
- $row = array();
110
-
111
- foreach ( $header as $key => $heading ) {
112
- $s_heading = $heading;
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
 
 
115
 
116
- if ( $s_heading == '' )
117
- continue;
118
-
119
- // Add the heading to the parsed data
120
- $row[$s_heading] = ( isset( $postmeta[$key] ) ) ? $this->format_data_from_csv( $postmeta[$key], $enc ) : '';
121
-
122
- // Raw Headers stores the actual column name in the CSV
123
- $raw_headers[ $s_heading ] = $heading;
124
- }
125
- $parsed_data[] = $row;
126
-
127
- unset( $postmeta, $row );
128
-
129
- $position = ftell( $handle );
130
-
131
- if ( $end_pos && $position >= $end_pos )
132
- break;
133
- }
134
- fclose( $handle );
135
- }
136
- return array( $parsed_data, $raw_headers, $position );
137
- }
138
-
139
- /**
140
- * Parse orders
141
- * @param array $item
142
- * @param integer $merge_empty_cells
143
- * @return array
144
- */
145
- public function parse_orders( $parsed_data, $raw_headers, $merging, $record_offset ) {
146
- $item = $parsed_data;
147
- global $WF_CSV_Order_Import, $wpdb;
148
- //$allow_unknown_products = isset( $_POST['allow_unknown_products'] ) && $_POST['allow_unknown_products'] ? true : false;
149
- $allow_unknown_products = true;
150
- $results = array();
151
-
152
- // Count row
153
- $row = 0;
154
-
155
- // skipped records
156
- $skipped = 0;
157
-
158
- $csv_export_file = true;
159
-
160
- $available_methods = WC()->shipping()->load_shipping_methods();
161
- $available_gateways = WC()->payment_gateways->payment_gateways();
162
- $shop_order_status = $this->wc_get_order_statuses_neat();
163
-
164
- $tax_rates = array();
165
- foreach ( $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates" ) as $_row ) {
166
- $tax_rates[ $_row->tax_rate_id ] = $_row;
167
- }
168
-
169
- $row++;
170
-
171
- if ( $row <= $record_offset ) {
172
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> Row %s - skipped due to record offset.', 'wf_order_import_export' ), $row ) );
173
- unset($item);
174
- return;
175
- }
176
-
177
- $postmeta = $order = array();
178
-
179
- if ( ! $csv_export_file ) {
180
- // standard format: optional integer order number and formatted order number
181
- $order_number = ( ! empty( $item['order_number'] ) ) ? $item['order_number'] : null;
182
- $order_number_formatted = ( ! empty( $item['order_number_formatted'] ) ) ? $item['order_number_formatted'] : $order_number;
183
-
184
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> Row %s - preparing for import.', 'wf_order_import_export' ), $row ) );
185
-
186
-
187
- // validate the supplied formatted order number/order number
188
- if ( is_numeric( $order_number_formatted ) && ! $order_number ) $order_number = $order_number_formatted; // use formatted for underlying order number if possible
189
-
190
- if ( $order_number && ! is_numeric( $order_number ) ) {
191
-
192
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Order number field must be an integer: %s.', 'wf_order_import_export' ), $order_number ) );
193
- $skipped++;
194
- unset($item);
195
- return;
196
-
197
- }
198
-
199
- if ( $order_number_formatted && ! $order_number ) {
200
-
201
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', __( '> > Skipped. Formatted order number provided but no numerical order number, see the documentation for further details.', 'wf_order_import_export' ) );
202
- $skipped++;
203
- unset($item);
204
- return;
205
-
206
- }
207
- } else {
208
- $order_number_formatted = ! empty( $item['order_id']) ? $item['order_id'] : 0 ;
209
- $order_number = ( ! empty( $item['order_number'] ) ? $item['order_number'] : ( is_numeric( $order_number_formatted ) ? $order_number_formatted : 0 ) );
210
- }
211
-
212
- if ( $order_number_formatted ) {
213
- // verify that this order number isn't already in use
214
- $query_args = array(
215
- 'numberposts' => 1,
216
- 'meta_key' => apply_filters( 'woocommerce_order_number_formatted_meta_name', '_order_number_formatted' ),
217
- 'meta_value' => $order_number_formatted,
218
- 'post_type' => 'shop_order',
219
- 'post_status' => array_keys( wc_get_order_statuses() ),
220
- 'fields' => 'ids',
221
- );
222
-
223
- $order_id = 0;
224
- $orders = get_posts( $query_args );
225
- if ( ! empty( $orders ) ) {
226
- list( $order_id ) = get_posts( $query_args );
227
- }
228
-
229
- $order_id = apply_filters( 'woocommerce_find_order_by_order_number', $order_id, $order_number_formatted );
230
-
231
- if ( $order_id ) {
232
- // skip if order ID already exist.
233
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Order %s already exists.', 'wf_order_import_export' ), $order_number_formatted ) );
234
- $skipped++;
235
- unset($item);
236
- return;
237
- }
238
- }
239
-
240
- if ( isset( $item['customer_id'] ) && $item['customer_id'] ) {
241
- // attempt to find the customer user
242
- $found_customer = false;
243
- if ( is_int( $item['customer_id'] ) ) {
244
-
245
- $found_customer = get_user_by( 'id', $item['customer_id'] );
246
-
247
- if ( ! $found_customer ) {
248
-
249
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Cannot find customer with id %s.', 'wf_order_import_export' ), $item['customer_id'] ) );
250
- $skipped++;
251
- unset($item);
252
- return;;
253
-
254
- }
255
- } elseif ( is_email( $item['customer_id'] ) ) {
256
- // check by email
257
- $found_customer = email_exists( $item['customer_id'] );
258
- }
259
-
260
- if ( ! $found_customer ) {
261
- $found_customer = username_exists( $item['customer_id'] );
262
- }
263
-
264
- if ( ! $found_customer ) {
265
- // guest checkout
266
- $item['customer_id'] = 0;
267
- } else {
268
- $item['customer_id'] = $found_customer; // user id
269
- }
270
- } elseif ( $csv_export_file && isset( $item['billing_email'] ) && $item['billing_email'] ) {
271
- // see if we can link to user by billing email id
272
- $found_customer = email_exists( $item['billing_email'] );
273
- if ( $found_customer ) $item['customer_id'] = $found_customer;
274
- else $item['customer_id'] = 0; // guest checkout
275
- } else {
276
- // guest checkout
277
- $item['customer_id'] = 0;
278
- }
279
-
280
-
281
- if ( ! empty( $item['status'] ) ) {
282
- $found_status = false;
283
- $available_statuses = array();
284
- foreach ( $shop_order_status as $status_slug => $status_name ) {
285
- if ( 0 == strcasecmp( $status_slug, $item['status'] ) ) $found_status = true;
286
- $available_statuses[] = $status_slug;
287
- }
288
-
289
- if ( ! $found_status ) {
290
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Unknown order status %s (%s).', 'wf_order_import_export' ), $item['status'], implode( $available_statuses, ', ' ) ) );
291
- $skipped++;
292
- unset($item);
293
- return;
294
- }
295
- } else {
296
- $item['status'] = 'processing';
297
- }
298
-
299
- if(!empty($item['order_date'])) $item['date'] = $item['order_date'];
300
-
301
- if ( ! empty( $item['date'] ) ) {
302
- if ( false === ( $item['date'] = strtotime( $item['date'] ) ) ) {
303
- // invalid date format
304
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Invalid date format %s.', 'wf_order_import_export' ), $item['date'] ) );
305
- $skipped++;
306
- unset($item);
307
- return;
308
- }
309
- } else {
310
- $item['date'] = time();
311
- }
312
-
313
-
314
- $order_notes = array();
315
- if ( ! empty( $item['order_notes'] ) ) {
316
- $order_notes = explode( "|", $item['order_notes'] );
317
- }
318
-
319
- // build the order data object
320
- $order['status'] = $item['status'];
321
- $order['date'] = $item['date'];
322
- $order['order_comments'] = ! empty( $item['customer_note'] ) ? $item['customer_note'] : null;
323
- $order['notes'] = $order_notes;
324
- if ( ! is_null( $order_number ) ) $order['order_number'] = $order_number; // optional order number, for convenience
325
- if ( $order_number_formatted ) $order['order_number_formatted'] = $order_number_formatted;
326
-
327
- // totals
328
- $order_tax = $order_shipping_tax = null;
329
-
330
- // Get any known order meta fields, and default any missing ones to 0/null
331
- // the provided shipping/payment method will be used as-is, and if found in the list of available ones, the respective titles will also be set
332
- foreach ( $this->order_meta_fields as $column ) {
333
-
334
- switch ( $column ) {
335
-
336
- case 'shipping_method':
337
- $value = isset( $item[ $column ] ) ? $item[ $column ] : '';
338
-
339
- // look up shipping method by id or title
340
- $shipping_method = isset( $available_methods[ $value ] ) ? $value : null;
341
-
342
- if ( ! $shipping_method ) {
343
- // try by title
344
- foreach ( $available_methods as $method ) {
345
- if ( 0 === strcasecmp( $method->title, $value ) ) {
346
- $shipping_method = $method->id;
347
- break; // go with the first one we find
348
- }
349
- }
350
- }
351
-
352
- if ( $shipping_method ) {
353
- //known shipping method found
354
- $postmeta[] = array( 'key' => '_shipping_method', 'value' => $shipping_method );
355
- $postmeta[] = array( 'key' => '_shipping_method_title', 'value' => $available_methods[ $shipping_method ]->title );
356
- } elseif ( $csv_export_file && $value ) {
357
- //Order CSV Export format, shipping method title with no corresponding shipping method type found, so just use the title
358
- $postmeta[] = array( 'key' => '_shipping_method', 'value' => '' );
359
- $postmeta[] = array( 'key' => '_shipping_method_title', 'value' => $value );
360
- } elseif ( $value ) {
361
- //Shipping method but no title
362
- $postmeta[] = array( 'key' => '_shipping_method', 'value' => $value );
363
- $postmeta[] = array( 'key' => '_shipping_method_title', 'value' => '' );
364
- } else {
365
- //none
366
- $postmeta[] = array( 'key' => '_shipping_method', 'value' => '' );
367
- $postmeta[] = array( 'key' => '_shipping_method_title', 'value' => '' );
368
- }
369
- break;
370
-
371
- case 'payment_method':
372
- $value = isset( $item[ $column ] ) ? $item[ $column ] : '';
373
-
374
- // look up shipping method by id or title
375
- $payment_method = isset( $available_gateways[ $value ] ) ? $value : null;
376
- if ( ! $payment_method ) {
377
- // try by title
378
- foreach ( $available_gateways as $method ) {
379
- if ( 0 === strcasecmp( $method->title, $value ) ) {
380
- $payment_method = $method->id;
381
- break; // go with the first one we find
382
- }
383
- }
384
- }
385
-
386
- if ( $payment_method ) {
387
- //Known payment method found
388
- $postmeta[] = array( 'key' => '_payment_method', 'value' => $payment_method );
389
- $postmeta[] = array( 'key' => '_payment_method_title', 'value' => $available_gateways[ $payment_method ]->title );
390
- } elseif ( $csv_export_file && $value ) {
391
- //Order CSV Export format, payment method title with no corresponding payments method type found, so just use the title
392
- $postmeta[] = array( 'key' => '_payment_method', 'value' => '' );
393
- $postmeta[] = array( 'key' => '_payment_method_title', 'value' => $value );
394
- } elseif ( $value ) {
395
- //payment method but no title
396
- $postmeta[] = array( 'key' => '_payment_method', 'value' => $value );
397
- $postmeta[] = array( 'key' => '_payment_method_title', 'value' => '' );
398
- } else {
399
- //none
400
- $postmeta[] = array( 'key' => '_payment_method', 'value' => '' );
401
- $postmeta[] = array( 'key' => '_payment_method_title', 'value' => '' );
402
- }
403
- break;
404
-
405
- // handle numerics
406
- case 'order_shipping': // legacy
407
- case 'shipping_total':
408
- $order_shipping = isset( $item[ $column ] ) ? $item[ $column ] : 0; // save the order shipping total for later use
409
- $postmeta[] = array( 'key' => '_order_shipping', 'value' => number_format( (float) $order_shipping, 2, '.', '' ) );
410
- break;
411
- case 'order_shipping_tax': // legacy
412
- case 'shipping_tax_total':
413
- // ignore blanks but allow zeroes
414
- if ( isset( $item[ $column ] ) && is_numeric( $item[ $column ] ) ) {
415
- $order_shipping_tax = $item[ $column ];
416
- }
417
- break;
418
- case 'order_tax': // legacy
419
- case 'tax_total':
420
- // ignore blanks but allow zeroes
421
- if ( isset( $item[ $column ] ) && is_numeric( $item[ $column ] ) ) {
422
- $order_tax = $item[ $column ];
423
- }
424
- break;
425
- case 'order_discount':
426
- case 'cart_discount':
427
- case 'order_total':
428
- $value = isset( $item[ $column ] ) ? $item[ $column ] : 0;
429
- $postmeta[] = array( 'key' => '_' . $column, 'value' => number_format( (float) $value, 2, '.', '' ) );
430
- break;
431
-
432
- case 'billing_country':
433
- case 'shipping_country':
434
- $value = isset( $item[ $column ] ) ? $item[ $column ] : '';
435
- // support country name or code by converting to code
436
- $country_code = array_search( $value, WC()->countries->countries );
437
- if ( $country_code ) $value = $country_code;
438
- $postmeta[] = array( 'key' => '_' . $column, 'value' => $value );
439
- break;
440
-
441
- case 'Download Permissions Granted':
442
- case 'download_permissions_granted':
443
- if ( isset( $item[ 'download_permissions_granted' ] ) ) {
444
- $postmeta[] = array( 'key' => '_download_permissions_granted', 'value' => $item[ 'download_permissions_granted' ] );
445
- }
446
- break;
447
- case 'download_permissions':
448
- if ( isset( $item[ 'download_permissions' ] ) ) {
449
- $postmeta[] = array( 'key' => '_download_permissions_granted', 'value' => $item[ 'download_permissions' ] );
450
- $postmeta[] = array( 'key' => '_download_permissions', 'value' => $item[ 'download_permissions' ] );
451
- }
452
- break;
453
-
454
- default: $postmeta[] = array( 'key' => '_' . $column, 'value' => isset( $item[ $column ] ) ? $item[ $column ] : "" );
455
- }
456
- }
457
-
458
- // Get any custom meta fields
459
- foreach ( $item as $key => $value ) {
460
-
461
- if ( ! $value ) {
462
- continue;
463
- }
464
-
465
- // Handle meta: columns - import as custom fields
466
- if ( strstr( $key, 'meta:' ) ) {
467
-
468
- // Get meta key name
469
- $meta_key = ( isset( $raw_headers[ $key ] ) ) ? $raw_headers[ $key ] : $key;
470
- $meta_key = trim( str_replace( 'meta:', '', $meta_key ) );
471
-
472
- // Add to postmeta array
473
- $postmeta[] = array(
474
- 'key' => esc_attr( $meta_key ),
475
- 'value' => $value,
476
- );
477
-
478
- }
479
- }
480
-
481
- $order_shipping_methods = array();
482
- $_shipping_methods = array();
483
-
484
- // pre WC 2.1 format of a single shipping method, left for backwards compatibility of import files
485
- if ( isset( $item['shipping_method'] ) && $item['shipping_method'] ) {
486
- // collect the shipping method id/cost
487
- $_shipping_methods[] = array(
488
- $item['shipping_method'],
489
- isset( $item['shipping_cost'] ) ? $item['shipping_cost'] : null
490
- );
491
- }
492
-
493
- // collect any additional shipping methods
494
- $i = null;
495
- if ( isset( $item['shipping_method_1'] ) ) {
496
- $i = 1;
497
- } elseif( isset( $item['shipping_method_2'] ) ) {
498
- $i = 2;
499
- }
500
-
501
- if ( ! is_null( $i ) ) {
502
- while ( ! empty( $item[ 'shipping_method_' . $i ] ) ) {
503
-
504
- $_shipping_methods[] = array(
505
- $item[ 'shipping_method_' . $i ],
506
- isset( $item[ 'shipping_cost_' . $i ] ) ? $item[ 'shipping_cost_' . $i ] : null
507
- );
508
- $i++;
509
- }
510
- }
511
-
512
- // if the order shipping total wasn't set, calculate it
513
- if ( ! isset( $order_shipping ) ) {
514
-
515
- $order_shipping = 0;
516
- foreach ( $_shipping_methods as $_shipping_method ) {
517
- $order_shipping += $_shipping_method[1];
518
- }
519
- $postmeta[] = array( 'key' => '_order_shipping' . $column, 'value' => number_format( (float) $order_shipping, 2, '.', '' ) );
520
-
521
- } elseif ( isset( $order_shipping ) && 1 == count( $_shipping_methods ) && is_null( $_shipping_methods[0][1] ) ) {
522
- // special case: if there was a total order shipping but no cost for the single shipping method, use the total shipping for the order shipping line item
523
- $_shipping_methods[0][1] = $order_shipping;
524
- }
525
-
526
- foreach ( $_shipping_methods as $_shipping_method ) {
527
-
528
- // look up shipping method by id or title
529
- $shipping_method = isset( $available_methods[ $_shipping_method[0] ] ) ? $_shipping_method[0] : null;
530
-
531
- if ( ! $shipping_method ) {
532
- // try by title
533
- foreach ( $available_methods as $method ) {
534
- if ( 0 === strcasecmp( $method->title, $_shipping_method[0] ) ) {
535
- $shipping_method = $method->id;
536
- break; // go with the first one we find
537
- }
538
- }
539
- }
540
-
541
- if ( $shipping_method ) {
542
- //Known shipping method found
543
- $order_shipping_methods[] = array( 'method_id' => $shipping_method, 'cost' => $_shipping_method[1], 'title' => $available_methods[ $shipping_method ]->title );
544
- } elseif ( $csv_export_file && $_shipping_method[0] ) {
545
- //Order CSV Export format, shipping method title with no corresponding shipping method type found, so just use the title
546
- $order_shipping_methods[] = array( 'method_id' => '', 'cost' => $_shipping_method[1], 'title' => $_shipping_method[0] );
547
- } elseif ( $_shipping_method[0] ) {
548
- //shipping method but no title
549
- $order_shipping_methods[] = array( 'method_id' => $_shipping_method[0], 'cost' => $_shipping_method[1], 'title' => '' );
550
- }
551
- }
552
-
553
- $order_items = array();
554
- if ( $csv_export_file ) {
555
- // standard format
556
- if ( ! empty( $item['line_item_1'] ) ) {
557
- // one or more order items
558
- $i = 1;
559
- while ( ! empty( $item[ 'line_item_' . $i ] ) ) {
560
-
561
- $_item_meta = preg_split( "~\\\\.(*SKIP)(*FAIL)|\|~s", $item[ 'line_item_' . $i ] );
562
-
563
- if ( $item[ 'line_item_' . $i ] && empty( $_item_meta ) ) {
564
- $_item_meta = explode( '|', $item[ 'line_item_' . $i ] );
565
- }
566
- $name = array_shift( $_item_meta );
567
- $name = substr($name, strpos($name, ":") + 1);
568
- $product_identifier_by_id = array_shift( $_item_meta ); // product_id:id
569
- $product_identifier_by_sku = array_shift( $_item_meta ); // sku
570
-
571
- $qty = array_shift( $_item_meta );
572
- $qty = substr($qty, strpos($qty, ":") + 1);
573
- $total = array_shift( $_item_meta );
574
- $total = substr($total, strpos($total, ":") + 1);
575
-
576
-
577
- // find by id
578
- if ( false !== strpos( $product_identifier_by_id, 'product_id:' ) ) {
579
- // product by product_id
580
- $product_id = substr( $product_identifier_by_id, 11 );
581
-
582
- // not a product
583
- if ( ! in_array( get_post_type( $product_id ), array( 'product', 'product_variation' ) ) ) {
584
- $product_id = '';
585
- }
586
-
587
- } else {
588
- // find by sku
589
- $product_sku = substr($product_identifier_by_sku, strpos($product_identifier_by_sku, ":") + 1);
590
- if($product_sku)
591
- $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value=%s LIMIT 1", $product_sku ) );
592
- else $product_id = '';
593
- }
594
-
595
- if ( ! $allow_unknown_products && ! $product_id ) {
596
- // unknown product
597
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Skipped. Unknown order item: %s.', 'wf_order_import_export' ), $product_identifier ) );
598
- $skipped++;
599
- $i++;
600
- continue; // break outer loop
601
- }
602
-
603
- // get any additional item meta
604
- $item_meta = array();
605
- foreach ( $_item_meta as $pair ) {
606
-
607
- // replace any escaped pipes
608
- $pair = str_replace( '\|', '|', $pair );
609
-
610
- // find the first ':' and split into name-value
611
- $split = strpos( $pair, ':' );
612
- $name = substr( $pair, 0, $split );
613
- $value = substr( $pair, $split + 1 );
614
-
615
- $item_meta[ $name ] = $value;
616
- }
617
-
618
- $order_items[] = array( 'product_id' => $product_id, 'qty' => $qty, 'total' => $total, 'meta' => $item_meta );
619
-
620
- $i++;
621
- }
622
- }
623
-
624
- } else {
625
- //CSV Order Export format
626
- $sku = $item['item_sku'];
627
- $qty = $item['item_amount'];
628
- $total = $item['row_price'];
629
-
630
- if ( ! $sku || ! $qty || ! is_numeric( $total ) ) {
631
- // invalid item
632
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Row %d - %s - skipped. Missing SKU, quantity or total', 'wf_order_import_export' ), $row, $item['order_id'] ) );
633
- $skipped++;
634
- unset($item);
635
- return; // break outer loop
636
- }
637
-
638
- // find by sku
639
- $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value=%s LIMIT 1", $sku ) );
640
-
641
- if ( ! $product_id ) {
642
- // unknown product
643
- $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf( __( '> > Row %d - %s - skipped. Unknown order item: %s.', 'wf_order_import_export' ), $row, $item['order_id'], $sku ) );
644
- $skipped++;
645
- unset($item);
646
- return; // break outer loop
647
- }
648
-
649
- $order_items[] = array( 'product_id' => $product_id, 'qty' => $qty, 'total' => $total );
650
- }
651
-
652
- $tax_items = array();
653
-
654
- // standard tax item format which supports multiple tax items in numbered columns containing a pipe-delimated, colon-labeled format
655
- if ( ! empty( $item['tax_item_1'] ) || ! empty( $item['tax_item'] ) ) {
656
- // one or more order tax items
657
-
658
- // get the first tax item
659
- $tax_item = ! empty( $item['tax_item_1'] ) ? $item['tax_item_1'] : $item['tax_item'];
660
-
661
- $i = 1;
662
- $tax_amount_sum = $shipping_tax_amount_sum = 0;
663
- while ( $tax_item ) {
664
-
665
- $tax_item_data = array();
666
-
667
- // turn "label: Tax | tax_amount: 10" into an associative array
668
- foreach ( explode( '|', $tax_item ) as $piece ) {
669
- list( $name, $value ) = explode( ':', $piece );
670
- $tax_item_data[ trim( $name ) ] = trim( $value );
671
- }
672
-
673
- // default rate id to 0 if not set
674
- if ( ! isset( $tax_item_data['rate_id'] ) ) {
675
- $tax_item_data['rate_id'] = 0;
676
- }
677
-
678
- // have a tax amount or shipping tax amount
679
- if ( isset( $tax_item_data['tax_amount'] ) || isset( $tax_item_data['shipping_tax_amount'] ) ) {
680
-
681
- // try and look up rate id by label if needed
682
- if ( isset( $tax_item_data['label'] ) && $tax_item_data['label'] && ! $tax_item_data['rate_id'] ) {
683
- foreach ( $tax_rates as $tax_rate ) {
684
-
685
- if ( 0 === strcasecmp( $tax_rate->tax_rate_name, $tax_item_data['label'] ) ) {
686
- // found the tax by label
687
- $tax_item_data['rate_id'] = $tax_rate->tax_rate_id;
688
- break;
689
- }
690
- }
691
- }
692
-
693
- // check for a rate being specified which does not exist, and clear it out (technically an error?)
694
- if ( $tax_item_data['rate_id'] && ! isset( $tax_rates[ $tax_item_data['rate_id'] ] ) ) {
695
- $tax_item_data['rate_id'] = 0;
696
- }
697
-
698
- // default label of 'Tax' if not provided
699
- if ( ! isset( $tax_item_data['label'] ) || ! $tax_item_data['label'] ) {
700
- $tax_item_data['label'] = 'Tax';
701
- }
702
-
703
- // default tax amounts to 0 if not set
704
- if ( ! isset( $tax_item_data['tax_amount'] ) ) {
705
- $tax_item_data['tax_amount'] = 0;
706
- }
707
- if ( ! isset( $tax_item_data['shipping_tax_amount'] ) ) {
708
- $tax_item_data['shipping_tax_amount'] = 0;
709
- }
710
-
711
- // handle compound flag by using the defined tax rate value (if any)
712
- if ( ! isset( $tax_item_data['tax_rate_compound'] ) ) {
713
- $tax_item_data['tax_rate_compound'] = '';
714
- if ( $tax_item_data['rate_id'] ) {
715
- $tax_item_data['tax_rate_compound'] = $tax_rates[ $tax_item_data['rate_id'] ]->tax_rate_compound;
716
- }
717
- }
718
-
719
- $tax_items[] = array(
720
- 'title' => '',
721
- 'rate_id' => $tax_item_data['rate_id'],
722
- 'label' => $tax_item_data['label'],
723
- 'compound' => $tax_item_data['tax_rate_compound'],
724
- 'tax_amount' => $tax_item_data['tax_amount'],
725
- 'shipping_tax_amount' => $tax_item_data['shipping_tax_amount'],
726
- );
727
-
728
- // sum up the order totals, in case it wasn't part of the import
729
- $tax_amount_sum += $tax_item_data['tax_amount'];
730
- $shipping_tax_amount_sum += $tax_item_data['shipping_tax_amount'];
731
- }
732
-
733
- // get the next tax item (if any)
734
- $i++;
735
- $tax_item = isset( $item[ 'tax_item_' . $i ] ) ? $item[ 'tax_item_' . $i ] : null;
736
- }
737
-
738
- if ( ! is_numeric( $order_tax ) ) {
739
- $order_tax = $tax_amount_sum;
740
- }
741
- if ( ! is_numeric( $order_shipping_tax ) ) {
742
- $order_shipping_tax = $shipping_tax_amount_sum;
743
- }
744
- }
745
-
746
- // default to zero if not set
747
- if ( ! is_numeric( $order_tax ) ) {
748
- $order_tax = 0;
749
- }
750
- if ( ! is_numeric( $order_shipping_tax ) ) {
751
- $order_shipping_tax = 0;
752
- }
753
-
754
- // no tax items specified, so create a default one using the tax totals
755
- if ( 0 == count( $tax_items ) ) {
756
-
757
- $tax_items[] = array(
758
- 'title' => '',
759
- 'rate_id' => 0,
760
- 'label' => 'Tax',
761
- 'compound' => '',
762
- 'tax_amount' => $order_tax,
763
- 'shipping_tax_amount' => $order_shipping_tax,
764
- );
765
- }
766
-
767
- // add the order tax totals to the order meta
768
- $postmeta[] = array( 'key' => '_order_tax', 'value' => number_format( (float) $order_tax, 2, '.', '' ) );
769
- $postmeta[] = array( 'key' => '_order_shipping_tax', 'value' => number_format( (float) $order_shipping_tax, 2, '.', '' ) );
770
-
771
-
772
- if ( $order ) {
773
- $order['postmeta'] = $postmeta;
774
- $order['order_items'] = $order_items;
775
- $order['order_shipping'] = $order_shipping_methods;
776
- $order['tax_items'] = $tax_items;
777
-
778
- // the order array will now contain the necessary name-value pairs for the wp_posts table, and also any meta data in the 'postmeta' array
779
- $results[] = $order;
780
- }
781
- // Result
782
- return array(
783
- $this->post_type => $results,
784
- 'skipped' => $skipped,
785
- );
786
- }
787
-
788
- private function wc_get_order_statuses_neat() {
789
-
790
- $order_statuses = array();
791
-
792
- foreach ( wc_get_order_statuses() as $slug => $name ) {
793
- $order_statuses[ preg_replace( '/^wc-/', '', $slug ) ] = $name;
794
- }
795
-
796
- return $order_statuses;
797
- }
798
- }
1
  <?php
2
+
3
  /**
4
  * WooCommerce CSV Importer class for managing parsing of CSV files.
5
  */
6
  class WF_CSV_Parser {
7
 
8
+ var $row;
9
+ var $post_type;
10
+ var $posts = array();
11
+ var $processed_posts = array();
12
+ var $file_url_import_enabled = true;
13
+ var $log;
14
+ var $merged = 0;
15
+ var $skipped = 0;
16
+ var $imported = 0;
17
+ var $errored = 0;
18
+ var $id;
19
+ var $file_url;
20
+ var $delimiter;
21
+
22
+ /**
23
+ * Constructor
24
+ */
25
+ public function __construct($post_type = 'shop_order') {
26
+ $this->post_type = $post_type;
27
+
28
+ $this->order_meta_fields = array(
29
+ "order_tax",
30
+ "order_shipping",
31
+ "order_shipping_tax",
32
+ "shipping_total",
33
+ "shipping_tax_total",
34
+ "fee_total",
35
+ "fee_tax_total",
36
+ "tax_total",
37
+ "discount_total",
38
+ "refunded_total",
39
+ "customer_user",
40
+ "cart_discount",
41
+ "order_discount",
42
+ "order_total",
43
+ "order_currency",
44
+ "payment_method",
45
+ "customer_email",
46
+ "billing_first_name",
47
+ "billing_last_name",
48
+ "billing_company",
49
+ "billing_address_1",
50
+ "billing_address_2",
51
+ "billing_city",
52
+ "billing_state",
53
+ "billing_postcode",
54
+ "billing_country",
55
+ "billing_email",
56
+ "billing_phone",
57
+ "shipping_first_name",
58
+ "shipping_last_name",
59
+ "shipping_company",
60
+ "shipping_address_1",
61
+ "shipping_address_2",
62
+ "shipping_city",
63
+ "shipping_state",
64
+ "shipping_postcode",
65
+ "shipping_country",
66
+ "shipping_method",
67
+ "Download Permissions Granted",
68
+ );
69
+ }
70
+
71
+ /**
72
+ * Format data from the csv file
73
+ * @param string $data
74
+ * @param string $enc
75
+ * @return string
76
+ */
77
+ public function format_data_from_csv($data, $enc) {
78
+ return ( $enc == 'UTF-8' ) ? $data : utf8_encode($data);
79
+ }
80
+
81
+ /**
82
+ * Parse the data
83
+ * @param string $file [description]
84
+ * @param string $delimiter [description]
85
+ * @param integer $start_pos [description]
86
+ * @param integer $end_pos [description]
87
+ * @return array
88
+ */
89
+ public function parse_data($file, $delimiter, $start_pos = 0, $end_pos = null) {
90
+ // Set locale
91
+ $enc = mb_detect_encoding($file, 'UTF-8, ISO-8859-1', true);
92
+ if ($enc)
93
+ setlocale(LC_ALL, 'en_US.' . $enc);
94
+ @ini_set('auto_detect_line_endings', true);
95
+
96
+ $parsed_data = array();
97
+ $raw_headers = array();
98
+
99
+ // Put all CSV data into an associative array
100
+ if (( $handle = fopen($file, "r") ) !== FALSE) {
101
+
102
+ $header = fgetcsv($handle, 0, $delimiter, '"', '"');
103
+ if ($start_pos != 0)
104
+ fseek($handle, $start_pos);
105
+
106
+ while (( $postmeta = fgetcsv($handle, 0, $delimiter, '"', '"') ) !== FALSE) {
107
+ $row = array();
108
+
109
+ foreach ($header as $key => $heading) {
110
+ $s_heading = $heading;
111
+
112
+
113
+
114
+ if ($s_heading == '')
115
+ continue;
116
 
117
+ // Add the heading to the parsed data
118
+ $row[$s_heading] = ( isset($postmeta[$key]) ) ? $this->format_data_from_csv($postmeta[$key], $enc) : '';
119
+
120
+ // Raw Headers stores the actual column name in the CSV
121
+ $raw_headers[$s_heading] = $heading;
122
+ }
123
+ $parsed_data[] = $row;
124
+
125
+ unset($postmeta, $row);
126
+
127
+ $position = ftell($handle);
128
+
129
+ if ($end_pos && $position >= $end_pos)
130
+ break;
131
+ }
132
+ fclose($handle);
133
+ }
134
+ return array($parsed_data, $raw_headers, $position);
135
+ }
136
+
137
+ /**
138
+ * Parse orders
139
+ * @param array $item
140
+ * @param integer $merge_empty_cells
141
+ * @return array
142
+ */
143
+ public function parse_orders($parsed_data, $raw_headers, $merging, $record_offset) {
144
+ $item = $parsed_data;
145
+ global $WF_CSV_Order_Import, $wpdb;
146
+ //$allow_unknown_products = isset( $_POST['allow_unknown_products'] ) && $_POST['allow_unknown_products'] ? true : false;
147
+ $allow_unknown_products = true;
148
+ $results = array();
149
+
150
+ // Count row
151
+ $row = 0;
152
+
153
+ // skipped records
154
+ $skipped = 0;
155
+
156
+ $csv_export_file = true;
157
+
158
+ $available_methods = WC()->shipping()->load_shipping_methods();
159
+ $available_gateways = WC()->payment_gateways->payment_gateways();
160
+ $shop_order_status = $this->wc_get_order_statuses_neat();
161
+
162
+ $tax_rates = array();
163
+ foreach ($wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates") as $_row) {
164
+ $tax_rates[$_row->tax_rate_id] = $_row;
165
+ }
166
+
167
+ $row++;
168
+
169
+ if ($row <= $record_offset) {
170
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> Row %s - skipped due to record offset.', 'wf_order_import_export'), $row));
171
+ unset($item);
172
+ return;
173
+ }
174
+
175
+ $postmeta = $order = array();
176
+
177
+ if (!$csv_export_file) {
178
+ // standard format: optional integer order number and formatted order number
179
+ $order_number = (!empty($item['order_number']) ) ? $item['order_number'] : null;
180
+ $order_number_formatted = (!empty($item['order_number_formatted']) ) ? $item['order_number_formatted'] : $order_number;
181
+
182
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> Row %s - preparing for import.', 'wf_order_import_export'), $row));
183
+
184
+
185
+ // validate the supplied formatted order number/order number
186
+ if (is_numeric($order_number_formatted) && !$order_number)
187
+ $order_number = $order_number_formatted; // use formatted for underlying order number if possible
188
+
189
+ if ($order_number && !is_numeric($order_number)) {
190
+
191
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Order number field must be an integer: %s.', 'wf_order_import_export'), $order_number));
192
+ $skipped++;
193
+ unset($item);
194
+ return;
195
+ }
196
+
197
+ if ($order_number_formatted && !$order_number) {
198
+
199
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', __('> > Skipped. Formatted order number provided but no numerical order number, see the documentation for further details.', 'wf_order_import_export'));
200
+ $skipped++;
201
+ unset($item);
202
+ return;
203
+ }
204
+ } else {
205
+ $order_number_formatted = !empty($item['order_id']) ? $item['order_id'] : 0;
206
+ $order_number = (!empty($item['order_number']) ? $item['order_number'] : ( is_numeric($order_number_formatted) ? $order_number_formatted : 0 ) );
207
+ }
208
+
209
+ if ($order_number_formatted) {
210
+ // verify that this order number isn't already in use
211
+ $query_args = array(
212
+ 'numberposts' => 1,
213
+ 'meta_key' => apply_filters('woocommerce_order_number_formatted_meta_name', '_order_number_formatted'),
214
+ 'meta_value' => $order_number_formatted,
215
+ 'post_type' => 'shop_order',
216
+ 'post_status' => array_keys(wc_get_order_statuses()),
217
+ 'fields' => 'ids',
218
+ );
219
+
220
+ $order_id = 0;
221
+ $orders = get_posts($query_args);
222
+ if (!empty($orders)) {
223
+ list( $order_id ) = get_posts($query_args);
224
+ }
225
+
226
+ $order_id = apply_filters('woocommerce_find_order_by_order_number', $order_id, $order_number_formatted);
227
+
228
+ if ($order_id) {
229
+ // skip if order ID already exist.
230
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Order %s already exists.', 'wf_order_import_export'), $order_number_formatted));
231
+ $skipped++;
232
+ unset($item);
233
+ return;
234
+ }
235
+ }
236
+ // handle the special (optional) customer_user field
237
+ if (isset($item['customer_id']) && $item['customer_id']) {
238
+ // attempt to find the customer user
239
+ $found_customer = false;
240
+ if (is_int($item['customer_id'])) {
241
+
242
+ $found_customer = get_user_by('id', $item['customer_id']);
243
+
244
+ if (!$found_customer) {
245
+
246
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Cannot find customer with id %s.', 'wf_order_import_export'), $item['customer_id']));
247
+ $skipped++;
248
+ unset($item);
249
+ return;
250
+ ;
251
+ }
252
+ } elseif (is_email($item['customer_id'])) {
253
+ // check by email
254
+ $found_customer = email_exists($item['customer_id']);
255
+ }
256
+
257
+ if (!$found_customer) {
258
+ $found_customer = username_exists($item['customer_id']);
259
+ }
260
+
261
+ if (!$found_customer) {
262
+ // guest checkout
263
+ $item['customer_id'] = 0;
264
+ } else {
265
+ $item['customer_id'] = $found_customer; // user id
266
+ }
267
+ } elseif ($csv_export_file && isset($item['billing_email']) && $item['billing_email']) {
268
+ // see if we can link to user by billing email id
269
+ $found_customer = email_exists($item['billing_email']);
270
+ if ($found_customer)
271
+ $item['customer_id'] = $found_customer;
272
+ else
273
+ $item['customer_id'] = 0; // guest checkout
274
+ } else {
275
+ // guest checkout
276
+ $item['customer_id'] = 0;
277
+ }
278
+
279
+
280
+ if (!empty($item['status'])) {
281
+ $found_status = false;
282
+ $available_statuses = array();
283
+ foreach ($shop_order_status as $status_slug => $status_name) {
284
+ if (0 == strcasecmp($status_slug, $item['status']))
285
+ $found_status = true;
286
+ $available_statuses[] = $status_slug;
287
+ }
288
+
289
+ if (!$found_status) {
290
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Unknown order status %s (%s).', 'wf_order_import_export'), $item['status'], implode($available_statuses, ', ')));
291
+ $skipped++;
292
+ unset($item);
293
+ return;
294
+ }
295
+ } else {
296
+ $item['status'] = 'processing';
297
+ }
298
+
299
+ if (!empty($item['order_date']))
300
+ $item['date'] = $item['order_date'];
301
+
302
+ if (!empty($item['date'])) {
303
+ if (false === ( $item['date'] = strtotime($item['date']) )) {
304
+ // invalid date format
305
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Invalid date format %s.', 'wf_order_import_export'), $item['date']));
306
+ $skipped++;
307
+ unset($item);
308
+ return;
309
+ }
310
+ } else {
311
+ $item['date'] = time();
312
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
 
 
314
 
315
+ $order_notes = array();
316
+ if (!empty($item['order_notes'])) {
317
+ $order_notes = explode("|", $item['order_notes']);
318
+ }
319
+
320
+ // build the order data object
321
+ $order['status'] = $item['status'];
322
+ $order['date'] = $item['date'];
323
+ $order['order_comments'] = !empty($item['customer_note']) ? $item['customer_note'] : null;
324
+ $order['notes'] = $order_notes;
325
+ if (!is_null($order_number))
326
+ $order['order_number'] = $order_number; // optional order number, for convenience
327
+ if ($order_number_formatted)
328
+ $order['order_number_formatted'] = $order_number_formatted;
329
+
330
+ // totals
331
+ $order_tax = $order_shipping_tax = null;
332
+
333
+ // Get any known order meta fields, and default any missing ones to 0/null
334
+ // the provided shipping/payment method will be used as-is, and if found in the list of available ones, the respective titles will also be set
335
+ foreach ($this->order_meta_fields as $column) {
336
+
337
+ switch ($column) {
338
+
339
+ case 'shipping_method':
340
+ $value = isset($item[$column]) ? $item[$column] : '';
341
+
342
+ // look up shipping method by id or title
343
+ $shipping_method = isset($available_methods[$value]) ? $value : null;
344
+
345
+ if (!$shipping_method) {
346
+ // try by title
347
+ foreach ($available_methods as $method) {
348
+ if (0 === strcasecmp($method->title, $value)) {
349
+ $shipping_method = $method->id;
350
+ break; // go with the first one we find
351
+ }
352
+ }
353
+ }
354
+
355
+ if ($shipping_method) {
356
+ //known shipping method found
357
+ $postmeta[] = array('key' => '_shipping_method', 'value' => $shipping_method);
358
+ $postmeta[] = array('key' => '_shipping_method_title', 'value' => $available_methods[$shipping_method]->title);
359
+ } elseif ($csv_export_file && $value) {
360
+ //Order CSV Export format, shipping method title with no corresponding shipping method type found, so just use the title
361
+ $postmeta[] = array('key' => '_shipping_method', 'value' => '');
362
+ $postmeta[] = array('key' => '_shipping_method_title', 'value' => $value);
363
+ } elseif ($value) {
364
+ //Shipping method but no title
365
+ $postmeta[] = array('key' => '_shipping_method', 'value' => $value);
366
+ $postmeta[] = array('key' => '_shipping_method_title', 'value' => '');
367
+ } else {
368
+ //none
369
+ $postmeta[] = array('key' => '_shipping_method', 'value' => '');
370
+ $postmeta[] = array('key' => '_shipping_method_title', 'value' => '');
371
+ }
372
+ break;
373
+
374
+ case 'payment_method':
375
+ $value = isset($item[$column]) ? $item[$column] : '';
376
+
377
+ // look up shipping method by id or title
378
+ $payment_method = isset($available_gateways[$value]) ? $value : null;
379
+ if (!$payment_method) {
380
+ // try by title
381
+ foreach ($available_gateways as $method) {
382
+ if (0 === strcasecmp($method->title, $value)) {
383
+ $payment_method = $method->id;
384
+ break; // go with the first one we find
385
+ }
386
+ }
387
+ }
388
+
389
+ if ($payment_method) {
390
+ //Known payment method found
391
+ $postmeta[] = array('key' => '_payment_method', 'value' => $payment_method);
392
+ $postmeta[] = array('key' => '_payment_method_title', 'value' => $available_gateways[$payment_method]->title);
393
+ } elseif ($csv_export_file && $value) {
394
+ //Order CSV Export format, payment method title with no corresponding payments method type found, so just use the title
395
+ $postmeta[] = array('key' => '_payment_method', 'value' => '');
396
+ $postmeta[] = array('key' => '_payment_method_title', 'value' => $value);
397
+ } elseif ($value) {
398
+ //payment method but no title
399
+ $postmeta[] = array('key' => '_payment_method', 'value' => $value);
400
+ $postmeta[] = array('key' => '_payment_method_title', 'value' => '');
401
+ } else {
402
+ //none
403
+ $postmeta[] = array('key' => '_payment_method', 'value' => '');
404
+ $postmeta[] = array('key' => '_payment_method_title', 'value' => '');
405
+ }
406
+ break;
407
+
408
+ // handle numerics
409
+ case 'order_shipping': // legacy
410
+ case 'shipping_total':
411
+ $order_shipping = isset($item[$column]) ? $item[$column] : 0; // save the order shipping total for later use
412
+ $postmeta[] = array('key' => '_order_shipping', 'value' => number_format((float) $order_shipping, 2, '.', ''));
413
+ break;
414
+ case 'order_shipping_tax': // legacy
415
+ case 'shipping_tax_total':
416
+ // ignore blanks but allow zeroes
417
+ if (isset($item[$column]) && is_numeric($item[$column])) {
418
+ $order_shipping_tax = $item[$column];
419
+ }
420
+ break;
421
+ case 'order_tax': // legacy
422
+ case 'tax_total':
423
+ // ignore blanks but allow zeroes
424
+ if (isset($item[$column]) && is_numeric($item[$column])) {
425
+ $order_tax = $item[$column];
426
+ }
427
+ break;
428
+ case 'order_discount':
429
+ case 'cart_discount':
430
+ case 'order_total':
431
+ $value = isset($item[$column]) ? $item[$column] : 0;
432
+ $postmeta[] = array('key' => '_' . $column, 'value' => number_format((float) $value, 2, '.', ''));
433
+ break;
434
+
435
+ case 'billing_country':
436
+ case 'shipping_country':
437
+ $value = isset($item[$column]) ? $item[$column] : '';
438
+ // support country name or code by converting to code
439
+ $country_code = array_search($value, WC()->countries->countries);
440
+ if ($country_code)
441
+ $value = $country_code;
442
+ $postmeta[] = array('key' => '_' . $column, 'value' => $value);
443
+ break;
444
+
445
+ case 'Download Permissions Granted':
446
+ case 'download_permissions_granted':
447
+ if (isset($item['download_permissions_granted'])) {
448
+ $postmeta[] = array('key' => '_download_permissions_granted', 'value' => $item['download_permissions_granted']);
449
+ }
450
+ break;
451
+ case 'download_permissions':
452
+ if (isset($item['download_permissions'])) {
453
+ $postmeta[] = array('key' => '_download_permissions_granted', 'value' => $item['download_permissions']);
454
+ $postmeta[] = array('key' => '_download_permissions', 'value' => $item['download_permissions']);
455
+ }
456
+ break;
457
+
458
+ default: $postmeta[] = array('key' => '_' . $column, 'value' => isset($item[$column]) ? $item[$column] : "");
459
+ }
460
+ }
461
+
462
+ if (!empty($item['customer_id']))
463
+ $postmeta[] = array('key' => '_customer_user', 'value' => $item['customer_id']);
464
+
465
+ // Get any custom meta fields
466
+ foreach ($item as $key => $value) {
467
 
468
+ if (!$value) {
469
+ continue;
470
+ }
471
 
472
+ // Handle meta: columns - import as custom fields
473
+ if (strstr($key, 'meta:')) {
 
 
 
474
 
475
+ // Get meta key name
476
+ $meta_key = ( isset($raw_headers[$key]) ) ? $raw_headers[$key] : $key;
477
+ $meta_key = trim(str_replace('meta:', '', $meta_key));
478
+
479
+ // Add to postmeta array
480
+ $postmeta[] = array(
481
+ 'key' => esc_attr($meta_key),
482
+ 'value' => $value,
483
+ );
484
+ }
485
+ }
486
+
487
+ $order_shipping_methods = array();
488
+ $_shipping_methods = array();
489
+
490
+ // pre WC 2.1 format of a single shipping method, left for backwards compatibility of import files
491
+ if (isset($item['shipping_method']) && $item['shipping_method']) {
492
+ // collect the shipping method id/cost
493
+ $_shipping_methods[] = array(
494
+ $item['shipping_method'],
495
+ isset($item['shipping_cost']) ? $item['shipping_cost'] : null
496
+ );
497
+ }
498
+
499
+ // collect any additional shipping methods
500
+ $i = null;
501
+ if (isset($item['shipping_method_1'])) {
502
+ $i = 1;
503
+ } elseif (isset($item['shipping_method_2'])) {
504
+ $i = 2;
505
+ }
506
+
507
+ if (!is_null($i)) {
508
+ while (!empty($item['shipping_method_' . $i])) {
509
+
510
+ $_shipping_methods[] = array(
511
+ $item['shipping_method_' . $i],
512
+ isset($item['shipping_cost_' . $i]) ? $item['shipping_cost_' . $i] : null
513
+ );
514
+ $i++;
515
+ }
516
+ }
517
+
518
+ // if the order shipping total wasn't set, calculate it
519
+ if (!isset($order_shipping)) {
520
+
521
+ $order_shipping = 0;
522
+ foreach ($_shipping_methods as $_shipping_method) {
523
+ $order_shipping += $_shipping_method[1];
524
+ }
525
+ $postmeta[] = array('key' => '_order_shipping' . $column, 'value' => number_format((float) $order_shipping, 2, '.', ''));
526
+ } elseif (isset($order_shipping) && 1 == count($_shipping_methods) && is_null($_shipping_methods[0][1])) {
527
+ // special case: if there was a total order shipping but no cost for the single shipping method, use the total shipping for the order shipping line item
528
+ $_shipping_methods[0][1] = $order_shipping;
529
+ }
530
+
531
+ foreach ($_shipping_methods as $_shipping_method) {
532
+
533
+ // look up shipping method by id or title
534
+ $shipping_method = isset($available_methods[$_shipping_method[0]]) ? $_shipping_method[0] : null;
535
+
536
+ if (!$shipping_method) {
537
+ // try by title
538
+ foreach ($available_methods as $method) {
539
+ if (0 === strcasecmp($method->title, $_shipping_method[0])) {
540
+ $shipping_method = $method->id;
541
+ break; // go with the first one we find
542
+ }
543
+ }
544
+ }
545
+
546
+ if ($shipping_method) {
547
+ //Known shipping method found
548
+ $order_shipping_methods[] = array('method_id' => $shipping_method, 'cost' => $_shipping_method[1], 'title' => $available_methods[$shipping_method]->title);
549
+ } elseif ($csv_export_file && $_shipping_method[0]) {
550
+ //Order CSV Export format, shipping method title with no corresponding shipping method type found, so just use the title
551
+ $order_shipping_methods[] = array('method_id' => '', 'cost' => $_shipping_method[1], 'title' => $_shipping_method[0]);
552
+ } elseif ($_shipping_method[0]) {
553
+ //shipping method but no title
554
+ $order_shipping_methods[] = array('method_id' => $_shipping_method[0], 'cost' => $_shipping_method[1], 'title' => '');
555
+ }
556
+ }
557
+
558
+ $order_items = array();
559
+ if ($csv_export_file) {
560
+ // standard format
561
+ if (!empty($item['line_item_1'])) {
562
+ // one or more order items
563
+ $i = 1;
564
+ while (!empty($item['line_item_' . $i])) {
565
+
566
+ $_item_meta = preg_split("~\\\\.(*SKIP)(*FAIL)|\|~s", $item['line_item_' . $i]);
567
+
568
+ if ($item['line_item_' . $i] && empty($_item_meta)) {
569
+ $_item_meta = explode('|', $item['line_item_' . $i]);
570
+ }
571
+ $name = array_shift($_item_meta);
572
+ $name = substr($name, strpos($name, ":") + 1);
573
+ $product_identifier_by_id = array_shift($_item_meta); // product_id:id
574
+ $product_identifier_by_sku = array_shift($_item_meta); // sku
575
+
576
+ $qty = array_shift($_item_meta);
577
+ $qty = substr($qty, strpos($qty, ":") + 1);
578
+ $total = array_shift($_item_meta);
579
+ $total = substr($total, strpos($total, ":") + 1);
580
+
581
+
582
+ // find by id
583
+ if (false !== strpos($product_identifier_by_id, 'product_id:')) {
584
+ // product by product_id
585
+ $product_id = substr($product_identifier_by_id, 11);
586
+
587
+ // not a product
588
+ if (!in_array(get_post_type($product_id), array('product', 'product_variation'))) {
589
+ $product_id = '';
590
+ }
591
+ } else {
592
+ // find by sku
593
+ $product_sku = substr($product_identifier_by_sku, strpos($product_identifier_by_sku, ":") + 1);
594
+ if ($product_sku)
595
+ $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value=%s LIMIT 1", $product_sku));
596
+ else
597
+ $product_id = '';
598
+ }
599
+
600
+ if (!$allow_unknown_products && !$product_id) {
601
+ // unknown product
602
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Skipped. Unknown order item: %s.', 'wf_order_import_export'), $product_identifier));
603
+ $skipped++;
604
+ $i++;
605
+ continue; // break outer loop
606
+ }
607
+
608
+ // get any additional item meta
609
+ $item_meta = array();
610
+ foreach ($_item_meta as $pair) {
611
+
612
+ // replace any escaped pipes
613
+ $pair = str_replace('\|', '|', $pair);
614
+
615
+ // find the first ':' and split into name-value
616
+ $split = strpos($pair, ':');
617
+ $name = substr($pair, 0, $split);
618
+ $value = substr($pair, $split + 1);
619
+
620
+ $item_meta[$name] = $value;
621
+ }
622
+
623
+ $order_items[] = array('product_id' => $product_id, 'qty' => $qty, 'total' => $total, 'meta' => $item_meta);
624
+
625
+ $i++;
626
+ }
627
+ }
628
+ } else {
629
+ //CSV Order Export format
630
+ $sku = $item['item_sku'];
631
+ $qty = $item['item_amount'];
632
+ $total = $item['row_price'];
633
+
634
+ if (!$sku || !$qty || !is_numeric($total)) {
635
+ // invalid item
636
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Row %d - %s - skipped. Missing SKU, quantity or total', 'wf_order_import_export'), $row, $item['order_id']));
637
+ $skipped++;
638
+ unset($item);
639
+ return; // break outer loop
640
+ }
641
+
642
+ // find by sku
643
+ $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value=%s LIMIT 1", $sku));
644
+
645
+ if (!$product_id) {
646
+ // unknown product
647
+ $WF_CSV_Order_Import->hf_order_log_data_change('order-csv-import', sprintf(__('> > Row %d - %s - skipped. Unknown order item: %s.', 'wf_order_import_export'), $row, $item['order_id'], $sku));
648
+ $skipped++;
649
+ unset($item);
650
+ return; // break outer loop
651
+ }
652
+
653
+ $order_items[] = array('product_id' => $product_id, 'qty' => $qty, 'total' => $total);
654
+ }
655
+
656
+ $tax_items = array();
657
+
658
+ // standard tax item format which supports multiple tax items in numbered columns containing a pipe-delimated, colon-labeled format
659
+ if (!empty($item['tax_item_1']) || !empty($item['tax_item'])) {
660
+ // one or more order tax items
661
+ // get the first tax item
662
+ $tax_item = !empty($item['tax_item_1']) ? $item['tax_item_1'] : $item['tax_item'];
663
+
664
+ $i = 1;
665
+ $tax_amount_sum = $shipping_tax_amount_sum = 0;
666
+ while ($tax_item) {
667
+
668
+ $tax_item_data = array();
669
+
670
+ // turn "label: Tax | tax_amount: 10" into an associative array
671
+ foreach (explode('|', $tax_item) as $piece) {
672
+ list( $name, $value ) = explode(':', $piece);
673
+ $tax_item_data[trim($name)] = trim($value);
674
+ }
675
+
676
+ // default rate id to 0 if not set
677
+ if (!isset($tax_item_data['rate_id'])) {
678
+ $tax_item_data['rate_id'] = 0;
679
+ }
680
+
681
+ // have a tax amount or shipping tax amount
682
+ if (isset($tax_item_data['tax_amount']) || isset($tax_item_data['shipping_tax_amount'])) {
683
+
684
+ // try and look up rate id by label if needed
685
+ if (isset($tax_item_data['label']) && $tax_item_data['label'] && !$tax_item_data['rate_id']) {
686
+ foreach ($tax_rates as $tax_rate) {
687
+
688
+ if (0 === strcasecmp($tax_rate->tax_rate_name, $tax_item_data['label'])) {
689
+ // found the tax by label
690
+ $tax_item_data['rate_id'] = $tax_rate->tax_rate_id;
691
+ break;
692
+ }
693
+ }
694
+ }
695
+
696
+ // check for a rate being specified which does not exist, and clear it out (technically an error?)
697
+ if ($tax_item_data['rate_id'] && !isset($tax_rates[$tax_item_data['rate_id']])) {
698
+ $tax_item_data['rate_id'] = 0;
699
+ }
700
+
701
+ // default label of 'Tax' if not provided
702
+ if (!isset($tax_item_data['label']) || !$tax_item_data['label']) {
703
+ $tax_item_data['label'] = 'Tax';
704
+ }
705
+
706
+ // default tax amounts to 0 if not set
707
+ if (!isset($tax_item_data['tax_amount'])) {
708
+ $tax_item_data['tax_amount'] = 0;
709
+ }
710
+ if (!isset($tax_item_data['shipping_tax_amount'])) {
711
+ $tax_item_data['shipping_tax_amount'] = 0;
712
+ }
713
+
714
+ // handle compound flag by using the defined tax rate value (if any)
715
+ if (!isset($tax_item_data['tax_rate_compound'])) {
716
+ $tax_item_data['tax_rate_compound'] = '';
717
+ if ($tax_item_data['rate_id']) {
718
+ $tax_item_data['tax_rate_compound'] = $tax_rates[$tax_item_data['rate_id']]->tax_rate_compound;
719
+ }
720
+ }
721
+
722
+ $tax_items[] = array(
723
+ 'title' => '',
724
+ 'rate_id' => $tax_item_data['rate_id'],
725
+ 'label' => $tax_item_data['label'],
726
+ 'compound' => $tax_item_data['tax_rate_compound'],
727
+ 'tax_amount' => $tax_item_data['tax_amount'],
728
+ 'shipping_tax_amount' => $tax_item_data['shipping_tax_amount'],
729
+ );
730
+
731
+ // sum up the order totals, in case it wasn't part of the import
732
+ $tax_amount_sum += $tax_item_data['tax_amount'];
733
+ $shipping_tax_amount_sum += $tax_item_data['shipping_tax_amount'];
734
+ }
735
+
736
+ // get the next tax item (if any)
737
+ $i++;
738
+ $tax_item = isset($item['tax_item_' . $i]) ? $item['tax_item_' . $i] : null;
739
+ }
740
+
741
+ if (!is_numeric($order_tax)) {
742
+ $order_tax = $tax_amount_sum;
743
+ }
744
+ if (!is_numeric($order_shipping_tax)) {
745
+ $order_shipping_tax = $shipping_tax_amount_sum;
746
+ }
747
+ }
748
+
749
+ // default to zero if not set
750
+ if (!is_numeric($order_tax)) {
751
+ $order_tax = 0;
752
+ }
753
+ if (!is_numeric($order_shipping_tax)) {
754
+ $order_shipping_tax = 0;
755
+ }
756
+
757
+ // no tax items specified, so create a default one using the tax totals
758
+ if (0 == count($tax_items)) {
759
+
760
+ $tax_items[] = array(
761
+ 'title' => '',
762
+ 'rate_id' => 0,
763
+ 'label' => 'Tax',
764
+ 'compound' => '',
765
+ 'tax_amount' => $order_tax,
766
+ 'shipping_tax_amount' => $order_shipping_tax,
767
+ );
768
+ }
769
+
770
+ // add the order tax totals to the order meta
771
+ $postmeta[] = array('key' => '_order_tax', 'value' => number_format((float) $order_tax, 2, '.', ''));
772
+ $postmeta[] = array('key' => '_order_shipping_tax', 'value' => number_format((float) $order_shipping_tax, 2, '.', ''));
773
+
774
+
775
+ if ($order) {
776
+ $order['postmeta'] = $postmeta;
777
+ $order['order_items'] = $order_items;
778
+ $order['order_shipping'] = $order_shipping_methods;
779
+ $order['tax_items'] = $tax_items;
780
+
781
+ // the order array will now contain the necessary name-value pairs for the wp_posts table, and also any meta data in the 'postmeta' array
782
+ $results[] = $order;
783
+ }
784
+ // Result
785
+ return array(
786
+ $this->post_type => $results,
787
+ 'skipped' => $skipped,
788
+ );
789
+ }
790
+
791
+ private function wc_get_order_statuses_neat() {
792
+
793
+ $order_statuses = array();
794
+
795
+ foreach (wc_get_order_statuses() as $slug => $name) {
796
+ $order_statuses[preg_replace('/^wc-/', '', $slug)] = $name;
797
+ }
798
 
799
+ return $order_statuses;
800
+ }
801
 
802
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/importer/class-wf-orderimpexpcsv-order-import.php CHANGED
@@ -577,7 +577,25 @@ class WF_OrderImpExpCsv_Order_Import extends WP_Importer {
577
  // Check if post exists when importing
578
  $new_added = false;
579
  $is_order_exist = $this->order_exists($post['order_number']);
 
 
 
 
 
 
580
 
 
 
 
 
 
 
 
 
 
 
 
 
581
  if (!$merging && $is_order_exist) {
582
  $usr_msg = 'Order already exists.';
583
  $this->add_import_result('skipped', __($usr_msg, 'wf_order_import_export'), $post['order_number'], $order_data['post_title'], $post['order_number']);
@@ -621,14 +639,19 @@ class WF_OrderImpExpCsv_Order_Import extends WP_Importer {
621
  $add_download_permissions = true;
622
  $meta_processed = true;
623
  }
 
 
 
 
 
624
 
625
  if ( ! $meta_processed ) {
626
  update_post_meta( $order_id, $meta['key'], $meta['value'] );
627
  }
628
 
629
  // set the paying customer flag on the user meta if applicable
630
- if ( '_customer_id' == $meta['key'] && $meta['value'] && in_array( $post['status'], array( 'processing', 'completed', 'refunded' ) ) ) {
631
- update_user_meta( $meta['value'], "paying_customer", 1 );
632
  }
633
  }
634
 
@@ -785,6 +808,30 @@ class WF_OrderImpExpCsv_Order_Import extends WP_Importer {
785
 
786
  unset( $post );
787
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788
  /**
789
  * Log a row's import status
790
  */
577
  // Check if post exists when importing
578
  $new_added = false;
579
  $is_order_exist = $this->order_exists($post['order_number']);
580
+ $meta_array = array();
581
+ if (!empty($post['postmeta'])) {
582
+ foreach ($post['postmeta'] as $meta) {
583
+ $meta_array[$meta['key']] = $meta['value'];
584
+ }
585
+ }
586
 
587
+ $user_id = $this->hf_check_customer($meta_array);
588
+ if (is_wp_error($user_id)) {
589
+ $this->hf_order_log_data_change('order-csv-import', __($user_id->get_error_message(), 'wf_order_import_export'));
590
+ $this->add_import_result('skipped', __($user_id->get_error_message(), 'wf_order_import_export'), $post['order_number'], $post['order_number'], $post['order_number']);
591
+ $skipped++;
592
+ unset($post);
593
+ return;
594
+ } elseif (empty($user_id)) {
595
+ $user_id = 0;
596
+ }
597
+
598
+
599
  if (!$merging && $is_order_exist) {
600
  $usr_msg = 'Order already exists.';
601
  $this->add_import_result('skipped', __($usr_msg, 'wf_order_import_export'), $post['order_number'], $order_data['post_title'], $post['order_number']);
639
  $add_download_permissions = true;
640
  $meta_processed = true;
641
  }
642
+
643
+ if (( '_customer_user' == $meta['key'])) {
644
+ update_post_meta($order_id, $meta['key'], $user_id);
645
+ $meta_processed = true;
646
+ }
647
 
648
  if ( ! $meta_processed ) {
649
  update_post_meta( $order_id, $meta['key'], $meta['value'] );
650
  }
651
 
652
  // set the paying customer flag on the user meta if applicable
653
+ if ( '_customer_id' == $meta['key'] && $user_id && in_array( $post['status'], array( 'processing', 'completed', 'refunded' ) ) ) {
654
+ update_user_meta( $user_id, "paying_customer", 1 );
655
  }
656
  }
657
 
808
 
809
  unset( $post );
810
  }
811
+
812
+
813
+ public function hf_check_customer($data) {
814
+
815
+ $customer_email = (!empty($data['_customer_email']) ) ? $data['_customer_email'] : '';
816
+
817
+ $found_customer = false;
818
+ $username = '';
819
+
820
+ if (!empty($customer_email)) {
821
+
822
+ if (is_email($customer_email) && false !== email_exists($customer_email)) {
823
+ $found_customer = email_exists($customer_email);
824
+ return $found_customer;
825
+ } else {
826
+ $found_customer = 0;
827
+ }
828
+ } else {
829
+ $found_customer = 0;
830
+ }
831
+
832
+ return $found_customer;
833
+ }
834
+
835
  /**
836
  * Log a row's import status
837
  */
includes/importer/data/data-wf-reserved-fields-pair.php CHANGED
@@ -21,7 +21,7 @@ return array(
21
  'order_currency' => 'order_currency | order_currency',
22
  'payment_method' => 'payment_method | payment_method',
23
  'shipping_method' => 'shipping_method | shipping_method',
24
- 'customer_id' => 'customer_id | customer_id',
25
  'customer_user' => 'customer_user | Customer id ( if not provided order will be created as Guest)',
26
  'billing_first_name' => 'billing_first_name | billing_first_name',
27
  'billing_last_name' => 'billing_last_name | billing_last_name',
21
  'order_currency' => 'order_currency | order_currency',
22
  'payment_method' => 'payment_method | payment_method',
23
  'shipping_method' => 'shipping_method | shipping_method',
24
+ 'customer_email' => 'customer_email | Customer Email ( if not provided order will be created as Guest)',
25
  'customer_user' => 'customer_user | Customer id ( if not provided order will be created as Guest)',
26
  'billing_first_name' => 'billing_first_name | billing_first_name',
27
  'billing_last_name' => 'billing_last_name | billing_last_name',
includes/importer/views/html-wf-import-options.php CHANGED
@@ -1,3 +1,4 @@
 
1
  <div class="wrap woocommerce">
2
  <div class="icon32" id="icon-woocommerce-importer"><br></div>
3
  <h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
1
+ <?php $tab = (isset($_GET['tab'])?$_GET['tab']:'import'); ?>
2
  <div class="wrap woocommerce">
3
  <div class="icon32" id="icon-woocommerce-importer"><br></div>
4
  <h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
order-import-export.php CHANGED
@@ -6,9 +6,9 @@ Plugin URI: https://www.xadapter.com/product/order-import-export-plugin-for-wooc
6
  Description: Export and Import Order detail including line items, From and To your WooCommerce Store.
7
  Author: XAdapter
8
  Author URI: https://www.xadapter.com/
9
- Version: 1.3.1
10
  Text Domain: wf_order_import_export
11
- WC tested up to: 3.3.5
12
  */
13
 
14
  if ( ! defined( 'ABSPATH' ) || ! is_admin() ) {
@@ -22,7 +22,7 @@ define("WF_CPN_IMP_EXP_ID", "wf_cpn_imp_exp");
22
  define("wf_coupon_csv_im_ex", "wf_coupon_csv_im_ex");
23
 
24
  if (!defined('WF_ORDERIMPEXP_CURRENT_VERSION')) {
25
- define("WF_ORDERIMPEXP_CURRENT_VERSION", "1.3.1");
26
  }
27
 
28
  /**
6
  Description: Export and Import Order detail including line items, From and To your WooCommerce Store.
7
  Author: XAdapter
8
  Author URI: https://www.xadapter.com/
9
+ Version: 1.3.2
10
  Text Domain: wf_order_import_export
11
+ WC tested up to: 3.4.0
12
  */
13
 
14
  if ( ! defined( 'ABSPATH' ) || ! is_admin() ) {
22
  define("wf_coupon_csv_im_ex", "wf_coupon_csv_im_ex");
23
 
24
  if (!defined('WF_ORDERIMPEXP_CURRENT_VERSION')) {
25
+ define("WF_ORDERIMPEXP_CURRENT_VERSION", "1.3.2");
26
  }
27
 
28
  /**
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Order Export & Order Import for WooCommerce ===
2
- Contributors: xadapter, mujeebur
3
  Donate link:
4
  Tags: Order Export, Order Import, WooCommerce Export Orders , WooCommerce Import Orders , Export Orders, Import Orders
5
  Requires at least: 3.0.1
6
- Tested up to: 4.9.5
7
- Stable tag: 1.3.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -112,6 +112,11 @@ Yes. You can import or export order line item details.
112
 
113
  == Changelog ==
114
 
 
 
 
 
 
115
  = 1.3.0 =
116
  * Fixed Coupon customer import issue.
117
  * WP 4.9.5 and WC 3.3.4 Tested OK
@@ -180,6 +185,5 @@ Yes. You can import or export order line item details.
180
  * Export /Import WooCommerce Orders.
181
 
182
  == Upgrade Notice ==
183
- = 1.3.0 =
184
- * Fixed Coupon customer import issue.
185
- * WP 4.9.5 and WC 3.3.4 Tested OK
1
  === Order Export & Order Import for WooCommerce ===
2
+ Contributors: xadapter, mujeebur, webtoffee
3
  Donate link:
4
  Tags: Order Export, Order Import, WooCommerce Export Orders , WooCommerce Import Orders , Export Orders, Import Orders
5
  Requires at least: 3.0.1
6
+ Tested up to: 4.9.6
7
+ Stable tag: 1.3.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
112
 
113
  == Changelog ==
114
 
115
+ = 1.3.2 =
116
+ * Bug fix.
117
+ = 1.3.1 =
118
+ * Fixed Order currency import issue.
119
+ * WP 4.9.5 and WC 3.3.5 Tested OK
120
  = 1.3.0 =
121
  * Fixed Coupon customer import issue.
122
  * WP 4.9.5 and WC 3.3.4 Tested OK
185
  * Export /Import WooCommerce Orders.
186
 
187
  == Upgrade Notice ==
188
+ = 1.3.2 =
189
+ * Bug fix.