-
Notifications
You must be signed in to change notification settings - Fork 0
/
fillauer-product-importer.php
468 lines (423 loc) · 11.5 KB
/
fillauer-product-importer.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
<?php
/**
* Plugin Name: Fillauer Product Importer
* Description: Import a CSV file to update products on the database
* Version: 1.8.8
*/
// Create menu
function importer_menu() {
add_menu_page( 'Product Importer', 'Product Importer', 'manage_options', 'product-options', 'product_import_page' );
add_submenu_page( 'Product Importer', 'Product Importer', 'Product Importer', 'Product Importer', 'Product Importer' );
}
function register_processor() {
wp_register_script( 'product-import-processor', plugins_url( 'dist/scripts/client.js', __FILE__ ), '1.0.0', true );
wp_register_style( 'product-import-styles', plugins_url( 'dist/styles/global.min.css', __FILE__ ), '1.0.0', true );
wp_register_style( 'product-import-styles-min', plugins_url( 'dist/styles/global.min.css', __FILE__ ), '1.0.0', true );
// Pass nounce for WP API Authentication
wp_localize_script(
'product-import-processor',
'wpApiSettings',
[
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'lang' => ICL_LANGUAGE_CODE,
]
);
wp_enqueue_script( 'product-import-processor' );
wp_enqueue_style( 'product-import-styles' );
}
function update_variations( $value, $prod, $field_name ) {
// New JSON
/*
variations = [
{
sku: 143-342-AC
name: 'asdf'
image: false
indent: ''
specs: {
color: 'blue',
weight: '10 g'
}
},
{
sku: 143-342-DF
name: 'fdsa'
image: 424
indent: ''
specs: {
color: 'red',
weight: '8 g'
}
}
]
*/
// error_log('Variation global dump: '.print_r($value,true));
$spec_labels = $value['labels'];
$json = [];
foreach ( $value['varies'] as $key => $varies ) {
// Create Variation
if ( ! array_key_exists( 'sku', $varies ) ) {
error_log( 'sku not found' );
continue;
}
$group = [
'sku' => $varies['sku'],
'name' => htmlspecialchars( $varies['name'] ),
'image' => array_key_exists( 'image', $varies ) ? $varies['image'] : false,
'indention' => array_key_exists( 'indent', $varies ) ? $varies['indent'] : '',
'specs' => [],
];
// Add Specifications for each variation entry
if ( array_key_exists( 'specs', $varies ) ) {
foreach ( $varies['specs'] as $index => $val ) {
// return true;
if ( $val === '' ) {
continue;
}
$spec = [
'spec_label' => $spec_labels[ $index ],
'spec_value' => $val,
];
array_push( $group['specs'], $spec );
}
} else {
error_log( 'Variation update fail: ' . print_r( $varies, true ) );
}
array_push( $json, $group );
}
update_field( 'variation_json', json_encode( $json ), $prod->ID );
return true;
};
function update_gallery( $value, $prod, $field_name ) {
$featured = true;
foreach ( $value as $item ) {
if ( $featured && ! strpos( $item, 'http' ) ) {
set_post_thumbnail( $prod->ID, $item );
}
add_row( 'gallery_list', [ 'asset_id' => $item ], $prod->ID );
$featured = false;
}
}
function update_warranty( $value, $prod, $field_name ) {
if ( array_key_exists( 'body', $value ) ) {
update_field( 'warranty_body', $value['body'], $prod->ID );
}
if ( array_key_exists( 'list', $value ) ) {
foreach ( $value['list'] as $item ) {
add_row( 'warranty_list', [ 'warranty_item' => $item ], $prod->ID );
}
}
}
function update_l_codes( $value, $prod, $field_name ) {
_log( $value, $prod, $field_name );
if ( ! empty( $value ) ) {
update_field( 'suggested_l_codes', nl2br( $value ), $prod->ID );
}
}
function update_features( $value, $prod, $field_name ) {
foreach ( $value as $item ) {
add_row( 'product_feats', [ 'product_feat' => $item ], $prod->ID );
}
}
function update_related_products( $value, $prod, $field_name ) {
foreach ( $value as $item ) {
add_row( 'related_products', [ 'related_product' => $item ], $prod->ID );
}
}
function update_indications( $value, $prod, $field_name ) {
foreach ( $value as $item ) {
add_row( 'indication_list', [ 'indication_item' => $item ], $prod->ID );
}
}
function update_downloads( $value, $prod, $field_name ) {
foreach ( $value as $item ) {
if ( array_key_exists( 'title', $item ) && array_key_exists( 'url', $item ) ) {
add_row(
'download_list',
[
'download' => [
'title' => $item['title'],
'url' => $item['url'],
],
],
$prod->ID
);
}
}
}
function update_region( $value, $prod, $field_name ) {
$valid_languages = apply_filters( 'wpml_active_languages', null, 'orderby=id&order=desc' ) ?: [];
$lang_codes = array_keys( $valid_languages );
if ( false == in_array( $value, $lang_codes ) ) {
error_log( '====== IMPORTER ======' );
error_log( 'Invalid language attempted: ' . print_r( $value, true ) );
error_log( '' );
return false;
}
$wpml_element_type = apply_filters( 'wpml_element_type', 'product' );
$args = [
'element_id' => $prod->ID,
'element_type' => $wpml_element_type,
'language_code' => $value,
];
do_action( 'wpml_set_element_language_details', $args );
}
function update_specs( $value, $prod, $field_name ) {
// error_log( 'specs ' . print_r( $value, true ) );
foreach ( $value as $key => $val ) {
// error_log( 'specs key' . print_r( $key, true ) );
// error_log( 'specs val' . print_r( $val, true ) );
$group[] = [
'spec_label' => $key,
'spec_value' => $val['val'],
'logo' => array_key_exists( 'icon', $val ) ? $val['icon'] : 'cog',
'featured' => array_key_exists( 'featured', $val ) ? $val['featured'] : false,
];
update_field( 'specifications', $group, $prod->ID );
}
return true;
}
function update_packages( $value, $prod, $field_name ) {
$package_index = 1;
foreach ( $value as $key => $value ) {
// error_log( 'package key ' . print_r( $key, true ) );
// error_log( 'package value ' . print_r( $value, true ) );
// Read product_info to find which fields to toggle
$descOn = false;
$featsOn = false;
$imageOn = false;
$table_image = false;
if ( key_exists( 'image', $value ) ) {
$table_image = $value['image'];
}
foreach ( $value['product_info'] as $val ) {
switch ( $val ) {
case 'name':
$descOn = true;
break;
case 'description':
$descOn = true;
break;
case 'features':
$featsOn = true;
break;
case 'image':
$imageOn = true;
break;
}
}
// Create Package
$group[] = [
'title' => $value['label'],
'package_pic' => $value['pic'],
'model' => $value['model'],
'product_info_name' => $descOn,
'product_info_description' => $descOn,
'product_info_features' => $featsOn,
'product_info_image' => $imageOn,
'table_image' => $table_image,
];
update_field( 'packages', $group, $prod->ID );
// Add SKUs
foreach ( $value['skus'] as $val ) {
add_sub_row(
[ 'packages', $package_index, 'skus' ],
[ 'sku' => $val ],
$prod->ID
);
}
// Add Headers (for model B accessory tables)
foreach ( $value['specs'] as $val ) {
add_sub_row(
[ 'packages', $package_index, 'headers' ],
[ 'header' => $val ],
$prod->ID
);
}
$package_index += 1;
}
return true;
}
function update_taxonomies( $value, $prod, $field_name ) {
foreach ( $value as $key => $val ) {
if ( taxonomy_exists( $key ) ) {
// Checks that hierarchal terms exist before associating with product.
if ( 'product_cat' === $key ) {
foreach ( $val as $cat ) {
$terms = explode( '>', $cat );
if ( ! empty( $terms ) ) {
$parent_term = '';
$insert = [];
foreach ( $terms as $term ) {
$term = trim( $term );
if ( ! term_exists( $term, 'product_cat' ) ) {
$args = [];
// If parent is not empty creates this term as a child of the parent.
if ( ! empty( $parent_term ) ) {
$parent_term = get_term_by( 'name', $parent_term, 'product_cat' );
$args = [
'parent' => $parent_term->term_id,
];
}
// Inserts the term.
wp_insert_term( $term, 'product_cat', $args );
}
$term_object = get_term_by( 'name', $term, 'product_cat' );
$insert[] = $term_object->term_id;
$parent_term = $term;
}
// Attaches all terms at once.
wp_set_object_terms( $prod->ID, $insert, $key, true );
}
}
} else {
// Just attach it to this product.
wp_set_object_terms( $prod->ID, $val, $key );
}
} else {
if ( function_exists( '_log' ) ) {
_log( 'Line Number: ' . __LINE__ . ' - Taxonomy ' . $key . ' does not exist.' );
}
}
}
return true;
}
add_action(
'rest_api_init',
function () {
register_rest_field(
'product',
'meta',
[
'get_callback' => function( $data ) {
$listing_obj = get_post_meta( $data['id'], '', false );
return $listing_obj;
},
'update_callback' => function( $value, $listing_obj, $field_name ) {
// Delete existing fields, if the product is being re-imported
//! THIS DOES NOT ACCOUNT FOR:
// Taxonomies,
// Title, Desc (Content) and Short Desc (Excerpt) will
// always post/overwrite with empty strings if absent
// we only need the ACF field names
$ACF_fields = get_fields( $listing_obj->ID ) ?: [];
$ACF_keys = array_keys( $ACF_fields );
foreach ( $ACF_keys as $ACF_key ) {
delete_field( $ACF_key, $listing_obj->ID );
}
// Assign meta data
foreach ( $value as $key => $value ) {
update_post_meta( $listing_obj->ID, $key, $value );
}
return true;
},
'schema' => null,
]
);
register_rest_field(
'product',
'gallery',
[
'update_callback' => 'update_gallery',
'schema' => null,
]
);
register_rest_field(
'product',
'specs',
[
'update_callback' => 'update_specs',
'schema' => null,
]
);
register_rest_field(
'product',
'packages',
[
'update_callback' => 'update_packages',
'schema' => null,
]
);
register_rest_field(
'product',
'variations',
[
'update_callback' => 'update_variations',
'schema' => null,
]
);
register_rest_field(
'product',
'warranty',
[
'update_callback' => 'update_warranty',
'schema' => null,
]
);
register_rest_field(
'product',
'related',
[
'update_callback' => 'update_related_products',
'schema' => null,
]
);
register_rest_field(
'product',
'suggested_l_codes',
[
'update_callback' => 'update_l_codes',
'schema' => null,
]
);
register_rest_field(
'product',
'features',
[
'update_callback' => 'update_features',
'schema' => null,
]
);
register_rest_field(
'product',
'indications',
[
'update_callback' => 'update_indications',
'schema' => null,
]
);
register_rest_field(
'product',
'downloads',
[
'update_callback' => 'update_downloads',
'schema' => null,
]
);
register_rest_field(
'product',
'region',
[
'update_callback' => 'update_region',
'schema' => null,
]
);
register_rest_field(
'product',
'terms',
[
'update_callback' => 'update_taxonomies',
'schema' => null,
]
);
}
);
add_action( 'admin_menu', 'importer_menu' );
add_action( 'importer_enqueue_scripts', 'register_processor' );
// ^^^ is creating a new action like this ok?
// Just to call it on plugin page, like below
function product_import_page() {
do_action( 'importer_enqueue_scripts' );
include 'plugin-page.php';
}