· 6 years ago · Jun 10, 2019, 11:10 PM
1<?php
2
3/*
4
5We are not checking against existing products in the database when importing. If you need to bulk update products on a certain category, delete all existing products in the category, then re-upload fresh.
6
7$category_identifier_string needs to contain the car model and the start of the parts collection title as shown in database. Example: "246 Dino 001"
8
9*/
10
11function sb_get_part_category_diagram_id( $category_identifier_string ) {
12 global $wpdb;
13
14 $diagram_id = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE '%s';", $category_identifier_string . '%' ) );
15
16 return $diagram_id ? $diagram_id[0] : false;
17}
18
19function sb_set_part_category_diagram( $part_category_id, $model_name, $part_category_name ) {
20 $part_category_diagram_lookup_string = $model_name . ' ' . explode( ' ', $part_category_name )[0];
21
22 $part_category_diagram_id = sb_get_part_category_diagram_id( $part_category_diagram_lookup_string );
23
24 if( $part_category_diagram_id ) {
25 update_term_meta( $part_category_id, 'thumbnail_id', $part_category_diagram_id );
26 }
27}
28
29// Helper function in sb_import_products
30function sb_convert_csv_row_to_array( $row ) {
31 // This is to strip out html entities that were popping up in the data, presumably resulting from different encoding
32 $row = preg_replace( "/\xEF\xBB\xBF/", "", $row );
33
34 $part_data = str_getcsv( $row );
35
36 // Ignore empty or header rows
37 if ( $part_data[0] == '' || strtolower( $part_data[0] ) == 'model' ) {
38 return false;
39 }
40
41 return [
42 'model_name' => trim( $part_data[0] ),
43 'part_category_name' => $part_data[1],
44 'part_diagram_number' => intval( $part_data[2] ) ?: 'N/A',
45 'part_sku' => $part_data[3],
46 'part_fitted_quantity' => intval( $part_data[4] ) ?: 'N/A',
47 'part_title' => $part_data[5],
48 ];
49}
50
51// Helper function in sb_import_products
52function sb_get_model_id( $model_name ) {
53
54 // Go try to find the model ID (Check if model exists in the first place)
55 $model_term_array = get_terms([
56 'taxonomy' => 'product_cat',
57 'hide_empty' => false,
58 'parent' => get_term_by( 'slug', 'ferrari', 'product_cat' )->term_id,
59 'name' => $model_name,
60 ]);
61
62 if( $model_term_array ) {
63 return $model_term_array[0]->term_id;
64 }
65
66 // The model does not exist, so create it and return the ID
67 $args = [
68 'parent' => get_term_by( 'slug', 'ferrari', 'product_cat' )->term_id,
69 ];
70
71 $term_info = wp_insert_term( $model_name, 'product_cat', $args );
72
73 if ( is_wp_error( $term_info ) ) {
74
75 return -1;
76
77 } else {
78
79 return $term_info['term_id'];
80
81 }
82}
83
84// Helper function in sb_import_products
85function sb_get_part_category_id( $model_id, $part_category_name ) {
86
87 // Look for existing part category. If we find one, return the ID
88 $part_category_term_array = get_terms([
89 'taxonomy' => 'product_cat',
90 'hide_empty' => false,
91 'parent' => $model_id,
92 'name' => $part_category_name,
93 ]);
94
95 if( $part_category_term_array ) {
96 return $part_category_term_array[0]->term_id;
97 }
98
99 // If we did not find an existing part category, create it and return the ID
100 $args = [
101 'parent' => $model_id,
102 ];
103
104 $term_info = wp_insert_term( $part_category_name, 'product_cat', $args );
105
106 if ( is_wp_error( $term_info ) ) {
107
108 return -1;
109
110 } else {
111
112 return $term_info['term_id'];
113
114 }
115}
116
117function sb_import_products() {
118 global $wpdb;
119
120 sb_debug_log_clear();
121 sb_debug_log( 'Starting Import' );
122
123 set_time_limit ( 120 * 60 ); // 120 minutes * 60 seconds
124
125 //https://wordpress.stackexchange.com/questions/9624/optimize-post-insert-and-delete-for-bulk-operations
126 wp_defer_term_counting( true );
127 sb_debug_log( 'Defer Term Counting' );
128
129 $disabled_keys = $wpdb->query("ALTER TABLE `stellar_wp_posts` DISABLE KEYS");
130 sb_debug_log( 'Disabled Keys: ' . $disabled_keys );
131
132 // grab all files from import folder
133 $import_url = sb_dir_root() . '/imports/*';
134
135 $product_csvs = glob( $import_url );
136
137 $num_product_csvs = count( $product_csvs );
138 echo "<h2>$num_product_csvs .csv product import CSVs found.</h2>";
139
140 // for each file
141 foreach ( $product_csvs as $csv ) {
142
143 if ( $csv_contents = file_get_contents( $csv ) ) {
144
145 sb_debug_log( 'FILE LOADED: ' . $csv );
146
147 $csv_rows = explode( PHP_EOL, $csv_contents );
148
149 $reversed_csv_rows = array_reverse( $csv_rows );
150 $num_rows = count( $reversed_csv_rows );
151
152 sb_debug_log( 'ROW COUNT: ' . $num_rows );
153
154 echo "<h3>$csv</h3><h4>$num_rows rows found</h4>";
155
156 $model_id = -1;
157 $part_category_id = -1;
158 $stored_part_category_name = '';
159
160 foreach( $reversed_csv_rows as $row ) {
161
162 $row_array = sb_convert_csv_row_to_array( $row );
163
164 // Ignore empty or header rows
165 if ( ! $row_array ) {
166 sb_debug_log( 'Skipping Row' );
167 continue;
168 }
169
170 extract( $row_array );
171
172 if ( $model_id == -1 ) {
173
174 sb_debug_log( 'Checking if ' . $model_name . ' exists. If not, creating it' );
175
176 // Check if car model exists. If not, create it
177 $model_id = sb_get_model_id( $model_name );
178
179 // If we couldn't create the model category, skip this row and continue to the next one
180 if( -1 == $model_id ) {
181 continue;
182 }
183
184 }
185
186 if ( $part_category_id == -1 || $stored_part_category_name != $part_category_name ) {
187
188 sb_debug_log( 'Checking if ' . $part_category_name . ' exists. If not, creating it' );
189
190 // Check if part category exists. If not, create it
191 $part_category_id = sb_get_part_category_id( $model_id, $part_category_name );
192
193 // If we couldn't create the part category, skip this row and continue to the next one
194 if( -1 == $part_category_id ) {
195 continue;
196 }
197
198 $stored_part_category_name = $part_category_name;
199
200 sb_set_part_category_diagram( $part_category_id, $model_name, $part_category_name );
201 }
202
203 $product_insertion_data = [
204 'post_status' => 'publish',
205 'post_content' => '',
206 'post_type' => 'product',
207 'post_title' => $part_title,
208 'meta_input' => [
209 '_sku' => $part_sku,
210 '_regular_price' => 1,
211 '_price' => 1,
212 ],
213 ];
214
215 $field_data = [
216 'diagram_number' => $part_diagram_number,
217 'fitted_quantity' => $part_fitted_quantity,
218 ];
219
220 $part_post_id = wp_insert_post( $product_insertion_data );
221
222 $post_data = [
223 'ID' => $part_post_id,
224 ];
225
226 CFS()->save( $field_data, $post_data );
227
228 wp_set_object_terms( $part_post_id, $part_category_id, 'product_cat' );
229
230 }
231 }
232 }
233
234 $enabled_keys = $wpdb->query("ALTER TABLE `stellar_wp_posts` ENABLE KEYS");
235 sb_debug_log( 'Enabled Keys: ' . $enabled_keys );
236
237 wp_defer_term_counting( false );
238 sb_debug_log( 'Enable Term Counting' );
239
240 sb_debug_log( 'Finishing Import' );
241}
242
243
244?>