forked from alexxxa/WooCommerce-Product-CSV-Import-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-csv-import.php
835 lines (642 loc) · 28.9 KB
/
product-csv-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
<?php
/*
Plugin Name: WooCommerce Product CSV Import Suite
Plugin URI: http://woothemes.com/woocommerce/
Description: Import and export products and variations straight from WordPress admin. Go to WooCommerce > CSV Import Suite to get started. Supports post fields, product data, custom post types, taxonomies, and images. More woocommerce extensions download from <a href="http://www.96down.com/">96down.com</a>.
Author: Mike Jolley
Author URI: http://mikejolley.com
Version: 1.2.2
Text Domain: wc_csv_importer
Copyright: © 2009-2011 WooThemes.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Adapted from the WordPress post importer by the WordPress team
*/
/**
* Required functions
*/
if ( ! function_exists( 'woothemes_queue_update' ) )
require_once( 'woo-includes/woo-functions.php' );
/**
* Plugin updates
*/
woothemes_queue_update( plugin_basename( __FILE__ ), '7ac9b00a1fe980fb61d28ab54d167d0d', '18680' );
/**
* Check WooCommerce exists
*/
if ( is_woocommerce_active() ) {
if ( ! is_admin() ) return;
/**
* Localisation
**/
load_plugin_textdomain( 'wc_csv_import', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
/**
* WC_CSV_Import_Suite class
**/
if ( ! class_exists( 'WC_Product_CSV_Import_Suite' ) ) {
class WC_Product_CSV_Import_Suite {
var $post_columns;
var $variation_columns;
var $errors = array();
public function __construct() {
add_action( 'admin_init', array( $this, 'init_fields' ), 5 );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_print_styles', array( $this, 'admin_scripts' ) );
}
function init_fields() {
// Post data to export
$this->post_columns = apply_filters('woocommerce_csv_product_post_columns', array(
'post_title' => 'post_title',
'post_name' => 'post_name',
'ID' => 'ID',
'post_excerpt' => 'post_excerpt',
'post_content' => 'post_content',
'post_status' => 'post_status',
'menu_order' => 'menu_order',
'post_date' => 'post_date',
'post_parent' => 'post_parent',
'comment_status' => 'comment_status',
// Meta
'_sku' => 'sku',
'_downloadable' => 'downloadable',
'_virtual' => 'virtual',
'_visibility' => 'visibility',
'_stock' => 'stock',
'_stock_status' => 'stock_status',
'_backorders' => 'backorders',
'_manage_stock' => 'manage_stock',
'_regular_price' => 'regular_price',
'_sale_price' => 'sale_price',
'_weight' => 'weight',
'_length' => 'length',
'_width' => 'width',
'_height' => 'height',
'_tax_status' => 'tax_status',
'_tax_class' => 'tax_class',
'_upsell_ids' => 'upsell_ids',
'_crosssell_ids' => 'crosssell_ids',
'_featured' => 'featured',
'_sale_price_dates_from' => 'sale_price_dates_from',
'_sale_price_dates_to' => 'sale_price_dates_to',
// Downloadable products
'_file_path' => 'file_path',
'_file_paths' => 'file_paths',
'_download_limit' => 'download_limit',
'_download_expiry' => 'download_expiry',
// Virtual products
'_product_url' => 'product_url',
'_button_text' => 'button_text',
// YOAST
'_yoast_wpseo_focuskw' => 'meta:_yoast_wpseo_focuskw',
'_yoast_wpseo_title' => 'meta:_yoast_wpseo_title',
'_yoast_wpseo_metadesc' => 'meta:_yoast_wpseo_metadesc',
'_yoast_wpseo_metakeywords' => 'meta:_yoast_wpseo_metakeywords',
));
// Post data to export
$this->variation_columns = apply_filters('woocommerce_csv_product_variation_post_columns', array(
'post_parent' => 'post_parent',
'ID' => 'ID',
'post_status' => 'post_status',
// Core product data
'_sku' => 'sku',
'_downloadable' => 'downloadable',
'_virtual' => 'virtual',
'_stock' => 'stock',
'_price' => 'price',
'_regular_price' => 'regular_price',
'_sale_price' => 'sale_price',
'_weight' => 'weight',
'_length' => 'length',
'_width' => 'width',
'_height' => 'height',
// Downloadable products
'_file_path' => 'file_path',
'_file_paths' => 'file_paths',
'_download_limit' => 'download_limit',
));
// 2.0
if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0", ">=" ) ) {
unset( $this->variation_columns['_price'] );
} else {
unset( $this->variation_columns['_regular_price'] );
}
}
/**
* Admin Init
*/
function admin_init() {
register_importer( 'woocommerce_csv', 'WooCommerce Products (CSV)', __('Import <strong>products</strong> to your store via a csv file.', 'wc_csv_import'), array( $this, 'product_importer' ) );
register_importer( 'woocommerce_variation_csv', 'WooCommerce Product Variations (CSV)', __('Import <strong>product variations</strong> to your store via a csv file.', 'wc_csv_import'), array( $this, 'variation_importer' ) );
if (!empty($_GET['action']) && !empty($_GET['page']) && $_GET['page']=='woocommerce_csv_import_suite') {
switch ($_GET['action']) {
case "export" :
$this->product_exporter( 'product' );
break;
case "export_variations" :
$this->product_exporter( 'product_variation' );
break;
}
}
}
/**
* Admin Menu
*/
function admin_menu() {
$page = add_submenu_page('woocommerce', __( 'CSV Import Suite', 'wc_csv_import' ), __( 'CSV Import Suite', 'wc_csv_import' ), 'manage_woocommerce', 'woocommerce_csv_import_suite', array($this, 'admin_page') );
//add_action('admin_print_styles-' . $page, array($this, 'admin_scripts'));
}
/**
* Admin Page
*/
function admin_scripts() {
global $woocommerce;
wp_enqueue_script( 'woocommerce_admin' );
wp_enqueue_script( 'chosen' );
wp_enqueue_style( 'woocommerce_admin_styles', $woocommerce->plugin_url() . '/assets/css/admin.css' );
wp_register_style( 'woocommerce-csv_importer', plugins_url( basename( plugin_dir_path(__FILE__) ) . '/css/style.css', basename( __FILE__ ) ), '', '1.0.0', 'screen' );
wp_enqueue_style( 'woocommerce-csv_importer' );
}
/**
* Admin Page
*/
function admin_page() {
global $woocommerce;
$tab = ( !empty($_GET['tab']) && $_GET['tab'] == 'export' ) ? 'export' : 'import';
?>
<div class="wrap woocommerce">
<div class="icon32" id="icon-woocommerce-importer"><br></div>
<h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
<a href="<?php echo admin_url('admin.php?page=woocommerce_csv_import_suite') ?>" class="nav-tab <?php echo ($tab == 'import') ? 'nav-tab-active' : ''; ?>"><?php _e('Import Products', 'wc_csv_import'); ?></a><a href="<?php echo admin_url('admin.php?page=woocommerce_csv_import_suite&tab=export') ?>" class="nav-tab <?php echo ($tab == 'export') ? 'nav-tab-active' : ''; ?>"><?php _e('Export Products', 'wc_csv_import'); ?></a>
</h2>
<?php
switch ($tab) {
case "export" :
$this->admin_export_page();
break;
default :
$this->admin_import_page();
break;
}
?>
</div>
<script type="text/javascript">
jQuery("select.chosen_select").chosen();
</script>
<?php
}
function admin_import_page() {
global $woocommerce;
?>
<div id="message" class="updated woocommerce-message wc-connect">
<div class="squeezer">
<h4><?php _e( '<strong>Product CSV Import Suite</strong> – Before getting started prepare your CSV files', 'woocommerce' ); ?></h4>
<p class="submit"><a href="http://www.woothemes.com/woocommerce-docs/user-guide/extensions/product-csv-import-suite/" class="button-primary"><?php _e( 'Documentation', 'woocommerce' ); ?></a> <a class="docs button-primary" href="<?php echo plugins_url( 'sample.csv', __FILE__ ); ?>"><?php _e('Sample CSV', 'woocommerce'); ?></a></p>
<p>
</div>
</div>
<div class="tool-box">
<h3 class="title"><?php _e('Import Product CSV', 'wc_csv_import'); ?></h3>
<p><?php _e('Import simple, grouped, external and variable products into WooCommerce using this tool.', 'wc_csv_import'); ?></p>
<p class="description"><?php _e('Upload a CSV from your computer. Click import to import your CSV as new products (existing products will be skipped), or click merge to merge products, ninja style. Importing requires the <code>post_title</code> column, whilst merging requires <code>sku</code> or <code>id</code>.', 'wc_csv_import'); ?></p>
<p class="submit"><a class="button" href="<?php echo admin_url('admin.php?import=woocommerce_csv'); ?>"><?php _e('Import Products', 'wc_csv_import'); ?></a> <a class="button" href="<?php echo admin_url('admin.php?import=woocommerce_csv&merge=1'); ?>"><?php _e('Merge Products', 'wc_csv_import'); ?></a></p>
</div>
<div class="tool-box">
<h3 class="title"><?php _e('Import Product Variations CSV', 'wc_csv_import'); ?></h3>
<p><?php _e('Import and add variations to your variable products using this tool.', 'wc_csv_import'); ?></p>
<p class="description"><?php _e('Each row must be mapped to a variable product via a <code>post_parent</code> or <code>parent_sku</code> column in order to import successfully. Merging also requires a <code>sku</code> or <code>id</code> column.', 'wc_csv_import'); ?></p>
<p class="submit"><a class="button" href="<?php echo admin_url('admin.php?import=woocommerce_variation_csv'); ?>"><?php _e('Import Variations', 'wc_csv_import'); ?></a> <a class="button" href="<?php echo admin_url('admin.php?import=woocommerce_variation_csv&merge=1'); ?>"><?php _e('Merge Variations', 'wc_csv_import'); ?></a></p>
</div>
<?php
}
function admin_export_page() {
global $woocommerce;
?>
<div class="tool-box">
<h3 class="title"><?php _e('Export Product CSV', 'wc_csv_import'); ?></h3>
<p><?php _e('Export your products using this tool. This exported CSV will be in an importable format.', 'wc_csv_import'); ?></p>
<p class="description"><?php _e('Click export to save your products to your computer.', 'wc_csv_import'); ?></p>
<form action="<?php echo admin_url('admin.php?page=woocommerce_csv_import_suite&action=export'); ?>" method="post">
<table class="form-table">
<tr>
<th>
<label for="v_limit"><?php _e( 'Limit', 'wc_csv_import' ); ?></label>
</th>
<td>
<input type="text" name="limit" id="v_limit" placeholder="<?php _e('Unlimited', 'wc_csv_import'); ?>" class="input-text" />
</td>
</tr>
<tr>
<th>
<label for="v_offset"><?php _e( 'Offset', 'wc_csv_import' ); ?></label>
</th>
<td>
<input type="text" name="offset" id="v_offset" placeholder="<?php _e('0', 'wc_csv_import'); ?>" class="input-text" />
</td>
</tr>
<tr>
<th>
<label for="v_columns"><?php _e( 'Columns', 'wc_csv_import' ); ?></label>
</th>
<td>
<select id="v_columns" name="columns[]" data-placeholder="<?php _e('All Columns', 'wc_csv_import'); ?>" class="chosen_select" multiple="multiple">
<?php
foreach ($this->post_columns as $key => $column) {
echo '<option value="'.$key.'">'.$column.'</option>';
}
echo '<option value="images">'.__('Images (featured and gallery)', 'wc_csv_import').'</option>';
echo '<option value="taxonomies">'.__('Taxonomies (cat/tags/shipping-class)', 'wc_csv_import').'</option>';
echo '<option value="attributes">'.__('Attributes', 'wc_csv_import').'</option>';
echo '<option value="meta">'.__('Meta (custom fields)', 'wc_csv_import').'</option>';
?>
</select>
</td>
</tr>
</table>
<p class="submit"><input type="submit" class="button" value="<?php _e('Export Products', 'wc_csv_import'); ?>" /></p>
</form>
</div>
<div class="tool-box">
<h3 class="title"><?php _e('Export Product Variations CSV', 'wc_csv_import'); ?></h3>
<p><?php _e('Export your product variations using this tool. This exported CSV will be in an importable format.', 'wc_csv_import'); ?></p>
<p class="description"><?php _e('Click export to save your products variations to your computer.', 'wc_csv_import'); ?></p>
<form action="<?php echo admin_url('admin.php?page=woocommerce_csv_import_suite&action=export_variations'); ?>" method="post">
<table class="form-table">
<tr>
<th>
<label for="limit"><?php _e( 'Limit', 'wc_csv_import' ); ?></label>
</th>
<td>
<input type="text" name="limit" id="limit" placeholder="<?php _e('Unlimited', 'wc_csv_import'); ?>" class="input-text" />
</td>
</tr>
<tr>
<th>
<label for="offset"><?php _e( 'Offset', 'wc_csv_import' ); ?></label>
</th>
<td>
<input type="text" name="offset" id="offset" placeholder="<?php _e('0', 'wc_csv_import'); ?>" class="input-text" />
</td>
</tr>
<tr>
<th>
<label for="columns"><?php _e( 'Columns', 'wc_csv_import' ); ?></label>
</th>
<td>
<select id="columns" name="columns[]" data-placeholder="<?php _e('All Columns', 'wc_csv_import'); ?>" class="chosen_select" multiple="multiple">
<?php
foreach ($this->variation_columns as $key => $column) {
echo '<option value="'.$key.'">'.$column.'</option>';
}
echo '<option value="images">'.__('Images (featured and gallery)', 'wc_csv_import').'</option>';
echo '<option value="taxonomies">'.__('Taxonomies (cat/tags/shipping-class)', 'wc_csv_import').'</option>';
echo '<option value="meta">'.__('Meta (custom fields)', 'wc_csv_import').'</option>';
?>
</select>
</td>
</tr>
</table>
<p class="submit"><input type="submit" class="button" value="<?php _e('Export Variations', 'wc_csv_import'); ?>" /></p>
</form>
</div>
<?php
}
/**
* Product Importer Tool
*/
function product_importer() {
if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) return;
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( ! class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) ) require $class_wp_importer;
}
/**
* Variation Importer Tool
*/
function variation_importer() {
if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) return;
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( ! class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) ) require $class_wp_importer;
}
// includes
require dirname( __FILE__ ) . '/classes/class-wc-product-import.php';
require dirname( __FILE__ ) . '/classes/class-wc-product_variation-import.php';
require dirname( __FILE__ ) . '/classes/class-wc-csv-parser.php';
require dirname( __FILE__ ) . '/classes/class-wc-csv-log.php';
// Dispatch
global $WC_CSV_Product_Import;
$WC_CSV_Product_Import = new WC_CSV_Product_Variation_Import();
$WC_CSV_Product_Import->dispatch();
}
/**
* Product Exporter Tool
*/
function product_exporter( $post_type = 'product' ) {
global $wpdb;
$csv_columns = ( $post_type == 'product' ) ? $this->post_columns : $this->variation_columns;
$product_taxonomies = get_object_taxonomies( $post_type, 'name' );
$export_columns = ( ! empty($_POST['columns'] ) ) ? $_POST['columns'] : '';
$product_args = array(
'numberposts' => ( ! empty($_POST['limit'] ) ) ? intval( $_POST['limit'] ) : -1,
'post_status' => array( 'publish', 'pending', 'private', 'draft' ),
'post_type' => $post_type,
'orderby' => 'ID',
'order' => 'ASC',
'offset' => ( ! empty($_POST['offset'] ) ) ? intval( $_POST['offset'] ) : 0
);
if ( $post_type == 'product_variation' ) {
$product_args['orderby'] = 'post_parent menu_order';
}
$products = get_posts( $product_args );
if ( ! $products || is_wp_error( $products ) ) {
$this->errors[] = __('Nothing found to export', 'wc_csv_import');
return;
}
// Get all metadata
$all_meta_keys = $this->get_all_metakeys( $post_type );
$found_attributes = $this->get_all_product_attributes( $post_type );
$wpdb->hide_errors();
@set_time_limit(0);
@ob_clean();
header( 'Content-Type: text/csv; charset=UTF-8' );
header( 'Content-Disposition: attachment; filename=woocommerce-product-export.csv' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
$fp = fopen('php://output', 'w');
// Loop products and load meta data
$found_product_meta = array();
// Some of the values may not be usable (e.g. arrays of arrays) but the worse
// that can happen is we get an empty column.
foreach ( $all_meta_keys as $meta ) {
if ( ! $meta ) continue;
if ( ! in_array( $meta, array_keys( $csv_columns ) ) && substr( $meta, 0, 1 ) == '_' ) continue;
$found_product_meta[] = $meta;
}
// Loop products
foreach ( $products as $product ) {
$meta_data = get_post_custom( $product->ID );
$product->meta = new stdClass;
$product->attributes = new stdClass;
// Meta data
foreach ( $meta_data as $meta => $value ) {
if ( ! $meta ) continue;
if ( ! in_array( $meta, array_keys( $csv_columns ) ) && substr( $meta, 0, 1 ) == '_' ) continue;
$meta_value = maybe_unserialize( maybe_unserialize( $value[0] ) );
if ( is_array( $meta_value ) ) {
if ( ! isset( $meta_value[0] ) || is_array( $meta_value[0] ) )
continue; // Can't handle multidimensional arrays
$meta_value = implode( '|', $meta_value );
}
$product->meta->$meta = $this->format_export_meta( $meta_value, $meta );
}
// Product attributes
if ( isset( $meta_data['_product_attributes'][0] ) ) {
$attributes = maybe_unserialize(maybe_unserialize( $meta_data['_product_attributes'][0] ));
if ( ! empty( $attributes ) && is_array( $attributes ) ) foreach ( $attributes as $key => $attribute ) {
if ( ! $key || ! isset( $attribute['position'] ) || ! isset( $attribute['is_visible'] ) || ! isset( $attribute['is_variation'] ) ) continue;
if ( $attribute['is_taxonomy'] == 1 ) {
$terms = wp_get_post_terms( $product->ID, $key, array("fields" => "names") );
if ( !is_wp_error( $terms ) ) {
$attribute_value = implode( '|', $terms );
} else {
$attribute_value = '';
}
} else {
$key = $attribute['name'];
$attribute_value = $attribute['value'];
}
$attribute_data = $attribute['position'] . '|' . $attribute['is_visible'] . '|' . $attribute['is_variation'];
$_default_attributes = isset( $meta_data['_default_attributes'][0] ) ? maybe_unserialize( maybe_unserialize( $meta_data['_default_attributes'][0] ) ) : '';
if ( is_array( $_default_attributes ) ) {
$_default_attribute = isset( $_default_attributes[ $key ] ) ? $_default_attributes[ $key ] : '';
} else {
$_default_attribute = '';
}
$product->attributes->$key = array(
'value' => $attribute_value,
'data' => $attribute_data,
'default' => $_default_attribute
);
}
}
// GPF
if ( isset( $meta_data['_woocommerce_gpf_data'][0] ) ) {
$product->gpf_data = $meta_data['_woocommerce_gpf_data'][0];
}
}
$found_product_meta = array_diff( $found_product_meta, array_keys( $csv_columns ) );
// Variable to hold the CSV data we're exporting
$row = array();
if ( $post_type == 'product_variation' ) {
$row[] = 'Parent';
$row[] = 'parent_sku';
}
// Export header rows
foreach ( $csv_columns as $column => $value ) {
if ( ! $export_columns || in_array( $column, $export_columns ) ) $row[] = esc_attr( $value );
}
// Handle special fields like taxonomies
if ( ! $export_columns || in_array( 'images', $export_columns ) ) {
$row[] = 'images';
}
if ( ! $export_columns || in_array( 'taxonomies', $export_columns ) ) {
foreach ( $product_taxonomies as $taxonomy ) {
if ( strstr( $taxonomy->name, 'pa_' ) ) continue; // Skip attributes
$row[] = 'tax:' . $this->format_data( $taxonomy->name );
}
}
if ( ! $export_columns || in_array( 'meta', $export_columns ) ) {
foreach ( $found_product_meta as $product_meta ) {
$row[] = 'meta:' . $this->format_data( $product_meta );
}
}
if ( ! $export_columns || in_array( 'attributes', $export_columns ) ) {
foreach ( $found_attributes as $attribute ) {
$row[] = 'attribute:' . $this->format_data( $attribute );
$row[] = 'attribute_data:' . $this->format_data( $attribute );
$row[] = 'attribute_default:' . $this->format_data( $attribute );
}
}
if ( function_exists( 'woocommerce_gpf_install' ) ) {
$row[] = 'gpf:availability';
$row[] = 'gpf:condition';
$row[] = 'gpf:brand';
$row[] = 'gpf:product_type';
$row[] = 'gpf:google_product_category';
$row[] = 'gpf:gtin';
$row[] = 'gpf:mpn';
$row[] = 'gpf:gender';
$row[] = 'gpf:age_group';
$row[] = 'gpf:color';
$row[] = 'gpf:size';
}
//$headers[] = '"' . implode( '","', $row ) . '"';
$row = array_map( array( $this, 'wrap_column' ), $row );
fwrite( $fp, implode( ',', $row ) . "\n" );
//fputcsv( $fp, $row, ',', '"' );
unset( $row );
// Loop products
foreach ( $products as $product ) {
$row = array();
if ( $post_type == 'product_variation' ) {
$post_parent_title = get_the_title( $product->post_parent );
if ( ! $post_parent_title ) continue;
$row[] = $this->format_data( $post_parent_title );
$parent_sku = get_post_meta( $product->post_parent, '_sku', true );
$row[] = $parent_sku;
}
// Get column values
foreach ( $csv_columns as $column => $value ) {
if ( ! $export_columns || in_array( $column, $export_columns ) ) {
if ( $post_type == 'product_variation' && $column == '_regular_price' && empty( $product->meta->$column ) )
$column = '_price';
if ( isset( $product->meta->$column ) ) {
$row[] = $this->format_data( $product->meta->$column );
} elseif ( isset( $product->$column ) && ! is_array( $product->$column ) ) {
$row[] = $this->format_data( $product->$column );
} else {
$row[] = '';
}
}
}
// Export images/gallery
if ( ! $export_columns || in_array( 'images', $export_columns ) ) {
$image_file_names = array();
// Featured image
if ( $featured_image_id = get_post_thumbnail_id( $product->ID ) ) {
$image_file_names[] = current( wp_get_attachment_image_src( $featured_image_id, 'full' ) );
}
// Images
$images = get_children( array('post_parent' => $product->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
$results = array();
if ( $images ) {
foreach ( $images as $image ) {
if ( $featured_image_id==$image->ID ) continue;
$image_file_names[] = current( wp_get_attachment_image_src( $image->ID, 'full' ) );
}
}
$row[] = implode( '|', $image_file_names );
}
// Export taxonomies
if ( ! $export_columns || in_array( 'taxonomies', $export_columns ) ) {
foreach ( $product_taxonomies as $taxonomy ) {
if ( strstr( $taxonomy->name, 'pa_' ) ) continue; // Skip attributes
$terms = wp_get_post_terms( $product->ID, $taxonomy->name, array("fields" => "names") );
$row[] = $this->format_data( implode( '|', $terms ) );
}
}
// Find and export attributes
if ( ! $export_columns || in_array( 'attributes', $export_columns ) ) {
foreach ( $found_attributes as $attribute ) {
if ( isset( $product->attributes ) && isset( $product->attributes->$attribute ) ) {
$values = $product->attributes->$attribute;
$row[] = $this->format_data( $values['value'] );
$row[] = $this->format_data( $values['data'] );
$row[] = $this->format_data( $values['default'] );
} else {
$row[] = '';
$row[] = '';
$row[] = '';
}
}
}
// Export GPF
if ( function_exists( 'woocommerce_gpf_install' ) ) {
$gpf_data = empty( $product->gpf_data ) ? '' : maybe_unserialize( $product->gpf_data );
$row[] = empty( $gpf_data['availability'] ) ? '' : $gpf_data['availability'];
$row[] = empty( $gpf_data['condition'] ) ? '' : $gpf_data['condition'];
$row[] = empty( $gpf_data['brand'] ) ? '' : $gpf_data['brand'];
$row[] = empty( $gpf_data['product_type'] ) ? '' : $gpf_data['product_type'];
$row[] = empty( $gpf_data['google_product_category'] ) ? '' : $gpf_data['google_product_category'];
$row[] = empty( $gpf_data['gtin'] ) ? '' : $gpf_data['gtin'];
$row[] = empty( $gpf_data['mpn'] ) ? '' : $gpf_data['mpn'];
$row[] = empty( $gpf_data['gender'] ) ? '' : $gpf_data['gender'];
$row[] = empty( $gpf_data['age_group'] ) ? '' : $gpf_data['age_group'];
$row[] = empty( $gpf_data['color'] ) ? '' : $gpf_data['color'];
$row[] = empty( $gpf_data['size'] ) ? '' : $gpf_data['size'];
}
// Add to csv
//$csv[] = '"' . implode( '","', $row ) . '"';
$row = array_map( array( $this, 'wrap_column' ), $row );
fwrite( $fp, implode( ',', $row ) . "\n" );
//fputcsv( $fp, $row, ',', '"' );
unset( $row );
}
fclose( $fp );
exit;
}
function format_export_meta( $meta_value, $meta ) {
if ( $meta == '_sale_price_dates_from' || $meta == '_sale_price_dates_to' ) {
if ( $meta_value ) return date( 'Y-m-d', $meta_value );
}
return $meta_value;
}
function format_data( $data ) {
$data = (string) $data;
$enc = mb_detect_encoding( $data, 'UTF-8, ISO-8859-1', true );
$data = ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
return $data;
}
function wrap_column( $data ) {
return '"' . str_replace( '"', '""', $data ) . '"';
}
/**
* Get a list of all the meta keys for a post type. This includes all public, private,
* used, no-longer used etc. They will be sorted once fetched.
*/
function get_all_metakeys( $post_type = 'product' ) {
global $wpdb;
$meta = $wpdb->get_col( $wpdb->prepare(
"SELECT DISTINCT pm.meta_key
FROM {$wpdb->postmeta} AS pm
LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status IN ( 'publish', 'pending', 'private', 'draft' )",
$post_type
) );
sort( $meta );
return $meta;
}
/**
* Get a list of all the product attributes for a post type.
* These require a bit more digging into the values.
*/
function get_all_product_attributes( $post_type = 'product' ) {
global $wpdb;
$results = $wpdb->get_col( $wpdb->prepare(
"SELECT DISTINCT pm.meta_value
FROM {$wpdb->postmeta} AS pm
LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status IN ( 'publish', 'pending', 'private', 'draft' )
AND pm.meta_key = '_product_attributes'",
$post_type
) );
// Go through each result, and look at the attribute keys within them.
$result = array();
if ( ! empty( $results ) ) {
foreach( $results as $_product_attributes ) {
$attributes = maybe_unserialize( maybe_unserialize( $_product_attributes ) );
if ( ! empty( $attributes ) && is_array( $attributes ) ) {
foreach( $attributes as $key => $attribute ) {
if ( ! $key || ! isset( $attribute['position'] ) || ! isset( $attribute['is_visible'] ) || ! isset( $attribute['is_variation'] ) )
continue;
if ( ! strstr( $key, 'pa_' ) )
$key = $attribute['name'];
$result[ $key ] = $key;
}
}
}
}
sort( $result );
return $result;
}
}
}
$GLOBALS['wc_product_csv_import'] = new WC_Product_CSV_Import_Suite();
}