PluginProbe
Welcart e-Commerce / 2.12.4
Welcart e-Commerce v2.12.4
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
← All changes | functions/item_post.php +3202 -2057 1.3.72.12.4 View file →
@@ -1,2057 +1,3202 @@
1 -<?php
2 -function usces_get_post_meta_by_metaid( $meta_id ) {
3 - global $wpdb;
4 - $res = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id), ARRAY_A);
5 - return $res;
6 -}
7 -function usces_get_post_meta( $post_id, $key ) {
8 - global $wpdb;
9 - $res = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key), ARRAY_A);
10 - return $res;
11 -}
12 -function usces_sort_post_meta( $post_id, $metastr ) {
13 - global $wpdb;
14 - $meta_ids = explode(',', $metastr);
15 - if( !empty($meta_ids) ){
16 - $i = 0;
17 - foreach( $meta_ids as $meta_id ){
18 -
19 - $rows = usces_get_post_meta_by_metaid( $meta_id );
20 - $values = unserialize($rows['meta_value']);
21 - $values['sort'] = $i;
22 - $serialized_values = serialize($values);
23 - $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $serialized_values, $rows['meta_id']) );
24 - $i++;
25 - }
26 - }
27 - return;
28 -}
29 -
30 -/**
31 - * item option
32 - */
33 -//function has_item_option_meta( $postid ) {
34 -function usces_get_opts( $post_id, $keyflag = 'sort' ) {
35 - $opts = array();
36 - $metas = usces_get_post_meta($post_id, '_iopt_');
37 -
38 - if( empty($metas) ) return $opts;
39 -
40 - foreach( $metas as $rows ){
41 - $values = unserialize($rows['meta_value']);
42 - $key = isset($values[$keyflag]) ? $values[$keyflag] : $values['sort'];
43 - $opts[$key] = array(
44 - 'meta_id' => $rows['meta_id'],
45 - 'name' => $values['name'],
46 - 'means' => $values['means'],
47 - 'essential' => $values['essential'],
48 - 'value' => $values['value'],
49 - 'sort' => $values['sort']
50 - );
51 - }
52 - ksort($opts);
53 -
54 - return $opts;
55 -}
56 -function usces_add_opt( $post_id, $newvalue, $check = true ) {
57 - global $wpdb, $usces;
58 - if( $check ){
59 - $metas = usces_get_post_meta($post_id, '_iopt_');
60 - if( !empty($metas) ){
61 - $meta_num = count($metas);
62 - $unique = true;
63 - $sortnull = true;
64 - foreach( $metas as $meta ){
65 - $values = unserialize($meta['meta_value']);
66 - if( $values['name'] == $newvalue['name'] )
67 - $unique = false;
68 - if( !isset($values['sort']) )
69 - $sortnull = false;
70 - $sort[] = $values['sort'];
71 - }
72 - if( !$unique )
73 - return -1;
74 -
75 - rsort($sort);
76 - $next_number = reset($sort) + 1;
77 - $unique_sort = array_unique($sort);
78 - if( $meta_num !== count($unique_sort) || $meta_num !== $next_number || !$sortnull){
79 - //To repair the sort data
80 - $i = 0;
81 - foreach( $metas as $rows ){
82 - $values = unserialize($rows['meta_value']);
83 - $values['sort'] = $i;
84 - $serialized_values = serialize($values);
85 - $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $serialized_values, $rows['meta_id']) );
86 - $i++;
87 - }
88 - }
89 - }
90 - $newvalue['sort'] = !empty($meta_num) ? $meta_num : 0;
91 - }
92 - $serialized_newvalue = serialize($newvalue);
93 - $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value ) VALUES (%d, '_iopt_', %s)", $post_id, $serialized_newvalue) );
94 - $id = $wpdb->insert_id;
95 - return $id;
96 -}
97 -
98 -/**
99 - * item sku
100 - */
101 -function usces_add_sku( $post_id, $newvalue, $check = true ) {
102 - global $wpdb, $usces;
103 - if( $check ){
104 - $metas = usces_get_post_meta($post_id, '_isku_');
105 - if( !empty($metas) ){
106 - $meta_num = count($metas);
107 - $unique = true;
108 - $sortnull = true;
109 - foreach( $metas as $rows ){
110 - $values = unserialize($rows['meta_value']);
111 - if( $values['code'] == $newvalue['code'] )
112 - $unique = false;
113 - if( !isset($values['sort']) )
114 - $sortnull = false;
115 - $sort[] = $values['sort'];
116 - }
117 - if( !$unique )
118 - return -1;
119 -
120 - rsort($sort);
121 - $next_number = $sort[0] + 1;
122 - $unique_sort = array_unique($sort);
123 - if( $meta_num != count($unique_sort) || $meta_num != $next_number || !$sortnull){
124 - //To repair the sort data
125 - $i = 0;
126 - foreach( $metas as $rows ){
127 - $values = unserialize($rows['meta_value']);
128 - $values['sort'] = $i;
129 - $serialized_values = serialize($values);
130 - $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $serialized_values, $rows['meta_id']) );
131 - $i++;
132 - }
133 - }
134 - }
135 - $newvalue['sort'] = !empty($meta_num) ? $meta_num : 0;
136 - }
137 - $newvalue = $usces->stripslashes_deep_post($newvalue);
138 - $serialized_newvalue = serialize($newvalue);
139 - $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value ) VALUES (%d, '_isku_', %s)", $post_id, $serialized_newvalue) );
140 - $id = $wpdb->insert_id;
141 - return $id;
142 -}
143 -
144 -
145 -/**
146 - * list_item_option
147 - */
148 -function list_item_option_meta( $opts ) {
149 - // Exit if no meta
150 - if ( ! $opts ) {
151 - ?>
152 - <table id="optlist-table" class="list" style="display: none;">
153 - <thead>
154 - <tr>
155 - <th class="hanldh"> </th>
156 - <th class="item-opt-key"><?php _e('option name','usces') ?></th>
157 - <th class="item-opt-value"><?php _e('selected amount','usces') ?></th>
158 - </tr>
159 - </thead>
160 - <tbody id="item-opt-list">
161 - <tr><td></td></tr>
162 - </tbody>
163 - </table>
164 - <?php
165 - }else{
166 - ?>
167 - <table id="optlist-table" class="list">
168 - <thead>
169 - <tr>
170 - <th class="hanldh"> </th>
171 - <th class="item-opt-key"><?php _e('option name','usces') ?></th>
172 - <th class="item-opt-value"><?php _e('selected amount','usces') ?></th>
173 - </tr>
174 - </thead>
175 - <tbody id="item-opt-list">
176 - <?php
177 - foreach ( $opts as $opt )
178 - echo _list_item_option_meta_row( $opt );
179 - ?>
180 - </tbody>
181 - </table>
182 - <?php
183 - }
184 -}
185 -
186 -/**
187 - * list_item_sku
188 - */
189 -function list_item_sku_meta( $skus ) {
190 - // Exit if no meta
191 - if ( empty( $skus ) ) {
192 - ?>
193 - <table id="skulist-table" class="list" style="display: none;">
194 - <thead>
195 - <tr>
196 - <th class="hanldh" rowspan="2"> </th>
197 - <th><?php _e('SKU code','usces'); ?></th>
198 - <th><?php echo apply_filters('usces_filter_listprice_label', __('normal price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
199 - <th><?php echo apply_filters('usces_filter_sellingprice_label', __('Sale price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
200 - <th><?php _e('stock','usces'); ?></th>
201 - <th><?php _e('stock status', 'usces'); ?></th>
202 - </tr>
203 - </thead>
204 - <tbody id="item-sku-list">
205 - <tr><td></td><td></td><td></td><td></td><td></td></tr>
206 - </tbody>
207 - </table>
208 - <?php
209 - }else{
210 - ?>
211 - <table id="skulist-table" class="list">
212 - <thead>
213 - <tr>
214 - <th class="hanldh" rowspan="2"> </th>
215 - <th class="item-sku-key"><?php _e('SKU code','usces'); ?></th>
216 - <th class="item-sku-cprice"><?php echo apply_filters('usces_filter_listprice_label', __('normal price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
217 - <th class="item-sku-price"><?php echo apply_filters('usces_filter_sellingprice_label', __('Sale price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
218 - <th class="item-sku-zaikonum"><?php _e('stock','usces'); ?></th>
219 - <th class="item-sku-zaiko"><?php _e('stock status','usces'); ?></th>
220 - </tr>
221 - <tr>
222 - <th><?php _e('SKU display name ','usces'); ?></th>
223 - <th><?php _e('unit','usces'); ?></th>
224 - <?php
225 - $advance_title = '<th colspan="2">&nbsp;</th>';
226 - echo apply_filters('usces_filter_sku_meta_form_advance_title', $advance_title);
227 - ?>
228 - <th><?php _e('Apply business package','usces'); ?></th>
229 - </tr>
230 - </thead>
231 - <tbody id="item-sku-list">
232 - <?php
233 - foreach ( $skus as $sku )
234 - echo _list_item_sku_meta_row( $sku );
235 - ?>
236 - </tbody>
237 - </table>
238 - <?php
239 - }
240 -}
241 -
242 -
243 -
244 -/**
245 - * option meta row
246 - */
247 -function _list_item_option_meta_row( $opt ) {
248 - $r = '';
249 - $style = '';
250 - $means = get_option('usces_item_option_select');
251 -
252 -// $opt['meta_value'] = unserialize( $opt['meta_value'] );
253 -
254 - $readonly = " readonly='true'";
255 - $name = esc_attr($opt['name']);
256 - $meansoption = '';
257 - foreach($means as $meankey => $meanvalue){
258 - if($meankey == $opt['means']) {
259 - $selected = ' selected="selected"';
260 - }else{
261 - $selected = '';
262 - }
263 - $meansoption .= '<option value="' . esc_attr($meankey) . '"' . $selected . '>' . esc_html($meanvalue) . "</option>\n";
264 - }
265 - $essential = $opt['essential'] == 1 ? " checked='checked'" : "";
266 - $value = '';
267 - if(is_array($opt['value'])){
268 - foreach($opt['value'] as $k => $v){
269 - $value .= $v . "\n";
270 - }
271 - }else{
272 - //$value = esc_attr(trim($opt['value']));
273 - $value = $opt['value'];
274 - }
275 - $value = trim($value);
276 - $id = (int) $opt['meta_id'];
277 - $sort = (int) $opt['sort'];
278 -
279 - ob_start();
280 - ?>
281 - <tr class="metastuffrow"><td colspan="3">
282 - <table id="itemopt-<?php echo $id; ?>" class="metastufftable">
283 - <tr>
284 - <th class='handlb' rowspan='2'> </th>
285 - <td class='item-opt-key'>
286 - <div><input name='itemopt[<?php echo $id; ?>][name]' id='itemopt[<?php echo $id; ?>][name]' class='metaboxfield' type='text' size='20' value='<?php echo $name; ?>'{$readonly} /></div>
287 - <div class='optcheck'>
288 - <select name='itemopt[<?php echo $id; ?>][means]' id='itemopt[<?php echo $id; ?>][means]'><?php echo $meansoption; ?></select>
289 - <label for='itemopt[<?php echo $id; ?>][essential]'><input name='itemopt[<?php echo $id; ?>][essential]' id='itemopt[<?php echo $id; ?>][essential]' type='checkbox' value='1'<?php echo $essential; ?> class='metaboxcheckfield' /><?php _e('Required','usces'); ?></label>
290 - </div>
291 - </td>
292 - <td class='item-opt-value'>
293 - <textarea name='itemopt[<?php echo $id; ?>][value]' id='itemopt[<?php echo $id; ?>][value]' class='metaboxfield'><?php echo esc_html($value); ?></textarea>
294 - </td>
295 - </tr>
296 - <tr>
297 - <td colspan='2' class='submittd'>
298 - <div id='itemoptsubmit-<?php echo $id; ?>' class='submit'>
299 - <input name='deleteitemopt[<?php echo $id; ?>]' id='deleteitemopt[<?php echo $id; ?>]' type='button' value='<?php esc_attr_e(__( 'Delete' )); ?>' onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('deleteitemopt', <?php echo $id; ?>);" />
300 - <input name='updateitemopt[<?php echo $id; ?>]' id='updateitemopt[<?php echo $id; ?>]' type='button' value='<?php esc_attr_e(__( 'Update' )); ?>' onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('updateitemopt', <?php echo $id; ?>);" />
301 - <input name='itemopt[<?php echo $id; ?>][sort]' id='itemopt[<?php echo $id; ?>][sort]' type='hidden' value='<?php echo $sort; ?>' />
302 - </div>
303 - <div id='itemopt_loading-<?php echo $id; ?>' class='meta_submit_loading'></div>
304 - </td>
305 - </tr>
306 - </table>
307 - </td></tr>
308 - <?php
309 - $r = ob_get_contents();
310 - ob_end_clean();
311 - return $r;
312 -}
313 -
314 -/**
315 - * sku meta row
316 - */
317 -function _list_item_sku_meta_row( $sku ) {
318 - $r = '';
319 - $style = '';
320 -
321 - $readonly = "";
322 - $key = esc_attr($sku['code']);
323 - $cprice = $sku['cprice'];
324 - $price = $sku['price'];
325 - $zaikonum = $sku['stocknum'];
326 - $zaiko = $sku['stock'];
327 - $skudisp = esc_attr($sku['name']);
328 - $skuunit = esc_attr($sku['unit']);
329 - $skugptekiyo = $sku['gp'];
330 - //$charging_type = $sku['meta_value']['charging_type'];
331 - $id = (int)$sku['meta_id'];
332 - $zaikoselectarray = get_option('usces_zaiko_status');
333 - $sort = (int) $sku['sort'];
334 -
335 - ob_start();
336 - ?>
337 - <tr class='metastuffrow'><td colspan='6'>
338 - <table id='itemsku-<?php echo $id; ?>' class='metastufftable'>
339 - <tr>
340 - <th class='handlb' rowspan='<?php echo apply_filters( 'usces_filter_sku_meta_rowspan', '3' ); ?>'> </th>
341 - <td class='item-sku-key'><input name='itemsku[<?php echo $id; ?>][key]' id='itemsku[<?php echo $id; ?>][key]' class='skuname metaboxfield' type='text' value='<?php echo $key; ?>'{$readonly} /></td>
342 - <td class='item-sku-cprice'><input name='itemsku[<?php echo $id; ?>][cprice]' id='itemsku[<?php echo $id; ?>][cprice]' class='skuprice metaboxfield' type='text' value='<?php echo $cprice; ?>' /></td>
343 - <td class='item-sku-price'><input name='itemsku[<?php echo $id; ?>][price]' id='itemsku[<?php echo $id; ?>][price]' class='skuprice metaboxfield' type='text' value='<?php echo $price; ?>' /></td>
344 - <td class='item-sku-zaikonum'><input name='itemsku[<?php echo $id; ?>][zaikonum]' id='itemsku[<?php echo $id; ?>][zaikonum]' class='skuzaikonum metaboxfield' type='text' value='<?php echo $zaikonum; ?>' /></td>
345 - <td class='item-sku-zaiko'>
346 - <select id='itemsku[<?php echo $id; ?>][zaiko]' name='itemsku[<?php echo $id; ?>][zaiko]' class='skuzaiko metaboxfield'>
347 - <?php
348 - for ( $i=0; $i<count($zaikoselectarray); $i++ ) {
349 - $selected = ( $i == $zaiko ) ? " selected='selected'" : '';
350 - ?>
351 - <option value='<?php echo $i; ?>'<?php echo $selected; ?>><?php echo $zaikoselectarray[$i]; ?></option>
352 - <?php
353 - }
354 - ?>
355 - </select>
356 - </td>
357 - </tr>
358 - <tr>
359 - <td class='item-sku-key'><input name='itemsku[<?php echo $id; ?>][skudisp]' id='itemsku[<?php echo $id; ?>][skudisp]' class='skudisp metaboxfield' type='text' value='<?php echo $skudisp; ?>' />
360 - </td>
361 - <td class='item-sku-cprice'><input name='itemsku[<?php echo $id; ?>][skuunit]' id='itemsku[<?php echo $id; ?>][skuunit]' class='skuunit metaboxfield' type='text' value='<?php echo $skuunit; ?>' /></td>
362 - <?php
363 - $default_field = "\n\t\t<td colspan='2'>&nbsp;</td>";
364 - echo apply_filters('usces_filter_sku_meta_row_advance', $default_field, $sku);
365 - ?>
366 - <td class='item-sku-zaiko'>
367 - <select id='itemsku[<?php echo $id; ?>][skugptekiyo]' name='itemsku[<?php echo $id; ?>][skugptekiyo]' class='skugptekiyo metaboxfield'>
368 - <option value='0' <?php echo ($skugptekiyo == 0 ? " selected='selected'" : ""); ?>><?php _e('Not apply','usces'); ?></option>
369 - <option value='1' <?php echo ($skugptekiyo == 1 ? " selected='selected'" : ""); ?>><?php _e('Apply','usces'); ?></option>
370 - </select>
371 - </td>
372 - </tr>
373 - <?php echo apply_filters( 'usces_filter_sku_meta_row', '', $sku ); ?>
374 - <tr>
375 - <td colspan='5' class='submittd'>
376 - <div id='skusubmit-<?php echo $id; ?>' class='submit'>
377 - <input name='deleteitemsku[<?php echo $id; ?>]' id='deleteitemsku[<?php echo $id; ?>]' type='button' value='<?php esc_attr_e(__( 'Delete' )) ?>' onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('deleteitemsku', <?php echo $id; ?>);" />
378 - <input name='updateitemsku[<?php echo $id; ?>]' id='updateitemsku[<?php echo $style; ?>]' type='button' value='<?php esc_attr_e(__( 'Update' )); ?>' onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('updateitemsku', <?php echo $id; ?>);" />
379 - <input name='itemsku[<?php echo $id; ?>][sort]' id='itemsku[<?php echo $id; ?>][sort]' type='hidden' value='<?php echo $sort; ?>' />
380 - </div>
381 - <div id='itemsku_loading-<?php echo $id; ?>' class='meta_submit_loading'></div>
382 - </td>
383 - </tr>
384 - </table>
385 - </td></tr>
386 - <?php
387 - $r = ob_get_contents();
388 - ob_end_clean();
389 - return $r;
390 -}
391 -
392 -
393 -/**
394 - * common_option_meta_form
395 - */
396 -function common_option_meta_form() {
397 - $means = get_option('usces_item_option_select');
398 - $meansoption = '';
399 - foreach($means as $meankey => $meanvalue){
400 - $meansoption .= '<option value="' . esc_attr($meankey) . '">' . esc_html($meanvalue) . "</option>\n";
401 - }
402 - ?>
403 - <div id="itemopt_ajax-response"></div>
404 - <p><strong><?php _e('Add a new option','usces') ?> : </strong></p>
405 - <table id="newmeta2">
406 - <thead>
407 - <tr>
408 - <th class="left"><label for="metakeyselect"><?php _e('option name','usces') ?></label></th>
409 - <th><label for="metavalue"><?php _e('selected amount','usces') ?></label></th>
410 - </tr>
411 - </thead>
412 -
413 - <tbody>
414 - <tr>
415 - <td class='item-opt-key'>
416 - <input type="text" id="newoptname" name="newoptname" class="metaboxfield" tabindex="7" value="" />
417 - <div class="optcheck">
418 - <select name='newoptmeans' id='newoptmeans' class="metaboxfield long"><?php echo $meansoption; ?></select>
419 - <label for='newoptessential'><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php _e('Required','usces') ?></label>
420 - </div>
421 - </td>
422 - <td class='item-opt-value'><textarea id="newoptvalue" name="newoptvalue" class='metaboxfield'></textarea></td>
423 - </tr>
424 -
425 - <tr>
426 - <td colspan="2" class="submittd">
427 - <div id='newcomoptsubmit' class='submit'>
428 - <input name="add_comopt" type="button" id="add_comopt" tabindex="9" value="<?php _e('Add common options','usces') ?>" onclick="itemOpt.post('addcommonopt', 0);" />
429 - </div>
430 - <div id="newcomopt_loading" class="meta_submit_loading"></div>
431 - </td>
432 - </tr>
433 - </tbody>
434 - </table>
435 - <?php
436 -}
437 -
438 -/**
439 - * item_option_meta_form
440 - */
441 -function item_option_meta_form() {
442 - global $wpdb;
443 - $usces_options = get_option('usces');
444 - $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
445 - $cart_number = (int)get_option('usces_cart_number');
446 - $opts = usces_get_opts($cart_number);
447 - $means = get_option('usces_item_option_select');
448 - $meansoption = '';
449 - foreach($means as $meankey => $meanvalue){
450 - $meansoption .= '<option value="' . esc_attr($meankey) . '">' . esc_html($meanvalue) . "</option>\n";
451 - }
452 - ?>
453 - <div id="itemopt_ajax-response"></div>
454 - <p><strong><?php _e('Applicable product options','usces') ?> : </strong></p>
455 - <table id="newmeta2">
456 - <thead>
457 - <tr>
458 - <th class="item-opt-key"><label for="metakeyselect"><?php _e('option name','usces') ?></label></th>
459 - <th class="item-opt-value"><label for="metavalue"><?php _e('selected amount','usces') ?></label></th>
460 - </tr>
461 - </thead>
462 -
463 - <tbody>
464 - <tr>
465 - <td class='item-opt-key'>
466 - <?php if ( !empty($opts) ) { ?>
467 - <select id="optkeyselect" name="optkeyselect" class="optkeyselect metaboxfield" tabindex="7" onchange="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('keyselect', this.value);">
468 - <option value="#NONE#"><?php _e( '-- Select --','usces' ); ?></option>
469 - <?php foreach ( $opts as $opt ){ ?>
470 - <option value='<?php echo $opt['meta_id']; ?>'><?php esc_attr_e($opt['name']); ?></option>
471 - <?php } ?>
472 - </select>
473 - <input type="hidden" id="newoptname" name="newoptname" class="metaboxfield" />
474 - <div class="optcheck">
475 - <select name='newoptmeans' id='newoptmeans'><?php echo $meansoption; ?></select>
476 - <label for='newoptessential'><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php _e('Required','usces') ?></label>
477 - </div>
478 - <?php } else { ?>
479 - <p><?php _e('Please create a common option.','usces') ?></p>
480 - <?php } ?>
481 - </td>
482 - <td class='item-opt-value'><textarea id="newoptvalue" name="newoptvalue" class='metaboxfield'></textarea></td>
483 - </tr>
484 -
485 - <tr>
486 - <td colspan="2" class="submittd">
487 - <?php if( is_array($opts) ) { ?>
488 - <div id='newitemoptsubmit' class='submit'>
489 - <input name="add_itemopt" type="button" id="add_itemopt" tabindex="9" value="<?php _e('Apply an option','usces') ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('additemopt', 0);" />
490 - </div>
491 - <div id="newitemopt_loading" class="meta_submit_loading"></div>
492 - <?php } ?>
493 - </td>
494 - </tr>
495 - </tbody>
496 - </table>
497 - <?php
498 -}
499 -
500 -/**
501 - * item_sku_meta_form
502 - */
503 -function item_sku_meta_form() {
504 - ?>
505 - <div id="sku_ajax-response"></div>
506 - <p><strong><?php _e('Add new SKU','usces') ?> : </strong></p>
507 - <table id="newsku">
508 - <thead>
509 - <tr>
510 - <th class="left"><?php _e('SKU code','usces') ?></th>
511 - <th><?php echo apply_filters('usces_filter_listprice_label', __('normal price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
512 - <th><?php echo apply_filters('usces_filter_sellingprice_label', __('Sale price','usces'), NULL, NULL); ?>(<?php usces_crcode(); ?>)</th>
513 - <th><?php _e('stock','usces') ?></th>
514 - <th><?php _e('stock status','usces') ?></th>
515 - </tr>
516 - <tr>
517 - <th><?php _e('SKU display name ','usces') ?></th>
518 - <th><?php _e('unit','usces') ?></th>
519 - <?php
520 - $advance_title = '<th colspan="2">&nbsp;</th>';
521 - echo apply_filters('usces_filter_sku_meta_form_advance_title', $advance_title);
522 - ?>
523 - <th><?php _e('Apply business package','usces') ?></th>
524 - </tr>
525 - </thead>
526 -
527 - <tbody>
528 - <tr>
529 - <td id="newskuleft" class='item-sku-key'><input type="text" id="newskuname" name="newskuname" class="newskuname metaboxfield"value="" /></td>
530 - <td class='item-sku-cprice'><input type="text" id="newskucprice" name="newskucprice" class='newskuprice metaboxfield' /></td>
531 - <td class='item-sku-price'><input type="text" id="newskuprice" name="newskuprice" class='newskuprice metaboxfield' /></td>
532 - <td class='item-sku-zaikonum'><input type="text" id="newskuzaikonum" name="newskuzaikonum" class='newskuzaikonum metaboxfield' /></td>
533 - <td class='item-sku-zaiko'>
534 - <select id="newskuzaikoselect" name="newskuzaikoselect" class="newskuzaikoselect metaboxfield">
535 - <?php
536 - $zaikoselectarray = get_option('usces_zaiko_status');
537 - foreach ( $zaikoselectarray as $v => $l ) {
538 - echo "\n<option value='" . esc_attr($v) . "'>" . esc_html($l) . "</option>";
539 - }
540 - ?>
541 - </select>
542 - </td>
543 - </tr>
544 - <tr>
545 - <td class='item-sku-key'><input type="text" id="newskudisp" name="newskudisp" class="newskudisp metaboxfield" /></td>
546 - <td class='item-sku-cprice'><input type="text" id="newskuunit" name="newskuunit" class='newskuunit metaboxfield' /></td>
547 - <?php
548 - $advance_field = '<td class="item-sku-price">&nbsp;</td><td class="item-sku-zaikonum">&nbsp;</td>';
549 - echo apply_filters('usces_filter_sku_meta_form_advance_field', $advance_field );
550 - ?>
551 - <td class='item-sku-zaiko'>
552 - <select id="newskugptekiyo" name="newskugptekiyo" class="newskugptekiyo metaboxfield">
553 - <option value="0"><?php _e('Not apply','usces') ?></option>
554 - <option value="1"><?php _e('Apply','usces') ?></option>
555 - </select>
556 - </td>
557 - </tr>
558 - <?php echo apply_filters( 'usces_filter_newsku_meta_row', '' ); ?>
559 - <tr>
560 - <td colspan="5" class="submittd">
561 - <div id='newskusubmit' class='submit'><input name="add_itemsku" type="button" id="add_itemsku" tabindex="9" value="<?php _e('Add SKU','usces') ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('additemsku', 0);" /></div>
562 - <div id="newitemsku_loading" class="meta_submit_loading"></div>
563 - </td>
564 - </tr>
565 - </tbody>
566 - </table>
567 - <?php
568 -}
569 -
570 -
571 -//
572 -// Post Meta
573 -//
574 -
575 -/**
576 - * add_item_option_meta
577 - */
578 -function add_item_option_meta( $post_ID ) {
579 - global $usces;
580 -
581 - $post_ID = (int) $post_ID;
582 - $value = array();
583 - $opts = array();
584 - $protected = array( '#NONE#', '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
585 -
586 - $newoptname = isset($_POST['newoptname']) ? trim( $_POST['newoptname'] ) : '';
587 - $newoptmeans = isset($_POST['newoptmeans']) ? (int)$_POST['newoptmeans']: 0;
588 - $newoptessential = isset($_POST['newoptessential']) ? $_POST['newoptessential']: 0;
589 - $newoptvalue = isset($_POST['newoptvalue']) ? trim($_POST['newoptvalue']) : '';
590 -
591 - if ( ($newoptmeans >= 2 || WCUtils::is_zero($newoptvalue) || !empty ( $newoptvalue )) && !empty ( $newoptname) ) {
592 -
593 - if ( $newoptname )
594 - $metakey = $newoptname; // default
595 -
596 - if ( in_array($metakey, $protected) )
597 - return false;
598 -
599 - wp_cache_delete($post_ID, 'post_meta');
600 -
601 - $value['name'] = str_replace("\\",'',$newoptname);
602 - $value['means'] = $newoptmeans;
603 - $value['essential'] = $newoptessential;
604 - $value['value'] = str_replace("\\",'',$newoptvalue);
605 - $value = $usces->stripslashes_deep_post($value);
606 -
607 - $id = usces_add_opt($post_ID, $value);
608 -
609 - return $id;
610 - }
611 - return false;
612 -} // add_meta
613 -
614 -/**
615 - * add_item_sku_meta
616 - */
617 -function add_item_sku_meta( $post_ID ) {
618 - global $usces;
619 -
620 - $post_ID = (int) $post_ID;
621 - $value = array();
622 - $skus = array();
623 - $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
624 -
625 - $newskuname = isset($_POST['newskuname']) ? trim( $_POST['newskuname'] ) : '';
626 - $newskucprice = isset($_POST['newskucprice']) ? $_POST['newskucprice']: '';
627 - $newskuprice = isset($_POST['newskuprice']) ? $_POST['newskuprice']: '';
628 - $newskuzaikonum = isset($_POST['newskuzaikonum']) ? $_POST['newskuzaikonum']: '';
629 - $newskuzaikoselect = isset($_POST['newskuzaikoselect']) ? $_POST['newskuzaikoselect'] : '';
630 - $newskudisp = isset($_POST['newskudisp']) ? trim( $_POST['newskudisp'] ) : '';
631 - $newskuunit = isset($_POST['newskuunit']) ? trim( $_POST['newskuunit'] ) : '';
632 - $newskugptekiyo = isset($_POST['newskugptekiyo']) ? $_POST['newskugptekiyo'] : '';
633 -
634 - if ( !WCUtils::is_blank($newskuname) && !WCUtils::is_blank($newskuprice) && !WCUtils::is_blank($newskuzaikoselect) ) {
635 -
636 - if ( in_array($newskuname, $protected) )
637 - return false;
638 -
639 - wp_cache_delete($post_ID, 'post_meta');
640 -
641 - $value['code'] = $newskuname;
642 - $value['name'] = $newskudisp;
643 - $value['cprice'] = $newskucprice;
644 - $value['price'] = $newskuprice;
645 - $value['unit'] = $newskuunit;
646 - $value['stocknum'] = $newskuzaikonum;
647 - $value['stock'] = $newskuzaikoselect;
648 - $value['gp'] = $newskugptekiyo;
649 - $value = apply_filters('usces_filter_add_item_sku_meta_value', $value);
650 -
651 - $id = usces_add_sku($post_ID, $value);
652 - return $id;
653 - }
654 - return false;
655 -} // add_meta
656 -
657 -
658 -
659 -/**
660 - * up_item_option_meta
661 - */
662 -function up_item_option_meta( $post_ID ) {
663 - global $wpdb, $usces;
664 -
665 - $post_ID = (int) $post_ID;
666 - $value = array();
667 -
668 - $optmetaid = isset($_POST['optmetaid']) ? (int)$_POST['optmetaid'] : '';
669 - $optname = isset($_POST['optname']) ? $_POST['optname'] : '';
670 - $optmeans = isset($_POST['optmeans']) ? (int)$_POST['optmeans']: 0;
671 - $optessential = isset($_POST['optessential']) ? $_POST['optessential']: 0;
672 - $optsort = isset($_POST['sort']) ? $_POST['sort']: 0;
673 - $optvalue = isset($_POST['optvalue']) ? trim($_POST['optvalue']) : '';
674 -
675 - $metakey = '_iopt_';
676 - $value['name'] = str_replace("\\",'',$optname);
677 - $value['means'] = $optmeans;
678 - $value['essential'] = $optessential;
679 - $value['value'] = str_replace("\\",'',$optvalue);
680 - $value['sort'] = $optsort;
681 - $value = $usces->stripslashes_deep_post($value);
682 - $valueserialized = serialize($value);
683 -
684 - wp_cache_delete($post_ID, 'post_meta');
685 -
686 - $res = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $valueserialized, $optmetaid) );
687 - return $res;
688 -} // update_meta
689 -
690 -/**
691 - * up_item_sku_meta
692 - */
693 -function up_item_sku_meta( $post_ID ) {
694 - global $wpdb, $usces;
695 -
696 - $post_ID = (int) $post_ID;
697 - $value = array();
698 -
699 - $skuname = isset($_POST['skuname']) ? trim( $_POST['skuname'] ) : '';
700 - $skumetaid = isset($_POST['skumetaid']) ? (int)$_POST['skumetaid'] : '';
701 - $skucprice = isset($_POST['skucprice']) ? trim( $_POST['skucprice'] ): 0;
702 - $skuprice = isset($_POST['skuprice']) ? trim( $_POST['skuprice'] ): 0;
703 - $skuzaikonum = isset($_POST['skuzaikonum']) ? trim( $_POST['skuzaikonum'] ): 0;
704 - $skuzaiko = isset($_POST['skuzaiko']) ? (int)$_POST['skuzaiko'] : '';
705 - $skudisp = isset($_POST['skudisp']) ? trim( $_POST['skudisp'] ): '';
706 - $skuunit = isset($_POST['skuunit']) ? trim( $_POST['skuunit'] ): '';
707 - $skugptekiyo = isset($_POST['skugptekiyo']) ? (int)$_POST['skugptekiyo'] : 0;
708 - $skusort = isset($_POST['sort']) ? $_POST['sort']: 0;
709 -
710 - $value['code'] = $skuname;
711 - $value['name'] = $skudisp;
712 - $value['cprice'] = $skucprice;
713 - $value['price'] = $skuprice;
714 - $value['unit'] = $skuunit;
715 - $value['stocknum'] = $skuzaikonum;
716 - $value['stock'] = $skuzaiko;
717 - $value['gp'] = $skugptekiyo;
718 - $value['sort'] = $skusort;
719 - $value = $usces->stripslashes_deep_post($value);
720 -
721 - $skus = $usces->get_skus($post_ID);
722 - foreach( $skus as $sku ){
723 - if( $sku['code'] == $skuname && $sku['meta_id'] != $skumetaid){
724 - return -1;
725 - break;
726 - }
727 - }
728 -
729 - $value = apply_filters('usces_filter_up_item_sku_meta_value', $value);
730 -
731 - $valueserialized = serialize($value);
732 -
733 - if ( !WCUtils::is_blank($skumetaid) && !WCUtils::is_blank($skuname) && !WCUtils::is_blank($skuprice) ) {
734 -
735 - wp_cache_delete($post_ID, 'post_meta');
736 -
737 - $res = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $valueserialized, $skumetaid) );
738 - return $res;
739 - }
740 - return false;
741 -} // update_meta
742 -
743 -
744 -
745 -/**
746 - * del_item_option_meta
747 - */
748 -function del_item_option_meta( $post_ID ) {
749 - global $wpdb;
750 -
751 - $post_ID = (int) $post_ID;
752 - $optmetaid = isset($_POST['optmetaid']) ? (int)$_POST['optmetaid'] : '';
753 -
754 - wp_cache_delete($post_ID, 'post_meta');
755 -
756 - $res = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $optmetaid) );
757 -
758 - $opts = usces_get_opts($post_ID);
759 - if( !empty($opts) ){
760 - $i = 0;
761 - foreach( $opts as $opt ){
762 - $opt['sort'] = $i;
763 - $meta_id = $opt['meta_id'];
764 - unset($opt['meta_id']);
765 - $serialized_values = serialize($opt);
766 - $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $serialized_values, $meta_id) );
767 - $i++;
768 - }
769 - }
770 - return ;
771 -} // delete_meta
772 -
773 -/**
774 - * del_item_sku_meta
775 - */
776 -function del_item_sku_meta( $post_ID ) {
777 - global $wpdb, $usces;
778 -
779 - $post_ID = (int) $post_ID;
780 - $skumetaid = isset($_POST['skumetaid']) ? (int)$_POST['skumetaid'] : '';
781 -
782 - wp_cache_delete($post_ID, 'post_meta');
783 -
784 - $res = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $skumetaid) );
785 -
786 - $skus = $usces->get_skus($post_ID);
787 - if( !empty($skus) ){
788 - $i = 0;
789 - foreach( $skus as $sku ){
790 - $sku['sort'] = $i;
791 - $meta_id = $sku['meta_id'];
792 - unset($sku['meta_id']);
793 - $serialized_values = serialize($sku);
794 - $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $serialized_values, $meta_id) );
795 - $i++;
796 - }
797 - }
798 - return ;
799 -} // delete_meta
800 -
801 -
802 -
803 -function select_common_option( $post_ID ) {
804 - global $wpdb;
805 -
806 - $meta_id = isset($_POST['meta_id']) ? $_POST['meta_id'] : '';
807 - if(!$meta_id) return ;
808 -
809 - $meta_value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_id = %d ", $meta_id) );
810 - $array_val = unserialize( $meta_value );
811 -
812 - $means = $array_val['means'];
813 - $essential = $array_val['essential'];
814 - $value = $array_val['value'];
815 - $res = $means . '#usces#' . $essential . '#usces#' . $value;
816 - return $res;
817 -} // select_common_option
818 -
819 -
820 -
821 -
822 -function order_item2cart_ajax(){
823 - global $usces;
824 -
825 - if( $_POST['action'] != 'order_item2cart_ajax' ) die(0);
826 -
827 - $_POST = $usces->stripslashes_deep_post($_POST);
828 - $res = order_item2cart();
829 -
830 - //if( $res === false ) die(0);//20120613ysk 0000500
831 -
832 - //REGEX BUG: but it'll return info
833 - // Compose JavaScript for return
834 - die( $res );
835 -}
836 -
837 -function order_item_ajax(){
838 - global $usces;
839 -
840 - if( $_POST['action'] != 'order_item_ajax' ) die(0);
841 -
842 - $res = false;
843 - $_POST = $usces->stripslashes_deep_post($_POST);
844 -
845 - switch ( $_POST['mode'] ) {
846 - case 'completionMail':
847 - case 'orderConfirmMail':
848 - case 'changeConfirmMail':
849 - case 'receiptConfirmMail':
850 - case 'mitumoriConfirmMail':
851 - case 'cancelConfirmMail':
852 - case 'otherConfirmMail':
853 - $res = usces_order_confirm_message( $_POST['order_id'] );
854 - break;
855 - case 'sendmail':
856 - $res = usces_ajax_send_mail();
857 - break;
858 - case 'get_order_item':
859 - $res = get_order_item( $_POST['itemcode'] );
860 - break;
861 - case 'ordercheckpost':
862 - $res = usces_update_ordercheck();
863 - break;
864 - case 'getmember':
865 - $res = usces_get_member_neworder();
866 - break;
867 - case 'recalculation':
868 - $res = usces_order_recalculation( $_POST['order_id'], $_POST['mem_id'], $_POST['post_ids'], $_POST['skus'], $_POST['prices'], $_POST['quants'], $_POST['use_point'], $_POST['shipping_charge'], $_POST['cod_fee'] );
869 - break;
870 - }
871 -
872 - $res = apply_filters( 'usces_filter_order_item_ajax', $res );
873 -
874 - if( $res === false ) die(0);
875 -
876 - //REGEX BUG: but it'll return info
877 - // Compose JavaScript for return
878 - die( $res );
879 -}
880 -
881 -/**
882 - * order Item html
883 - */
884 -function usces_get_member_neworder() {
885 - global $wpdb;
886 - $wpdb->show_errors();
887 - $res = '';
888 - $member_table = $wpdb->prefix . "usces_member";
889 - $query = $wpdb->prepare("SELECT * FROM $member_table WHERE mem_email = %s", trim($_POST['email']));
890 - $value = $wpdb->get_row( $query, ARRAY_A );
891 - if( !$value ){
892 - die( 'none#usces#1' );
893 - }else{
894 - $res .= 'ok#usces#member_id=' . $value['ID'];
895 - $res .= '#usces#customer[name1]=' . $value['mem_name1'];
896 - $res .= '#usces#customer[name2]=' . $value['mem_name2'];
897 - $res .= '#usces#customer[name3]=' . $value['mem_name3'];
898 - $res .= '#usces#customer[name4]=' . $value['mem_name4'];
899 - $res .= '#usces#customer[zipcode]=' . $value['mem_zip'];
900 - $res .= '#usces#customer[pref]=' . $value['mem_pref'];
901 - $res .= '#usces#customer[address1]=' . $value['mem_address1'];
902 - $res .= '#usces#customer[address2]=' . $value['mem_address2'];
903 - $res .= '#usces#customer[address3]=' . $value['mem_address3'];
904 - $res .= '#usces#customer[tel]=' . $value['mem_tel'];
905 - $res .= '#usces#customer[fax]=' . $value['mem_fax'];
906 - $res .= '#usces#delivery[name1]=' . $value['mem_name1'];
907 - $res .= '#usces#delivery[name2]=' . $value['mem_name2'];
908 - $res .= '#usces#delivery[name3]=' . $value['mem_name3'];
909 - $res .= '#usces#delivery[name4]=' . $value['mem_name4'];
910 - $res .= '#usces#delivery[zipcode]=' . $value['mem_zip'];
911 - $res .= '#usces#delivery[pref]=' . $value['mem_pref'];
912 - $res .= '#usces#delivery[address1]=' . $value['mem_address1'];
913 - $res .= '#usces#delivery[address2]=' . $value['mem_address2'];
914 - $res .= '#usces#delivery[address3]=' . $value['mem_address3'];
915 - $res .= '#usces#delivery[tel]=' . $value['mem_tel'];
916 - $res .= '#usces#delivery[fax]=' . $value['mem_fax'];
917 -
918 - $member_metetable = $wpdb->prefix . "usces_member_meta";
919 - $query = $wpdb->prepare("SELECT * FROM $member_metetable WHERE meta_key LIKE %s AND member_id = %d", 'csmb_%', $value['ID']);
920 - $customs = $wpdb->get_results( $query, ARRAY_A );
921 - if( !empty($customs) ){
922 - foreach( $customs as $cusv ){
923 - $res .= '#usces#custom_customer[' . substr($cusv['meta_key'], 5) . ']=' . $cusv['meta_value'];
924 - $res .= '#usces#custom_delivery[' . substr($cusv['meta_key'], 5) . ']=' . $cusv['meta_value'];
925 - }
926 - }
927 - //die( urlencode($res) );
928 - }
929 -
930 - die( $res );
931 -}
932 -
933 -function order_item2cart() {
934 - global $usces;
935 -
936 - if( $_POST['action'] != 'order_item2cart_ajax' ) die(0);
937 -
938 - $res = usces_update_ordercart();
939 -
940 -//20120613ysk start 0000500
941 - //if( $res === false ) die(0);
942 - //if( $res === 0 ) die('nodata');
943 -
944 - //die( $res );
945 - return $res;
946 -//20120613ysk end
947 -}
948 -
949 -function get_order_item( $item_code ) {
950 - global $usces, $post;
951 -
952 - $post_id = $usces->get_postIDbyCode( $item_code );
953 - if( $post_id == NULL ) return false;
954 - $post = get_post($post_id);
955 -
956 - $pict_id = $usces->get_mainpictid( $item_code );
957 - $pict_link = wp_get_attachment_image($pict_id, array(150, 150), true);
958 - preg_match("/^\<a .+\>(\<img .+\/\>)\<\/a\>$/", $pict_link, $match);
959 - $pict = isset($match[1]) ? $match[1] : '';
960 - $skus = $usces->get_skus( $post_id );
961 - $optkeys = $usces->get_itemOptionKey( $post_id );
962 - $itemName = esc_html($usces->getItemName($post_id));
963 -
964 - $r = '';
965 - $r .= $pict_link . "\n";
966 - $r .= "<h3>" . $itemName . "</h3>\n";
967 - $r .= "<div class='skuform'>\n";
968 -
969 - $r .= "<table class='skumulti'>\n";
970 - $r .= "<thead>\n";
971 - $r .= "<tr>\n";
972 - $r .= "<th>" . __('SKU code','usces') . "</th>\n";
973 - $r .= "<th>" . __('SKU display name ','usces') . "</th>\n";
974 - $usces_listprice = __('List price', 'usces') . usces_guid_tax('return');
975 - $r .= "<th>" . apply_filters('usces_filter_listprice_label', $usces_listprice, __('List price', 'usces'), usces_guid_tax('return')) . "</th>\n";
976 - $usces_sellingprice = __('Sale price','usces') . usces_guid_tax('return');
977 - $r .= "<th>" . apply_filters('usces_filter_sellingprice_label', $usces_sellingprice, __('Sale price', 'usces'), usces_guid_tax('return')) . "</th>\n";
978 - $r .= "<th>" . __('stock','usces') . "</th>\n";
979 - $r .= "<th>" . __('stock','usces') . "</th>\n";
980 - $r .= "<th>" . __('unit','usces') . "</th>\n";
981 - $r .= "<th>&nbsp;</th>\n";
982 - $r .= "</tr>\n";
983 - $r .= "</thead>\n";
984 - $r .= "<tbody>\n";
985 - //for ($i=0; $i<count($skus['code']); $i++) :
986 - // $sku = esc_attr($skus['code'][$i]);
987 - // $cprice = esc_attr($skus['cprice'][$i]);
988 - // $price = esc_attr($skus['price'][$i]);
989 - // $zaiko = esc_attr($usces->zaiko_status[$skus['stock'][$i]]);
990 - // $zaikonum = esc_attr($skus['stocknum'][$i]);
991 - // $disp = esc_attr($skus['name'][$i]);
992 - // $unit = esc_attr($skus['unit'][$i]);
993 - // $gptekiyo = $skus['gp'][$i];
994 - foreach($skus as $sku) :
995 - $key = esc_attr($sku['code']);
996 - $cprice = esc_attr($sku['cprice']);
997 - $price = esc_attr($sku['price']);
998 - $zaiko = esc_attr($usces->zaiko_status[$sku['stock']]);
999 - $zaikonum = esc_attr($sku['stocknum']);
1000 - $disp = esc_attr($sku['name']);
1001 - $unit = esc_attr($sku['unit']);
1002 - $gptekiyo = $sku['gp'];
1003 - $sort = (int)$sku['sort'];
1004 - $r .= "<tr>\n";
1005 - $r .= "<td rowspan='2'>" . $key . "</td>\n";
1006 - $r .= "<td>" . $disp . "</td>\n";
1007 - $r .= "<td><span class='cprice'>" . ( ( !empty($cprice) ) ? usces_crform( $cprice, true, false, 'return' ) : '' ) . "</span></td>\n";
1008 - $r .= "<td><span class='price'>" . usces_crform( $price, true, false, 'return' ) . "</span></td>\n";
1009 - $r .= "<td>" . $zaiko . "</td>\n";
1010 - $r .= "<td>" . $zaikonum . "</td>\n";
1011 -// $r .= "<td>" . usces_the_itemQuant() . "</td>\n";
1012 - $r .= "<td>" . $unit . "</td>\n";
1013 - $r .= "<td>\n";
1014 - $r .= "<input name=\"itemNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWName[{$post_id}][{$key}]\" value=\"{$itemName}\" />\n";
1015 - $r .= "<input name=\"itemNEWCode[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWCode[{$post_id}][{$key}]\" value=\"{$item_code}\" />\n";
1016 - $r .= "<input name=\"skuNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWName[{$post_id}][{$key}]\" value=\"{$key}\" />\n";
1017 - $r .= "<input name=\"skuNEWCprice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWCprice[{$post_id}][{$key}]\" value=\"{$cprice}\" />\n";
1018 - $r .= "<input name=\"skuNEWDisp[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWDisp[{$post_id}][{$key}]\" value=\"{$disp}\" />\n";
1019 - $r .= "<input name=\"zaikoNEWnum[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaikoNEWnum[{$post_id}][{$key}]\" value=\"{$zaikonum}\" />\n";
1020 - $r .= "<input name=\"zaiNEWko[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaiNEWko[{$post_id}][{$key}]\" value=\"{$zaiko}\" />\n";
1021 - $r .= "<input name=\"uniNEWt[{$post_id}][{$key}]\" type=\"hidden\" id=\"uniNEWt[{$post_id}][{$key}]\" value=\"{$unit}\" />\n";
1022 - $r .= "<input name=\"gpNEWtekiyo[{$post_id}][{$key}]\" type=\"hidden\" id=\"gpNEWtekiyo[{$post_id}][{$key}]\" value=\"{$gptekiyo}\" />\n";
1023 - $r .= "<input name=\"skuNEWPrice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWPrice[{$post_id}][{$key}]\" value=\"{$price}\" />\n";
1024 - $r .= "<input name=\"inNEWCart[{$post_id}][{$key}]\" type=\"button\" id=\"inNEWCart[{$post_id}][{$key}]\" class=\"skubutton\" value=\"" . __('Add to Whish List','usces') . "\" onclick=\"orderItem.add2cart('{$post_id}', '{$key}');\" />";
1025 - $r .= "</td>\n";
1026 - $r .= "</tr>\n";
1027 - $r .= "<tr>\n";
1028 - if($optkeys) :
1029 - $r .= "<td colspan='7'>\n";
1030 - foreach($optkeys as $optkey => $optvalue) :
1031 - $r .= "<div>\n";
1032 - $name = esc_attr($optvalue);
1033 - $optcode = esc_attr(urlencode($name));
1034 - $opts = usces_get_opts($post_id, 'name');
1035 - $opt = $opts[$optvalue];
1036 -//20110715ysk start 0000202
1037 - $means = (int)$opt['means'];
1038 - $essential = (int)$opt['essential'];
1039 - $r .= "\n<label for='itemNEWOption[{$post_id}][{$key}][{$optcode}]' class='iopt_label'>{$name}</label>\n";
1040 - switch($means) {
1041 - case 0://Single-select
1042 - case 1://Multi-select
1043 - $selects = explode("\n", $opt['value']);
1044 - $multiple = ($means === 0) ? '' : ' multiple';
1045 - $multiple_array = ($means === 0) ? '' : '_multiple';
1046 -
1047 - $r .= "\n<select name='itemNEWOption[{$post_id}][{$key}][{$optcode}]' id='itemNEWOption[{$post_id}][{$key}][{$optcode}]' class='iopt_select{$multiple_array}'{$multiple}>\n";
1048 - if($essential == 1)
1049 - $r .= "\t<option value='#NONE#' selected='selected'>" . __('Choose','usces') . "</option>\n";
1050 - $s=0;
1051 - foreach($selects as $v) {
1052 - if($s == 0 && $essential == 0)
1053 - $selected = ' selected="selected"';
1054 - else
1055 - $selected = '';
1056 - $r .= "\t<option value='{$v}'{$selected}>{$v}</option>\n";
1057 - $s++;
1058 - }
1059 - $r .= "</select>\n";
1060 - break;
1061 - case 2://Text
1062 - $r .= "\n<input name='itemNEWOption[{$post_id}][{$key}][{$optcode}]' type='text' id='itemNEWOption[{$post_id}][{$key}][{$optcode}]' class='iopt_text' onKeyDown=\"if (event.keyCode == 13) {return false;}\" value=\"\" />\n";
1063 - break;
1064 - case 5://Text-area
1065 - $r .= "\n<textarea name='itemNEWOption[{$post_id}][{$key}][{$optcode}]' id='itemNEWOption[{$post_id}][{$key}][{$optcode}]' class='iopt_textarea'></textarea>\n";
1066 - break;
1067 - }
1068 -//20110715ysk end
1069 - $r .= "<input name=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" value=\"{$optcode}\" />\n";
1070 - $r .= "<input name=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" value=\"{$essential}\" />\n";
1071 - $r .= "</div>\n";
1072 - endforeach;
1073 - $r .= "</td>\n";
1074 - endif;
1075 - $r .= "</tr>\n";
1076 - //endfor;
1077 - endforeach;
1078 - $r .= "</tbody>\n";
1079 - $r .= "</table>\n";
1080 -
1081 -
1082 - $r .= "</div>\n";
1083 -
1084 - return $r;
1085 -}
1086 -
1087 -function item_option_ajax()
1088 -{
1089 -
1090 - if( $_POST['action'] != 'item_option_ajax' ) die(0);
1091 -
1092 - if(isset($_POST['update'])){
1093 - $id = up_item_option_meta( $_POST['ID'] );
1094 -
1095 - }else if(isset($_POST['delete'])){
1096 - $id = del_item_option_meta( $_POST['ID'] );
1097 -
1098 - }else if(isset($_POST['select'])){
1099 - $res = select_common_option( $_POST['ID'] );
1100 - die( $res );
1101 -
1102 - }else if(isset($_POST['sort'])){
1103 - $id = usces_sort_post_meta( $_POST['ID'], $_POST['meta'] );
1104 - //die( $res );
1105 -
1106 - }else{
1107 - $id = add_item_option_meta( $_POST['ID'] );
1108 -
1109 - }
1110 -
1111 - $opts = usces_get_opts( $_POST['ID'] );
1112 -
1113 - $r = '';
1114 - foreach ( $opts as $opt )
1115 - $r .= _list_item_option_meta_row( $opt );
1116 -
1117 - $res = $r . '#usces#' . $id;
1118 - //REGEX BUG: but it'll return info
1119 - // Compose JavaScript for return
1120 - die( $res );
1121 -}
1122 -
1123 -function item_sku_ajax(){
1124 - global $usces;
1125 -
1126 - $id = '';
1127 - if( $_POST['action'] != 'item_sku_ajax' ) die(0);
1128 -
1129 - if(isset($_POST['update'])){
1130 - $id = up_item_sku_meta( $_POST['ID'] );
1131 -
1132 - }else if(isset($_POST['delete'])){
1133 - $id = del_item_sku_meta( $_POST['ID'] );
1134 -
1135 - }else if(isset($_POST['select'])){
1136 - $res = select_item_sku( $_POST['ID'] );
1137 - die( $res );
1138 -
1139 - }else if(isset($_POST['sort'])){
1140 - $id = usces_sort_post_meta( $_POST['ID'], $_POST['meta'] );
1141 - //die( $res );
1142 -
1143 - }else{
1144 - $id = add_item_sku_meta( $_POST['ID'] );
1145 -
1146 - }
1147 -
1148 - $skus = $usces->get_skus( $_POST['ID'] );
1149 -
1150 - $r = '';
1151 -
1152 - foreach ( (array)$skus as $sku )
1153 - $r .= _list_item_sku_meta_row( $sku );
1154 -
1155 - //$list = has_item_sku_list();
1156 -
1157 - $res = $r . '#usces#' . $id;
1158 -
1159 - die( $res );
1160 -}
1161 -
1162 -function item_save_metadata( $post_id, $post ) {
1163 - global $usces, $wpdb;
1164 -
1165 - $message = '';
1166 -
1167 -// usces_log('item_save_metadata : '.print_r($post_id,true), 'acting_transaction.log');
1168 -// usces_log('item_save_metadata : '.print_r($_POST['post_ID'],true), 'acting_transaction.log');
1169 - // パーミッションチェック
1170 - if ( isset($_POST['page']) && 'usces_itemedit' == $_POST['page']) {
1171 - if ( !current_user_can( 'edit_post', $post_id ) ){
1172 - $usces->set_action_status('error', 'ERROR : '.__('Sorry, you do not have the right to edit this post.'));
1173 - return $post_id;
1174 - }
1175 - } else {
1176 - return $post_id;
1177 - }
1178 -
1179 - if ( !wp_verify_nonce( $_POST['usces_nonce'], 'usc-e-shop' )) {
1180 - return $post_id;
1181 - }
1182 -
1183 -// $post_id = $_POST['post_ID'];
1184 -// if( $post_id < 0 ){
1185 -// return $post_id;
1186 -// }
1187 -
1188 -
1189 -
1190 - // 自動保存ルーチンかどうかチェック。そうだった場合はフォームを送信しない(何もしない)
1191 - if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
1192 - return $post_id;
1193 -
1194 - $usces->set_item_mime($post_id, 'item');
1195 -
1196 - $itemCode = trim($_POST['itemCode' ]);
1197 - if( preg_match('/[^0-9a-zA-Z\-_]/', $itemCode) ){
1198 -// $itemCode = '';
1199 -// $usces->action_message = '商品コードは半角英数(-_を含む)で入力して下さい。' . "<br />";
1200 - }
1201 - if( empty($itemCode) ){
1202 - $itemCode = '';
1203 - $message .= __('商品コードが入力されていません。', 'usces') . "<br />";
1204 - }elseif( $res = usces_is_same_itemcode($post->ID, $itemCode)) {
1205 - $message .= 'post_ID ';
1206 - foreach( $res as $postid )
1207 - $message .= $postid . ', ';
1208 - $message .= 'に同じ商品コードが登録されています。' . "<br />";
1209 - $usces->set_action_status('error', 'ERROR : '.$message);
1210 - }
1211 - update_post_meta($post_id, '_itemCode', $itemCode);
1212 -
1213 -
1214 - if(isset($_POST['itemName'])){
1215 - $itemName = trim($_POST['itemName']);
1216 - update_post_meta($post_id, '_itemName', $itemName);
1217 - }
1218 -// if(isset($_POST['itemCode'])){
1219 -// $itemCode = trim($_POST['itemCode']);
1220 -// update_post_meta($post_id, '_itemCode', $itemCode);
1221 -// }
1222 - if(isset($_POST['itemRestriction'])){
1223 - $itemRestriction = trim($_POST['itemRestriction']);
1224 - update_post_meta($post_id, '_itemRestriction', $itemRestriction);
1225 - }
1226 - if(isset($_POST['itemPointrate'])){
1227 - $itemPointrate = (int)$_POST['itemPointrate'];
1228 - update_post_meta($post_id, '_itemPointrate', $itemPointrate);
1229 - }
1230 - if(isset($_POST['itemGpNum1'])){
1231 - $itemGpNum1 = (int)$_POST['itemGpNum1'];
1232 - update_post_meta($post_id, '_itemGpNum1', $itemGpNum1);
1233 - }
1234 - if(isset($_POST['itemGpNum2'])){
1235 - $itemGpNum2 = (int)$_POST['itemGpNum2'];
1236 - update_post_meta($post_id, '_itemGpNum2', $itemGpNum2);
1237 - }
1238 - if(isset($_POST['itemGpNum3'])){
1239 - $itemGpNum3 = (int)$_POST['itemGpNum3'];
1240 - update_post_meta($post_id, '_itemGpNum3', $itemGpNum3);
1241 - }
1242 - if(isset($_POST['itemGpDis1'])){
1243 - $itemGpDis1 = (int)$_POST['itemGpDis1'];
1244 - update_post_meta($post_id, '_itemGpDis1', $itemGpDis1);
1245 - }
1246 - if(isset($_POST['itemGpDis2'])){
1247 - $itemGpDis2 = (int)$_POST['itemGpDis2'];
1248 - update_post_meta($post_id, '_itemGpDis2', $itemGpDis2);
1249 - }
1250 - if(isset($_POST['itemGpDis3'])){
1251 - $itemGpDis3 = (int)$_POST['itemGpDis3'];
1252 - update_post_meta($post_id, '_itemGpDis3', $itemGpDis3);
1253 - }
1254 -
1255 - if(isset($_POST['itemShipping'])){
1256 - $itemShipping = (int)$_POST['itemShipping'];
1257 - update_post_meta($post_id, '_itemShipping', $itemShipping);
1258 - }
1259 - if(isset($_POST['itemDeliveryMethod'])){
1260 - $itemDeliveryMethod = array();
1261 - foreach( (array)$_POST["itemDeliveryMethod"] as $dmid){
1262 - $itemDeliveryMethod[] = $dmid;
1263 - }
1264 - update_post_meta($post_id, '_itemDeliveryMethod', $itemDeliveryMethod);
1265 - }
1266 - if(isset($_POST['itemShippingCharge'])){
1267 - $itemShippingCharge = (int)$_POST['itemShippingCharge'];
1268 - update_post_meta($post_id, '_itemShippingCharge', $itemShippingCharge);
1269 - }
1270 - $itemIndividualSCharge = isset($_POST['itemIndividualSCharge']) ? 1 : 0;
1271 - update_post_meta($post_id, '_itemIndividualSCharge', $itemIndividualSCharge);
1272 -
1273 - if(isset($_POST['wcexp'])){
1274 - $wcexp = serialize($_POST['wcexp']);
1275 - update_post_meta($post_id, '_wcexp', $wcexp);
1276 - }
1277 -
1278 - //SKU
1279 - if( isset($_POST['itemsku']) ){
1280 - $meta_ids = array();
1281 - $codes = array();
1282 - $uniq_code = false;
1283 - $irreg_code = false;
1284 - $irreg_price = false;
1285 -
1286 - foreach($_POST['itemsku'] as $mid => $temp){
1287 - $meta_ids[] = $mid;
1288 - }
1289 - $meta_ids = array_unique($meta_ids);
1290 - foreach( $meta_ids as $meta_id ){
1291 -
1292 - $skucode = isset($_POST['itemsku'][$meta_id]['key']) ? trim( $_POST['itemsku'][$meta_id]['key'] ) : '';
1293 - $skucprice = isset($_POST['itemsku'][$meta_id]['cprice']) ? trim( $_POST['itemsku'][$meta_id]['cprice'] ): 0;
1294 - $skuprice = isset($_POST['itemsku'][$meta_id]['price']) ? trim( $_POST['itemsku'][$meta_id]['price'] ): 0;
1295 - $skustocknum = isset($_POST['itemsku'][$meta_id]['zaikonum']) ? trim( $_POST['itemsku'][$meta_id]['zaikonum'] ): 0;
1296 - $skustock = isset($_POST['itemsku'][$meta_id]['zaiko']) ? (int)$_POST['itemsku'][$meta_id]['zaiko'] : '';
1297 - $skuname = isset($_POST['itemsku'][$meta_id]['skudisp']) ? trim( $_POST['itemsku'][$meta_id]['skudisp'] ): '';
1298 - $skuunit = isset($_POST['itemsku'][$meta_id]['skuunit']) ? trim( $_POST['itemsku'][$meta_id]['skuunit'] ): '';
1299 - $skugp = isset($_POST['itemsku'][$meta_id]['skugptekiyo']) ? (int)$_POST['itemsku'][$meta_id]['skugptekiyo'] : 0;
1300 - $skusort = isset($_POST['itemsku'][$meta_id]['sort']) ? $_POST['itemsku'][$meta_id]['sort']: 0;
1301 -
1302 - $skus['code'] = $skucode;
1303 - $skus['name'] = $skuname;
1304 - $skus['cprice'] = $skucprice;
1305 - $skus['price'] = $skuprice;
1306 - $skus['unit'] = $skuunit;
1307 - $skus['stocknum'] = $skustocknum;
1308 - $skus['stock'] = $skustock;
1309 - $skus['gp'] = $skugp;
1310 - $skus['sort'] = $skusort;
1311 - $skus = $usces->stripslashes_deep_post($skus);
1312 - $skus = apply_filters( 'usces_filter_item_save_sku_metadata', $skus, $meta_id );
1313 -
1314 - $valueserialized = serialize($skus);
1315 - $res = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $valueserialized, $meta_id) );
1316 -
1317 - if( in_array( $skucode, $codes ) )
1318 - $uniq_code = true;
1319 -
1320 - if( WCUtils::is_blank($skucode) )
1321 - $irreg_code = true;
1322 -
1323 - if( WCUtils::is_blank($skuprice) || preg_match('/[^0-9.]/', $skuprice) || 1 < substr_count($skuprice, '.' ) )
1324 - $irreg_price = true;
1325 -
1326 - $codes[] = $skucode;
1327 - }
1328 -
1329 - if( $uniq_code ){
1330 - $message .= 'SKUコードが重複しています。' . "<br />";
1331 - }
1332 - if( $irreg_code ){
1333 - $message .= 'SKUコードの値が不正です。' . "<br />";
1334 - }
1335 - if( $irreg_price ){
1336 - $message .= '売価の値が不正なSKUが存在します。' . "<br />";
1337 - }
1338 - }
1339 - //OPT
1340 - if( isset($_POST['itemopt']) ){
1341 - $meta_ids = array();
1342 - $names = array();
1343 - $uniq_name = false;
1344 - $irreg_name = false;
1345 - $irreg_value = false;
1346 -
1347 - foreach($_POST['itemopt'] as $mid => $temp){
1348 - $meta_ids[] = $mid;
1349 - }
1350 - $meta_ids = array_unique($meta_ids);
1351 - foreach( $meta_ids as $meta_id ){
1352 -
1353 - $optname = isset($_POST['itemopt'][$meta_id]['name']) ? $_POST['itemopt'][$meta_id]['name'] : '';
1354 - $optmeans = isset($_POST['itemopt'][$meta_id]['means']) ? (int)$_POST['itemopt'][$meta_id]['means']: 0;
1355 - $optessential = isset($_POST['itemopt'][$meta_id]['essential']) ? $_POST['itemopt'][$meta_id]['essential']: 0;
1356 - $optsort = isset($_POST['itemopt'][$meta_id]['sort']) ? $_POST['itemopt'][$meta_id]['sort']: 0;
1357 - $optvalue = isset($_POST['itemopt'][$meta_id]['value']) ? trim($_POST['itemopt'][$meta_id]['value']) : '';
1358 -
1359 - $opts['name'] = str_replace("\\",'',$optname);
1360 - $opts['value'] = str_replace("\\",'',$optvalue);
1361 - $opts['means'] = $optmeans;
1362 - $opts['essential'] = $optessential;
1363 - $opts['sort'] = $optsort;
1364 - $opts = $usces->stripslashes_deep_post($opts);
1365 -
1366 - $valueserialized = serialize($opts);
1367 - $res = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $valueserialized, $meta_id) );
1368 -
1369 - if( in_array( $optname, $names ) )
1370 - $uniq_name = true;
1371 -
1372 - if( WCUtils::is_blank($optname) )
1373 - $irreg_name = true;
1374 -
1375 - if( WCUtils::is_blank($optvalue) && 1 >= $optmeans )
1376 - $irreg_value = true;
1377 -
1378 - $names[] = $optname;
1379 - }
1380 -
1381 -
1382 - if( $uniq_name ){
1383 - $message .= 'オプション名が重複している商品オプションが存在します。' . "<br />";
1384 - }
1385 - if( $irreg_name ){
1386 - $message .= 'オプション名が未入力の商品オプションが存在します。' . "<br />";
1387 - }
1388 - if( $irreg_value ){
1389 - $message .= '商品オプションで「シングルセレクト」、「マルチセレクト」を選択した場合は、セレクト値を入力してください。' . "<br />";
1390 - }
1391 - }
1392 -
1393 - do_action('usces_action_save_product', $post_id, $post);
1394 - $message = apply_filters( 'usces_filter_save_product_message', $message, $post_id );
1395 -
1396 -// $usces->action_status = 'none';
1397 -// $usces->action_message = '';
1398 - if( $message ){
1399 - $usces->set_action_status('error', 'ERROR : '.$message);
1400 - }else{
1401 - $usces->set_action_status('success', '商品の登録が完了しました。 ');
1402 - }
1403 -
1404 - wp_cache_delete($post_id, 'post_meta');
1405 -}
1406 -
1407 -function usces_link_replace($para) {
1408 - //$str = 'admin.php?page=' . USCES_PLUGIN_BASENAME . '&';
1409 - $str = 'admin.php?page=usces_itemedit&';
1410 - $url = preg_replace('|post\.php\?|i', $str, $para);
1411 - return $url;
1412 -
1413 -}
1414 -
1415 -function usces_count_posts( $type = 'post', $perm = '' ) {
1416 - global $wpdb;
1417 -
1418 - $user = wp_get_current_user();
1419 -
1420 - $cache_key = $type;
1421 -
1422 - $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_mime_type = 'item'";
1423 - //$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
1424 - if ( 'readable' == $perm && is_user_logged_in() ) {
1425 - if ( !current_user_can("read_private_{$type}s") ) {
1426 - $cache_key .= '_' . $perm . '_' . $user->ID;
1427 - $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
1428 - }
1429 - }
1430 - $query .= ' GROUP BY post_status';
1431 -
1432 - $count = wp_cache_get($cache_key, 'counts');
1433 - if ( false !== $count )
1434 - // return $count;
1435 -
1436 - $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
1437 -
1438 - $stats = array( );
1439 - foreach( (array) $count as $row_num => $row ) {
1440 - $stats[$row['post_status']] = $row['num_posts'];
1441 - }
1442 -
1443 - $stats = (object) $stats;
1444 - wp_cache_set($cache_key, $stats, 'counts');
1445 -
1446 - return $stats;
1447 -}
1448 -
1449 -//20100809ysk start
1450 -/**
1451 - * custom order meta row
1452 - */
1453 -function _list_custom_order_meta_row($key, $entry) {
1454 - $r = '';
1455 - $style = '';
1456 - $key = esc_attr($key);
1457 -
1458 - $name = esc_attr($entry['name']);
1459 - $means = get_option('usces_custom_order_select');
1460 - $meansoption = '';
1461 - foreach($means as $meankey => $meanvalue) {
1462 - if($meankey == $entry['means']) {
1463 - $selected = ' selected="selected"';
1464 - } else {
1465 - $selected = '';
1466 - }
1467 - $meansoption .= '<option value="'.esc_attr($meankey).'"'.$selected.'>'.esc_html($meanvalue)."</option>\n";
1468 - }
1469 - $essential = $entry['essential'] == 1 ? " checked='checked'" : "";
1470 - $value = '';
1471 - if(is_array($entry['value'])) {
1472 - foreach($entry['value'] as $k => $v) {
1473 - $value .= $v."\n";
1474 - }
1475 - }
1476 - $value = esc_attr(trim($value));
1477 -
1478 - $r .= "\n\t<tr id='csod-{$key}' class='{$style}'>";
1479 - $r .= "\n\t\t<td class='left'><div><input type='text' name='csod[{$key}][key]' id='csod[{$key}][key]' class='optname' size='20' value='{$key}' readonly /></div>";
1480 - $r .= "\n\t\t<div><input type='text' name='csod[{$key}][name]' id='csod[{$key}][name]' class='optname' size='20' value='{$name}' /></div>";
1481 - $r .= "\n\t\t<div class='optcheck'><select name='csod[{$key}][means]' id='csod[{$key}][means]'>".$meansoption."</select>\n";
1482 - $r .= "<input type='checkbox' name='csod[{$key}][essential]' id='csod[{$key}][essential]' value='1'{$essential} /><label for='csod[{$key}][essential]'>".__('Required','usces')."</label></div>";
1483 -//20100818ysk start
1484 - //$r .= "\n\t\t<div class='submit'><input type='button' name='deletecsod[{$key}]' id='deletecsod[{$key}]' value='".esc_attr(__( 'Delete' ))."' onclick='customOrder.del(\"{$key}\");' />";
1485 - //$r .= "\n\t\t<input type='button' name='updatecsod' id='updatecsod[{$key}]' value='".esc_attr(__( 'Update' ))."' onclick='customOrder.upd(\"{$key}\");' /></div>";
1486 - $r .= "\n\t\t<div class='submit'><input type='button' name='del_csod[{$key}]' id='del_csod[{$key}]' value='".esc_attr(__( 'Delete' ))."' onclick='customField.delOrder(\"{$key}\");' />";
1487 - $r .= "\n\t\t<input type='button' name='upd_csod[{$key}]' id='upd_csod[{$key}]' value='".esc_attr(__( 'Update' ))."' onclick='customField.updOrder(\"{$key}\");' /></div>";
1488 -//20100818ysk end
1489 - $r .= "\n\t\t<div id='csod_loading-{$key}' class='meta_submit_loading'></div>";
1490 - $r .= "</td>";
1491 - $r .= "\n\t\t<td class='item-opt-value'><textarea name='csod[{$key}][value]' id='csod[{$key}][value]' class='optvalue'>{$value}</textarea></td>\n\t</tr>";
1492 - return $r;
1493 -}
1494 -//20100809ysk end
1495 -
1496 -//20100818ysk start
1497 -/**
1498 - * has custom field meta
1499 - */
1500 -function usces_has_custom_field_meta($fieldname) {
1501 - switch($fieldname) {
1502 - case 'order':
1503 - $field = 'usces_custom_order_field';
1504 - break;
1505 - case 'customer':
1506 - $field = 'usces_custom_customer_field';
1507 - break;
1508 - case 'delivery':
1509 - $field = 'usces_custom_delivery_field';
1510 - break;
1511 - case 'member':
1512 - $field = 'usces_custom_member_field';
1513 - break;
1514 - default:
1515 - return array();
1516 - }
1517 - $fields = get_option($field);
1518 - if( empty($fields) ){
1519 - $meta = array();
1520 - }elseif( is_array($fields) ){
1521 - $meta = $fields;
1522 - }else{
1523 - $meta = unserialize($fields);
1524 - }
1525 - return $meta;
1526 -}
1527 -
1528 -function usces_getinfo_ajax(){
1529 - global $wp_version;
1530 - $wcex_str = '';
1531 - $res = '';
1532 - $wcex = usces_get_wcex();
1533 - foreach ( (array)$wcex as $key => $values ) {
1534 - $wcex_str .= $key . "-" . $values['version'] . ",";
1535 - }
1536 - $wcex_str = rtrim($wcex_str, ',');
1537 - if ( version_compare($wp_version, '3.4', '>=') ){
1538 - $theme_ob = wp_get_theme();
1539 - $themedata['Name'] = $theme_ob->get('Name');
1540 - $themedata['Version'] = $theme_ob->get('Version');
1541 - }else{
1542 - $themedata = get_theme_data( get_stylesheet_directory().'/style.css' );
1543 - }
1544 -
1545 -
1546 - $v = urlencode(USCES_VERSION);
1547 - $wcid = urlencode(get_option('usces_wcid'));
1548 - $locale = urlencode(get_locale());
1549 - $theme = urlencode($themedata['Name'] . '-' . $themedata['Version']);
1550 - $wcex = urlencode($wcex_str);
1551 - $interface_url = 'http://www.welcart.com/util/welcart_information2.php';
1552 - $wcurl = urlencode(get_home_url());
1553 - $interface = parse_url($interface_url);
1554 -
1555 - $vars ="v=$v&wcid=$wcid&locale=$locale&theme=$theme&wcex=$wcex&wcurl=$wcurl";
1556 - $header = "POST " . $interface_url . " HTTP/1.1\r\n";
1557 - $header .= "Host: " . $_SERVER['HTTP_HOST'] . "\r\n";
1558 - $header .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
1559 - $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
1560 - $header .= "Content-Length: " . strlen($vars) . "\r\n";
1561 - $header .= "Connection: close\r\n\r\n";
1562 - $header .= $vars;
1563 - $fp = fsockopen($interface['host'],80,$errno,$errstr,30);
1564 - if( $fp ){
1565 - fwrite($fp, $header);
1566 - $i=0;
1567 - while ( !feof($fp) ) {
1568 - $scr = fgets($fp, 10240);
1569 - preg_match("/<(title|data)>(.*)<(\/title|\/data)>$/", $scr, $match);
1570 -
1571 - if(!empty($match[2])){
1572 - switch( $match[1] ){
1573 - case'title':
1574 - $res .= '<div style="text-align: center;border-bottom: 1px dotted #CCCCCC;width: 80%;margin-bottom: 10px;padding-bottom: 3px; margin-right: auto; margin-left: auto;"><stlong>' . $match[2] . '</strong></div><ul>';
1575 - break;
1576 - case 'data':
1577 - $res .= '<li>' . $match[2] . '</li>';
1578 - break;
1579 - }
1580 - }
1581 - $i++;
1582 - if($i>50) {
1583 - $res = 'ERROR';
1584 - break;
1585 - }
1586 - }
1587 -
1588 - $res .= '</ul>';
1589 - fclose($fp);
1590 -
1591 - }else{
1592 - $res = 'ERROR';
1593 - }
1594 - die($res);
1595 -}
1596 -
1597 -/**
1598 - * custom field ajax
1599 - */
1600 -function custom_field_ajax() {
1601 - global $usces;
1602 - $_POST = $usces->stripslashes_deep_post($_POST);
1603 -
1604 - if($_POST['action'] != 'custom_field_ajax') die(0);
1605 - switch($_POST['field']) {
1606 - case 'order':
1607 - $field = 'usces_custom_order_field';
1608 - break;
1609 - case 'customer':
1610 - $field = 'usces_custom_customer_field';
1611 - break;
1612 - case 'delivery':
1613 - $field = 'usces_custom_delivery_field';
1614 - break;
1615 - case 'member':
1616 - $field = 'usces_custom_member_field';
1617 - break;
1618 - default:
1619 - die(0);
1620 - }
1621 -
1622 - $meta = usces_has_custom_field_meta($_POST['field']);
1623 - $dupkey = 0;
1624 -
1625 - if(isset($_POST['add'])) {
1626 - $newkey = isset($_POST['newkey']) ? trim($_POST['newkey']) : '';
1627 - $newname = isset($_POST['newname']) ? trim($_POST['newname']) : '';
1628 - $newmeans = isset($_POST['newmeans']) ? $_POST['newmeans'] : 0;
1629 - $newessential = isset($_POST['newessential']) ? $_POST['newessential'] : 0;
1630 - $newposition = isset($_POST['newposition']) ? trim($_POST['newposition']) : '';
1631 -
1632 - if($newmeans == 2) {//Text
1633 - $newvalue = isset($_POST['newvalue']) ? trim($_POST['newvalue']) : '';
1634 - $nv = $newvalue;
1635 -
1636 - } else {
1637 - $newvalue = isset($_POST['newvalue']) ? explode('\n', trim($_POST['newvalue'])) : '';
1638 - foreach((array)$newvalue as $v) {
1639 - if( !WCUtils::is_blank($v) )
1640 - $nv[] = trim($v);
1641 - }
1642 - }
1643 -
1644 - if(!array_key_exists($newkey, $meta)) {
1645 - if(($newmeans >= 2 || WCUtils::is_zero($newvalue) || !empty($newvalue)) && !empty($newkey) && !empty($newname)) {
1646 - $meta[$newkey]['name'] = $newname;
1647 - $meta[$newkey]['means'] = $newmeans;
1648 - $meta[$newkey]['essential'] = $newessential;
1649 - $meta[$newkey]['value'] = $nv;
1650 - if( !WCUtils::is_blank($newposition) ) $meta[$newkey]['position'] = $newposition;
1651 - update_option($field, $meta);
1652 - }
1653 - } else {
1654 - $dupkey = 1;
1655 - }
1656 -
1657 - } elseif(isset($_POST['update'])) {
1658 - $key = isset($_POST['key']) ? trim($_POST['key']) : '';
1659 - $name = isset($_POST['name']) ? trim($_POST['name']) : '';
1660 - $means = isset($_POST['means']) ? $_POST['means'] : 0;
1661 - $essential = isset($_POST['essential']) ? $_POST['essential'] : 0;
1662 - $position = isset($_POST['position']) ? trim($_POST['position']) : '';
1663 -
1664 - if($means == 2) {//Text
1665 - $value = isset($_POST['value']) ? trim($_POST['value']) : '';
1666 - $nv = $value;
1667 -
1668 - } else {
1669 - $value = isset($_POST['value']) ? explode('\n', trim($_POST['value'])) : '';
1670 - foreach((array)$value as $v) {
1671 - if( !WCUtils::is_blank($v) )
1672 - $nv[] = trim($v);
1673 - }
1674 - }
1675 -
1676 - if($means >= 2 || WCUtils::is_zero($value) || !empty($value)) {
1677 - $meta[$key]['name'] = $name;
1678 - $meta[$key]['means'] = $means;
1679 - $meta[$key]['essential'] = $essential;
1680 - $meta[$key]['value'] = $nv;
1681 - if( !WCUtils::is_blank($position) ) $meta[$key]['position'] = $position;
1682 - update_option($field, $meta);
1683 - }
1684 -
1685 - } elseif(isset($_POST['delete'])) {
1686 - $key = isset($_POST['key']) ? trim($_POST['key']) : '';
1687 - unset($meta[$key]);
1688 - update_option($field, $meta);
1689 - }
1690 -
1691 - $r = '';
1692 - switch($_POST['field']) {
1693 - case 'order':
1694 - foreach($meta as $key => $entry)
1695 - $r .= _list_custom_order_meta_row($key, $entry);
1696 - break;
1697 - case 'customer':
1698 - foreach($meta as $key => $entry)
1699 - $r .= _list_custom_customer_meta_row($key, $entry);
1700 - break;
1701 - case 'delivery':
1702 - foreach($meta as $key => $entry)
1703 - $r .= _list_custom_delivery_meta_row($key, $entry);
1704 - break;
1705 - case 'member':
1706 - foreach($meta as $key => $entry)
1707 - $r .= _list_custom_member_meta_row($key, $entry);
1708 - break;
1709 - }
1710 -
1711 - $res = $r . '#usces#' . $dupkey;
1712 - //REGEX BUG: but it'll return info
1713 - // Compose JavaScript for return
1714 - die($res);
1715 -}
1716 -
1717 -/**
1718 - * list custom customer meta row
1719 - */
1720 -function _list_custom_customer_meta_row($key, $entry) {
1721 - $r = '';
1722 - $style = '';
1723 - $key = esc_attr($key);
1724 -
1725 - $name = esc_attr($entry['name']);
1726 - $means = get_option('usces_custom_customer_select');
1727 - $meansoption = '';
1728 - foreach($means as $meankey => $meanvalue) {
1729 - $selected = ($meankey == $entry['means']) ? " selected='selected'" : "";
1730 - $meansoption .= "<option value='".esc_attr($meankey)."'".$selected.">".esc_html($meanvalue)."</option>\n";
1731 - }
1732 - $essential = $entry['essential'] == 1 ? " checked='checked'" : "";
1733 - $value = '';
1734 - if(is_array($entry['value'])) {
1735 - foreach($entry['value'] as $k => $v) {
1736 - $value .= $v."\n";
1737 - }
1738 - }
1739 - $value = esc_attr(trim($value));
1740 - $positions = get_option('usces_custom_field_position_select');
1741 - $positionsoption = '';
1742 - foreach($positions as $poskey => $posvalue) {
1743 - $selected = ($poskey == $entry['position']) ? " selected='selected'" : "";
1744 - $positionsoption .= "<option value='".esc_attr($poskey)."'".$selected.">".esc_html($posvalue)."</option>\n";
1745 - }
1746 -
1747 - $r .= "\n\t<tr id='cscs-{$key}' class='{$style}'>";
1748 - $r .= "\n\t\t<td class='left'><div><input type='text' name='cscs[{$key}][key]' id='cscs[{$key}][key]' class='optname' size='20' value='{$key}' readonly /></div>";
1749 - $r .= "\n\t\t<div><input type='text' name='cscs[{$key}][name]' id='cscs[{$key}][name]' class='optname' size='20' value='{$name}' /></div>";
1750 - $r .= "\n\t\t<div class='optcheck'><select name='cscs[{$key}][means]' id='cscs[{$key}][means]'>".$meansoption."</select>\n";
1751 - $r .= "<input type='checkbox' name='cscs[{$key}][essential]' id='cscs[{$key}][essential]' value='1'{$essential} /><label for='cscs[{$key}][essential]'>".__('Required','usces')."</label>\n";
1752 - $r .= "<select name='cscs[{$key}][position]' id='cscs[{$key}][position]'>".$positionsoption."</select></div>";
1753 - $r .= "\n\t\t<div class='submit'><input type='button' name='del_cscs[{$key}]' id='del_cscs[{$key}]' value='".esc_attr(__( 'Delete' ))."' onclick='customField.delCustomer(\"{$key}\");' />";
1754 - $r .= "\n\t\t<input type='button' name='upd_cscs[{$key}]' id='upd_cscs[{$key}]' value='".esc_attr(__( 'Update' ))."' onclick='customField.updCustomer(\"{$key}\");' /></div>";
1755 - $r .= "\n\t\t<div id='cscs_loading-{$key}' class='meta_submit_loading'></div>";
1756 - $r .= "</td>";
1757 - $r .= "\n\t\t<td class='item-opt-value'><textarea name='cscs[{$key}][value]' id='cscs[{$key}][value]' class='optvalue'>{$value}</textarea></td>\n\t</tr>";
1758 - return $r;
1759 -}
1760 -
1761 -/**
1762 - * list custom delivery meta row
1763 - */
1764 -function _list_custom_delivery_meta_row($key, $entry) {
1765 - $r = '';
1766 - $style = '';
1767 - $key = esc_attr($key);
1768 -
1769 - $name = esc_attr($entry['name']);
1770 - $means = get_option('usces_custom_delivery_select');
1771 - $meansoption = '';
1772 - foreach($means as $meankey => $meanvalue) {
1773 - $selected = ($meankey == $entry['means']) ? " selected='selected'" : "";
1774 - $meansoption .= "<option value='".esc_attr($meankey)."'".$selected.">".esc_html($meanvalue)."</option>\n";
1775 - }
1776 - $essential = $entry['essential'] == 1 ? " checked='checked'" : "";
1777 - $value = '';
1778 - if(is_array($entry['value'])) {
1779 - foreach($entry['value'] as $k => $v) {
1780 - $value .= $v."\n";
1781 - }
1782 - }
1783 - $value = esc_attr(trim($value));
1784 - $positions = get_option('usces_custom_field_position_select');
1785 - $positionsoption = '';
1786 - foreach($positions as $poskey => $posvalue) {
1787 - $selected = ($poskey == $entry['position']) ? " selected='selected'" : "";
1788 - $positionsoption .= "<option value='".esc_attr($poskey)."'".$selected.">".esc_html($posvalue)."</option>\n";
1789 - }
1790 -
1791 - $r .= "\n\t<tr id='csde-{$key}' class='{$style}'>";
1792 - $r .= "\n\t\t<td class='left'><div><input type='text' name='csde[{$key}][key]' id='csde[{$key}][key]' class='optname' size='20' value='{$key}' readonly /></div>";
1793 - $r .= "\n\t\t<div><input type='text' name='csde[{$key}][name]' id='csde[{$key}][name]' class='optname' size='20' value='{$name}' /></div>";
1794 - $r .= "\n\t\t<div class='optcheck'><select name='csde[{$key}][means]' id='csde[{$key}][means]'>".$meansoption."</select>\n";
1795 - $r .= "<input type='checkbox' name='csde[{$key}][essential]' id='csde[{$key}][essential]' value='1'{$essential} /><label for='csde[{$key}][essential]'>".__('Required','usces')."</label>\n";
1796 - $r .= "<select name='csde[{$key}][position]' id='csde[{$key}][position]'>".$positionsoption."</select></div>";
1797 - $r .= "\n\t\t<div class='submit'><input type='button' name='del_csde[{$key}]' id='del_csde[{$key}]' value='".esc_attr(__( 'Delete' ))."' onclick='customField.delDelivery(\"{$key}\");' />";
1798 - $r .= "\n\t\t<input type='button' name='upd_csde[{$key}]' id='upd_csde[{$key}]' value='".esc_attr(__( 'Update' ))."' onclick='customField.updDelivery(\"{$key}\");' /></div>";
1799 - $r .= "\n\t\t<div id='csde_loading-{$key}' class='meta_submit_loading'></div>";
1800 - $r .= "</td>";
1801 - $r .= "\n\t\t<td class='item-opt-value'><textarea name='csde[{$key}][value]' id='csde[{$key}][value]' class='optvalue'>{$value}</textarea></td>\n\t</tr>";
1802 - return $r;
1803 -}
1804 -
1805 -/**
1806 - * list custom member meta row
1807 - */
1808 -function _list_custom_member_meta_row($key, $entry) {
1809 - $r = '';
1810 - $style = '';
1811 - $key = esc_attr($key);
1812 -
1813 - $name = esc_attr($entry['name']);
1814 - $means = get_option('usces_custom_member_select');
1815 - $meansoption = '';
1816 - foreach($means as $meankey => $meanvalue) {
1817 - $selected = ($meankey == $entry['means']) ? " selected='selected'" : "";
1818 - $meansoption .= "<option value='".esc_attr($meankey)."'".$selected.">".esc_html($meanvalue)."</option>\n";
1819 - }
1820 - $essential = $entry['essential'] == 1 ? " checked='checked'" : "";
1821 - $value = '';
1822 - if(is_array($entry['value'])) {
1823 - foreach($entry['value'] as $k => $v) {
1824 - $value .= $v."\n";
1825 - }
1826 - }
1827 - $value = esc_attr(trim($value));
1828 - $positions = get_option('usces_custom_field_position_select');
1829 - $positionsoption = '';
1830 - foreach($positions as $poskey => $posvalue) {
1831 - $selected = ($poskey == $entry['position']) ? " selected='selected'" : "";
1832 - $positionsoption .= "<option value='".esc_attr($poskey)."'".$selected.">".esc_attr($posvalue)."</option>\n";
1833 - }
1834 - $r .= "\n\t<tr id='csmb-{$key}' class='{$style}'>";
1835 - $r .= "\n\t\t<td class='left'><div><input type='text' name='csmb[{$key}][key]' id='csmb[{$key}][key]' class='optname' size='20' value='{$key}' readonly /></div>";
1836 - $r .= "\n\t\t<div><input type='text' name='csmb[{$key}][name]' id='csmb[{$key}][name]' class='optname' size='20' value='{$name}' /></div>";
1837 - $r .= "\n\t\t<div class='optcheck'><select name='csmb[{$key}][means]' id='csmb[{$key}][means]'>".$meansoption."</select>\n";
1838 - $r .= "<input type='checkbox' name='csmb[{$key}][essential]' id='csmb[{$key}][essential]' value='1'{$essential} /><label for='csmb[{$key}][essential]'>".__('Required','usces')."</label>\n";
1839 - $r .= "<select name='csmb[{$key}][position]' id='csmb[{$key}][position]'>".$positionsoption."</select></div>";
1840 - $r .= "\n\t\t<div class='submit'><input type='button' name='del_csmb[{$key}]' id='del_csmb[{$key}]' value='".esc_attr(__( 'Delete' ))."' onclick='customField.delMember(\"{$key}\");' />";
1841 - $r .= "\n\t\t<input type='button' name='upd_csmb[{$key}]' id='upd_csmb[{$key}]' value='".esc_attr(__( 'Update' ))."' onclick='customField.updMember(\"{$key}\");' /></div>";
1842 - $r .= "\n\t\t<div id='csmb_loading-{$key}' class='meta_submit_loading'></div>";
1843 - $r .= "</td>";
1844 - $r .= "\n\t\t<td class='item-opt-value'><textarea name='csmb[{$key}][value]' id='csmb[{$key}][value]' class='optvalue'>{$value}</textarea></td>\n\t</tr>";
1845 - return $r;
1846 -}
1847 -//20100818ysk end
1848 -
1849 -function change_states_ajax(){
1850 - global $usces, $usces_states;
1851 - $_POST = $usces->stripslashes_deep_post($_POST);
1852 -
1853 - $c = $_POST['country'];
1854 - $res = '';
1855 -//20110331ysk start
1856 -/* if( !isset($usces_states[$c]) || empty($usces_states[$c]) )
1857 - die('error');
1858 -
1859 - foreach( (array)$usces_states[$c] as $state ){
1860 - $res .= '<option value="' . $state . '">' . $state . '</option>';
1861 - }*/
1862 - $prefs = get_usces_states($c);
1863 - if(is_array($prefs) and 0 < count($prefs)) {
1864 - foreach((array)$prefs as $state) {
1865 - $res .= '<option value="' . $state . '">' . $state . '</option>';
1866 - }
1867 - } else {
1868 - die('error');
1869 - }
1870 -//20110331ysk end
1871 - die($res);
1872 -}
1873 -
1874 -//20110331ysk start
1875 -function get_usces_states($country) {
1876 - global $usces, $usces_states;
1877 -
1878 - $states = array();
1879 - $prefs = maybe_unserialize($usces->options['province']);
1880 - if( !isset($prefs[$country]) || empty($prefs[$country]) ) {
1881 - if($country == $usces->options['system']['base_country']) {
1882 - foreach((array)$prefs as $state) {
1883 - if(!is_array($state))
1884 - array_push($states, $state);
1885 - }
1886 - if(count($states) == 0) {
1887 - if( !empty($usces_states[$country]) ) {
1888 - $prefs = $usces_states[$country];
1889 - if(is_array($prefs)) {
1890 - $states = $prefs;
1891 - }
1892 - }
1893 - }
1894 - } else {
1895 - if( !empty($usces_states[$country]) ) {
1896 - $prefs = $usces_states[$country];
1897 - if(is_array($prefs)) {
1898 - $states = $prefs;
1899 - }
1900 - }
1901 - }
1902 - } else {
1903 - $states = $prefs[$country];
1904 - }
1905 - return $states;
1906 -}
1907 -
1908 -function target_market_ajax() {
1909 - global $usces;
1910 - $_POST = $usces->stripslashes_deep_post($_POST);
1911 - $res = "";
1912 - $target = explode(",", $_POST['target']);
1913 - foreach((array)$target as $country) {
1914 - $prefs = get_usces_states($country);
1915 - if(is_array($prefs) and 0 < count($prefs)) {
1916 - $pos = strpos($prefs[0], '--');
1917 - if($pos !== false) array_shift($prefs);
1918 - $res .= $country.",";
1919 - foreach((array)$prefs as $state) {
1920 - $res .= $state."\n";
1921 - }
1922 - $res = rtrim($res, "\n")."#usces#";
1923 -//20110430ysk start
1924 - } else {
1925 - $res .= $country.",#usces#";
1926 -//20110430ysk end
1927 - }
1928 - }
1929 - $res = rtrim($res, "#usces#");
1930 - die($res);
1931 -}
1932 -//20110331ysk end
1933 -//20120309ysk start 0000430
1934 -function usces_admin_ajax() {
1935 - switch($_POST['mode']) {
1936 - case 'options_backup':
1937 - $options = get_option('usces');
1938 - $res = true;
1939 - if( is_array($options) ) {
1940 - $usces_backup_date = current_time('mysql');
1941 - update_option('usces_backup', $options);
1942 - update_option('usces_backup_date', $usces_backup_date);
1943 - $res = $usces_backup_date;
1944 - } else {
1945 - $res = false;
1946 - }
1947 - die($res);
1948 - break;
1949 - case 'options_restore':
1950 - $options = get_option('usces_backup');
1951 - $res = true;
1952 - if( is_array($options) ) {
1953 - update_option('usces', $options);
1954 - } else {
1955 - $res = false;
1956 - }
1957 - die($res);
1958 - break;
1959 - }
1960 - do_action('usces_action_admin_ajax');
1961 -}
1962 -//20120309ysk end
1963 -function usces_order_recalculation( $order_id, $mem_id, $post_ids, $skus, $prices, $quants, $use_point, $shipping_charge, $cod_fee ) {
1964 - global $usces;
1965 -
1966 - $res = 'ok';
1967 - if( !empty($order_id) ) {
1968 - $data = $usces->get_order_data( $order_id, 'direct' );
1969 - $condition = unserialize( $data['order_condition'] );
1970 - } else {
1971 - $condition = $usces->get_condition();
1972 - }
1973 -//usces_log('condition : '.print_r($condition,true), 'acting_transaction.log');
1974 -
1975 - $post_id = explode("#usces#", $post_ids);
1976 - $sku = explode("#usces#", $skus);
1977 - $price = explode("#usces#", $prices);
1978 - $quant = explode("#usces#", $quants);
1979 - $cart = array();
1980 - for( $i = 0; $i < count($post_id); $i++ ) {
1981 - if( $post_id[$i] )
1982 - $cart[] = array( "post_id"=>$post_id[$i], "sku"=>$sku[$i], "price"=>$price[$i], "quantity"=>$quant[$i] );
1983 - }
1984 -
1985 - $total_items_price = 0;
1986 - foreach( $cart as $cart_row ) {
1987 - $total_items_price += $cart_row['price'] * $cart_row['quantity'];
1988 - }
1989 - $meminfo = $usces->get_member_info( $mem_id );
1990 -
1991 - $discount = 0;
1992 - if( $condition['display_mode'] == 'Promotionsale' ) {
1993 - if( $condition['campaign_privilege'] == 'discount' ) {
1994 - if ( 0 === (int)$condition['campaign_category'] ) {
1995 - $discount = $total_items_price * $condition['privilege_discount'] / 100;
1996 - } else {
1997 - foreach( $cart as $cart_row ) {
1998 - if( in_category( (int)$condition['campaign_category'], $cart_row['post_id']) ) {
1999 - $discount += $cart_row['price'] * $cart_row['quantity'] * $condition['privilege_discount'] / 100;
2000 - }
2001 - }
2002 - }
2003 - }
2004 - }
2005 - if( 0 < $discount ) $discount = ceil($discount * -1);
2006 -
2007 - $point = 0;
2008 - if( 'activate' == $usces->options['membersystem_state'] && 'activate' == $usces->options['membersystem_point'] && !empty($meminfo['ID']) ) {
2009 - if( $condition['display_mode'] == 'Promotionsale' ) {
2010 - if( $condition['campaign_privilege'] == 'discount' ) {
2011 - foreach( $cart as $cart_row ) {
2012 - $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2013 - if( !in_array( $condition['campaign_category'], $cats ) ) {
2014 - $rate = get_post_meta( $cart_row['post_id'], '_itemPointrate', true );
2015 - $price = $cart_row['price'] * $cart_row['quantity'];
2016 - $point += $price * $rate / 100;
2017 - }
2018 - }
2019 - } elseif( $condition['campaign_privilege'] == 'point' ) {
2020 - foreach( $cart as $cart_row ) {
2021 - $rate = get_post_meta( $cart_row['post_id'], '_itemPointrate', true );
2022 - $price = $cart_row['price'] * $cart_row['quantity'];
2023 - $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2024 - if( in_array( $condition['campaign_category'], $cats ) ) {
2025 - $point += $price * $rate / 100 * $condition['privilege_point'];
2026 - } else {
2027 - $point += $price * $rate / 100;
2028 - }
2029 - }
2030 - }
2031 - } else {
2032 - foreach( $cart as $cart_row ) {
2033 - $rate = get_post_meta( $cart_row['post_id'], '_itemPointrate', true );
2034 - $price = $cart_row['price'] * $cart_row['quantity'];
2035 - $point += $price * $rate / 100;
2036 - }
2037 - }
2038 - }
2039 -
2040 - if( 0 < $point ) $point = ceil($point);
2041 -//20130425ysk start 0000699
2042 - if( 0 < $use_point ) {
2043 - $point = ceil( $point - ( $point * $use_point / $total_items_price ) );
2044 - if( 0 > $point )
2045 - $point = 0;
2046 - }
2047 -//20130425ysk end
2048 - $point = apply_filters( 'usces_filter_set_point_recalculation', $point, $condition, $cart, $meminfo, $use_point );
2049 - $total_price = $total_items_price - $use_point + $discount + $shipping_charge + $cod_fee;
2050 - $total_price = apply_filters('usces_filter_set_cart_fees_total_price', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee);
2051 - $tax = $usces->getTax( $total_price );
2052 - $total_full_price = $total_price + $tax;
2053 - $total_full_price = apply_filters('usces_filter_set_cart_fees_total_full_price', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee);
2054 -
2055 - return $res."#usces#".$discount."#usces#".usces_crform( $tax, false, false, 'return', false )."#usces#".$point."#usces#".usces_crform( $total_full_price, false, false, 'return', false );
2056 -}
2057 -?>
1 +<?php
2 +/**
3 + * Welcart SKU and Options
4 + *
5 + * Functions for manipulating SKUs and Options in product registration.
6 + *
7 + * @package Welcart
8 + */
9 +
10 +defined( 'ABSPATH' ) || exit;
11 +
12 +/**
13 + * Add Item SKU.
14 + *
15 + * @since 2.3.3
16 + * @param string $post_id Post ID.
17 + * @param string $new_sku SKU data.
18 + * @param boolean $check Switch of check.
19 + * @return New meta id.
20 + */
21 +function usces_add_sku( $post_id, $new_sku, $check = true ) {
22 + global $wpdb, $usces;
23 +
24 + if ( $check ) {
25 +
26 + $skus = wel_get_skus( $post_id, 'sort', false );
27 +
28 + if ( ! empty( $skus ) ) {
29 +
30 + $sku_num = count( $skus );
31 + $unique = true;
32 + $sortnull = true;
33 + $sort = array();
34 + foreach ( (array) $skus as $sku ) {
35 +
36 + if ( (string) $sku['code'] === (string) $new_sku['code'] ) {
37 + $unique = false;
38 + }
39 + if ( ! isset( $sku['sort'] ) ) {
40 + $sortnull = false;
41 + }
42 + $sort[] = $sku['sort'];
43 + }
44 + if ( ! $unique ) {
45 + return -1;
46 + }
47 +
48 + rsort( $sort );
49 + $next_number = $sort[0] + 1;
50 + $unique_sort = array_unique( $sort );
51 + if ( count( $unique_sort ) !== $sku_num || $sku_num != $next_number || ! $sortnull ) {
52 + // To repair the sort data.
53 + $i = 0;
54 + foreach ( (array) $skus as $sku ) {
55 + $sku['sort'] = $i;
56 + wel_update_sku_data_by_id( $sku['meta_id'], $post_id, $sku );
57 + $i++;
58 + }
59 + }
60 + }
61 + $new_sku['sort'] = ! empty( $sku_num ) ? $sku_num : 0;
62 + }
63 + $new_sku = $usces->stripslashes_deep_post( $new_sku );
64 + $new_meta_id = wel_add_sku_data( $post_id, $new_sku );
65 +
66 + return $new_meta_id;
67 +}
68 +
69 +/**
70 + * Add Item SKU by ajax.
71 + * Ajax response when a new SKU is registered in the SKU block on the product registration screen.
72 + *
73 + * @since 2.3.3
74 + * @param string $post_ID Post ID.
75 + * @return New meta id.
76 + */
77 +function add_item_sku_meta( $post_ID ) {
78 + global $usces;
79 +
80 + $post_id = (int) $post_ID;
81 + $value = array();
82 + $skus = array();
83 + $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
84 +
85 + $args = array(
86 + 'newskuname' => FILTER_DEFAULT,
87 + 'newskucprice' => FILTER_DEFAULT,
88 + 'newskuprice' => FILTER_DEFAULT,
89 + 'newskuzaikonum' => FILTER_DEFAULT,
90 + 'newskuzaikoselect' => FILTER_DEFAULT,
91 + 'newskudisp' => FILTER_DEFAULT,
92 + 'newskuunit' => FILTER_DEFAULT,
93 + 'newskugptekiyo' => FILTER_DEFAULT,
94 + 'newskutaxrate' => FILTER_DEFAULT,
95 + );
96 +
97 + $inputs = filter_input_array( INPUT_POST, $args );
98 +
99 + $newskuname = ( null !== $inputs['newskuname'] ) ? trim( $inputs['newskuname'] ) : '';
100 + $newskucprice = ( null !== $inputs['newskucprice'] ) ? $inputs['newskucprice'] : '';
101 + $newskuprice = ( null !== $inputs['newskuprice'] ) ? $inputs['newskuprice'] : '';
102 + $newskuzaikonum = ( null !== $inputs['newskuzaikonum'] ) ? $inputs['newskuzaikonum'] : '';
103 + $newskuzaikoselect = ( null !== $inputs['newskuzaikoselect'] ) ? $inputs['newskuzaikoselect'] : '';
104 + $newskudisp = ( null !== $inputs['newskudisp'] ) ? trim( $inputs['newskudisp'] ) : '';
105 + $newskuunit = ( null !== $inputs['newskuunit'] ) ? trim( $inputs['newskuunit'] ) : '';
106 + $newskugptekiyo = ( null !== $inputs['newskugptekiyo'] ) ? $inputs['newskugptekiyo'] : '';
107 + $newskutaxrate = ( null !== $inputs['newskutaxrate'] ) ? $inputs['newskutaxrate'] : '';
108 +
109 + if ( ! WCUtils::is_blank( $newskuname ) && ! WCUtils::is_blank( $newskuprice ) && ! WCUtils::is_blank( $newskuzaikoselect ) ) {
110 +
111 + if ( in_array( $newskuname, $protected ) ) {
112 + return false;
113 + }
114 +
115 + $value['code'] = $newskuname;
116 + $value['name'] = $newskudisp;
117 + $value['cprice'] = $newskucprice;
118 + $value['price'] = $newskuprice;
119 + $value['unit'] = $newskuunit;
120 + $value['stocknum'] = $newskuzaikonum;
121 + $value['stock'] = $newskuzaikoselect;
122 + $value['gp'] = $newskugptekiyo;
123 + $value['taxrate'] = $newskutaxrate;
124 +
125 + $value = apply_filters( 'usces_filter_add_item_sku_meta_value', $value );
126 +
127 + $res = apply_filters( 'usces_filter_before_add_item_sku_meta', false, $post_id, $value );
128 + if ( false !== $res ) {
129 + return $res;
130 + }
131 +
132 + $id = usces_add_sku( $post_ID, $value, true );
133 +
134 + return $id;
135 + } else {
136 + return false;
137 + }
138 +}
139 +
140 +/**
141 + * Update Item SKU by ajax.
142 + * Ajax response when SKU information is updated in the SKU block of the product registration screen.
143 + *
144 + * @since 2.3.3
145 + * @param string $post_ID Post ID.
146 + * @return Query result.
147 + */
148 +function up_item_sku_meta( $post_ID ) {
149 + global $wpdb, $usces;
150 +
151 + $post_id = (int) $post_ID;
152 + $value = array();
153 +
154 + $skucode = filter_input( INPUT_POST, 'skuname', FILTER_DEFAULT );
155 + $skumetaid = filter_input( INPUT_POST, 'skumetaid', FILTER_DEFAULT );
156 +
157 + $res = apply_filters( 'usces_filter_before_up_item_sku_meta', false, $post_ID, $skumetaid, $skucode );
158 + if ( false !== $res ) {
159 + return $res;
160 + }
161 +
162 + $args = array(
163 + 'skucprice' => FILTER_DEFAULT,
164 + 'skuprice' => FILTER_DEFAULT,
165 + 'skuzaikonum' => FILTER_DEFAULT,
166 + 'skuzaiko' => FILTER_DEFAULT,
167 + 'skudisp' => FILTER_DEFAULT,
168 + 'skuunit' => FILTER_DEFAULT,
169 + 'skugptekiyo' => FILTER_DEFAULT,
170 + 'sort' => FILTER_DEFAULT,
171 + 'skutaxrate' => FILTER_DEFAULT,
172 + );
173 +
174 + $inputs = filter_input_array( INPUT_POST, $args );
175 +
176 + $skucprice = ( null !== $inputs['skucprice'] ) ? trim( $inputs['skucprice'] ) : 0;
177 + $skuprice = ( null !== $inputs['skuprice'] ) ? trim( $inputs['skuprice'] ) : 0;
178 + $skuzaikonum = ( null !== $inputs['skuzaikonum'] ) ? trim( $inputs['skuzaikonum'] ) : 0;
179 + $skuzaiko = ( null !== $inputs['skuzaiko'] ) ? (int) $inputs['skuzaiko'] : '';
180 + $skudisp = ( null !== $inputs['skudisp'] ) ? trim( $inputs['skudisp'] ) : '';
181 + $skuunit = ( null !== $inputs['skuunit'] ) ? trim( $inputs['skuunit'] ) : '';
182 + $skugptekiyo = ( null !== $inputs['skugptekiyo'] ) ? (int) $inputs['skugptekiyo'] : 0;
183 + $skusort = ( null !== $inputs['sort'] ) ? $inputs['sort'] : 0;
184 + $skutaxrate = ( null !== $inputs['skutaxrate'] ) ? $inputs['skutaxrate'] : '';
185 +
186 + $value['code'] = $skucode;
187 + $value['name'] = $skudisp;
188 + $value['cprice'] = $skucprice;
189 + $value['price'] = $skuprice;
190 + $value['unit'] = $skuunit;
191 + $value['stocknum'] = $skuzaikonum;
192 + $value['stock'] = $skuzaiko;
193 + $value['gp'] = $skugptekiyo;
194 + $value['sort'] = $skusort;
195 + $value['taxrate'] = $skutaxrate;
196 +
197 + $value = $usces->stripslashes_deep_post( $value );
198 +
199 + $skus = wel_get_skus( $post_id, 'meta_id', false );
200 + foreach ( $skus as $sku ) {
201 + if ( (string) $sku['code'] === (string) $skucode && $sku['meta_id'] != $skumetaid ) {
202 + return -1;
203 + }
204 + }
205 +
206 + $value = apply_filters( 'usces_filter_up_item_sku_meta_value', $value );
207 +
208 + if ( ! WCUtils::is_blank( $skumetaid ) && ! WCUtils::is_blank( $skucode ) && ! WCUtils::is_blank( $skuprice ) ) {
209 +
210 + $res = wel_update_sku_data_by_id( $skumetaid, $post_id, $value );
211 + return $res;
212 +
213 + } else {
214 +
215 + return false;
216 + }
217 +}
218 +
219 +/**
220 + * Delete Item SKU by ajax.
221 + * Ajax response when SKU is deleted in the SKU block of the product registration screen.
222 + *
223 + * @since 2.3.3
224 + * @param string $post_ID Post ID.
225 + * @return Result.
226 + */
227 +function del_item_sku_meta( $post_ID ) {
228 + global $wpdb, $usces;
229 +
230 + $post_id = (int) $post_ID;
231 + $meta_id = filter_input( INPUT_POST, 'skumetaid', FILTER_VALIDATE_INT );
232 +
233 + $res = apply_filters( 'usces_filter_before_del_item_sku_meta', false, $post_id, $meta_id );
234 + if ( false !== $res ) {
235 + return $res;
236 + }
237 +
238 + $res = wel_delete_sku_data_by_id( $meta_id, $post_id );
239 +
240 + // Resort.
241 + $skus = wel_get_skus( $post_id, 'meta_id', false );
242 +
243 + if ( ! empty( $skus ) ) {
244 + uasort( $skus, function($a, $b) {
245 + return $a['sort'] <=> $b['sort'];
246 + });
247 +
248 + $i = 0;
249 + foreach ( $skus as $sku ) {
250 + $sku['sort'] = $i;
251 + $meta_id = $sku['meta_id'];
252 + wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
253 + $i++;
254 + }
255 + }
256 + return;
257 +}
258 +
259 +/**
260 + * List of Item SKUs.
261 + * To generate a list of SKUs in the SKU block of the product registration screen.
262 + *
263 + * @since 2.3.3
264 + * @param string $skus All SKUs.
265 + */
266 +function list_item_sku_meta( $skus ) {
267 +
268 + if ( empty( $skus ) ) { // Exit if no meta.
269 + ?>
270 + <table id="skulist-table" class="list" style="display: none;">
271 + <thead>
272 + <tr>
273 + <th class="hanldh" rowspan="2">&emsp;</th>
274 + <th><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
275 + <th><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
276 + <th><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
277 + <th><?php esc_html_e( 'stock', 'usces' ); ?></th>
278 + <th><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
279 + </tr>
280 + </thead>
281 + <tbody id="item-sku-list">
282 + <tr><td></td><td></td><td></td><td></td><td></td></tr>
283 + </tbody>
284 + </table>
285 + <?php
286 + } else {
287 + ?>
288 + <table id="skulist-table" class="list">
289 + <thead>
290 + <tr>
291 + <th class="hanldh" rowspan="2">&emsp;</th>
292 + <th class="item-sku-key"><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
293 + <th class="item-sku-cprice"><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
294 + <th class="item-sku-price"><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
295 + <th class="item-sku-zaikonum"><?php esc_html_e( 'stock', 'usces' ); ?></th>
296 + <th class="item-sku-zaiko"><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
297 + </tr>
298 + <tr>
299 + <th><?php esc_html_e( 'SKU display name ', 'usces' ); ?></th>
300 + <th><?php esc_html_e( 'unit', 'usces' ); ?></th>
301 + <?php
302 + $advance_title = '<th colspan="2">&nbsp;</th>';
303 + echo apply_filters( 'usces_filter_sku_meta_form_advance_title', $advance_title );
304 + ?>
305 + <th><?php esc_html_e( 'Apply business package', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title2', '' ); ?>
306 + </tr>
307 + </thead>
308 + <tbody id="item-sku-list">
309 + <?php
310 + foreach ( $skus as $sku ) {
311 + echo _list_item_sku_meta_row( $sku );
312 + }
313 + ?>
314 + </tbody>
315 + </table>
316 + <?php
317 + }
318 +}
319 +
320 +/**
321 + * Row of Item SKU List.
322 + * To generate a list of SKUs in the SKU block of the product registration screen.
323 + *
324 + * @since 2.3.3
325 + * @param string $sku SKU.
326 + */
327 +function _list_item_sku_meta_row( $sku ) {
328 + $r = '';
329 + $style = '';
330 +
331 + $key = $sku['code'];
332 + $cprice = $sku['cprice'];
333 + $price = $sku['price'];
334 + $zaikonum = $sku['stocknum'];
335 + $zaiko = $sku['stock'];
336 + $skudisp = $sku['name'];
337 + $skuunit = $sku['unit'];
338 + $skugptekiyo = $sku['gp'];
339 + $id = (int) $sku['meta_id'];
340 + $zaikoselectarray = get_option( 'usces_zaiko_status', array() );
341 + $zaikoselectarray = apply_filters( 'usces_filter_zaikoselectarray', $zaikoselectarray );
342 + $zaikoselect_count = ( $zaikoselectarray && is_array( $zaikoselectarray ) ) ? count( $zaikoselectarray ) : 0;
343 + $sort = (int) $sku['sort'];
344 + $sku_colspan = apply_filters( 'usces_filter_sku_meta_colspan', '6' );
345 +
346 + ob_start();
347 + ?>
348 + <tr class="metastuffrow"><td colspan="<?php echo esc_attr( $sku_colspan ); ?>">
349 + <table id="itemsku-<?php echo esc_attr( $id ); ?>" class="metastufftable">
350 + <tr>
351 + <th class="handlb" rowspan="<?php echo apply_filters( 'usces_filter_sku_meta_rowspan', '3' ); ?>">&emsp;</th>
352 + <td class="item-sku-key"><input name="itemsku[<?php echo esc_attr( $id ); ?>][key]" id="itemsku[<?php echo esc_attr( $id ); ?>][key]" class="skuname metaboxfield" type="text" value="<?php echo esc_attr( $key ); ?>" /></td>
353 + <td class="item-sku-cprice"><input name="itemsku[<?php echo esc_attr( $id ); ?>][cprice]" id="itemsku[<?php echo esc_attr( $id ); ?>][cprice]" class="skuprice metaboxfield" type="text" value="<?php echo esc_attr( $cprice ); ?>" /></td>
354 + <td class="item-sku-price"><input name="itemsku[<?php echo esc_attr( $id ); ?>][price]" id="itemsku[<?php echo esc_attr( $id ); ?>][price]" class="skuprice metaboxfield" type="text" value="<?php echo esc_attr( $price ); ?>" /></td>
355 + <td class="item-sku-zaikonum"><input name="itemsku[<?php echo esc_attr( $id ); ?>][zaikonum]" id="itemsku[<?php echo esc_attr( $id ); ?>][zaikonum]" class="skuzaikonum metaboxfield" type="text" value="<?php echo esc_attr( $zaikonum ); ?>" /></td>
356 + <td class="item-sku-zaiko">
357 + <select id="itemsku[<?php echo esc_attr( $id ); ?>][zaiko]" name="itemsku[<?php echo esc_attr( $id ); ?>][zaiko]" class="skuzaiko metaboxfield">
358 + <?php
359 + for ( $i = 0; $i < $zaikoselect_count; $i++ ) {
360 + ?>
361 + <option value="<?php echo esc_attr( $i ); ?>"<?php selected( $zaiko, $i ); ?>><?php echo esc_attr( $zaikoselectarray[ $i ] ); ?></option>
362 + <?php
363 + }
364 + ?>
365 + </select>
366 + </td><?php echo apply_filters( 'usces_filter_sku_meta_field1', '', $sku ); ?>
367 + </tr>
368 + <tr>
369 + <td class="item-sku-key"><input name="itemsku[<?php echo esc_attr( $id ); ?>][skudisp]" id="itemsku[<?php echo esc_attr( $id ); ?>][skudisp]" class="skudisp metaboxfield" type="text" value="<?php echo esc_attr( $skudisp ); ?>" />
370 + </td>
371 + <td class="item-sku-cprice"><input name="itemsku[<?php echo esc_attr( $id ); ?>][skuunit]" id="itemsku[<?php echo esc_attr( $id ); ?>][skuunit]" class="skuunit metaboxfield" type="text" value="<?php echo esc_attr( $skuunit ); ?>" /></td>
372 + <?php
373 + $default_field = "\n\t\t" . '<td colspan="2">&nbsp;</td>';
374 + echo apply_filters( 'usces_filter_sku_meta_row_advance', $default_field, $sku );
375 + ?>
376 + <td class="item-sku-zaiko">
377 + <select id="itemsku[<?php echo esc_attr( $id ); ?>][skugptekiyo]" name="itemsku[<?php echo esc_attr( $id ); ?>][skugptekiyo]" class="skugptekiyo metaboxfield">
378 + <option value="0"<?php selected( $skugptekiyo, 0 ); ?>><?php esc_html_e( 'Not apply', 'usces' ); ?></option>
379 + <option value="1"<?php selected( $skugptekiyo, 1 ); ?>><?php esc_html_e( 'Apply', 'usces' ); ?></option>
380 + </select>
381 + </td><?php echo apply_filters( 'usces_filter_sku_meta_field2', '', $sku ); ?>
382 + </tr>
383 + <?php echo apply_filters( 'usces_filter_sku_meta_row', '', $sku ); ?>
384 + <tr>
385 + <td colspan="<?php echo esc_attr( $sku_colspan - 1 ); ?>" class="submittd">
386 + <div id="skusubmit-<?php echo esc_attr( $id ); ?>" class="submit">
387 + <input name="deleteitemsku[<?php echo esc_attr( $id ); ?>]" id="deleteitemsku[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Delete' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('deleteitemsku', <?php echo esc_attr( $id ); ?>);" />
388 + <input name="updateitemsku[<?php echo esc_attr( $id ); ?>]" id="updateitemsku[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Update' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('updateitemsku', <?php echo esc_attr( $id ); ?>);" />
389 + <input name="itemsku[<?php echo esc_attr( $id ); ?>][sort]" id="itemsku[<?php echo esc_attr( $id ); ?>][sort]" type="hidden" value="<?php echo esc_attr( $sort ); ?>" />
390 + <?php usces_sku_meta_row_reduced_taxrate( $sku ); ?>
391 + </div>
392 + <div id="itemsku_loading-<?php echo esc_attr( $id ); ?>" class="meta_submit_loading"></div>
393 + </td>
394 + </tr>
395 + </table>
396 + </td></tr>
397 + <?php
398 + $r = ob_get_contents();
399 + ob_end_clean();
400 + return $r;
401 +}
402 +
403 +/**
404 + * New SKU Form.
405 + * The form to add a new SKU in the SKU block on the product registration screen.
406 + *
407 + * @since 2.3.3
408 + */
409 +function item_sku_meta_form() {
410 + $sku_colspan = apply_filters( 'usces_filter_sku_meta_colspan', '6' );
411 + ?>
412 + <div id="sku_ajax-response"></div>
413 + <p><strong><?php esc_html_e( 'Add new SKU', 'usces' ); ?> : </strong></p>
414 + <table id="newsku">
415 + <thead>
416 + <tr>
417 + <th class="left"><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
418 + <th><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
419 + <th><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
420 + <th><?php esc_html_e( 'stock', 'usces' ); ?></th>
421 + <th><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
422 + </tr>
423 + <tr>
424 + <th><?php esc_html_e( 'SKU display name ', 'usces' ); ?></th>
425 + <th><?php esc_html_e( 'unit', 'usces' ); ?></th>
426 + <?php
427 + $advance_title = '<th colspan="2">&nbsp;</th>';
428 + echo apply_filters( 'usces_filter_sku_meta_form_advance_title', $advance_title );
429 + ?>
430 + <th><?php esc_html_e( 'Apply business package', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title2', '' ); ?>
431 + </tr>
432 + </thead>
433 + <tbody>
434 + <tr>
435 + <td id="newskuleft" class="item-sku-key"><input type="text" id="newskuname" name="newskuname" class="newskuname metaboxfield" value="" /></td>
436 + <td class="item-sku-cprice"><input type="text" id="newskucprice" name="newskucprice" class="newskuprice metaboxfield" /></td>
437 + <td class="item-sku-price"><input type="text" id="newskuprice" name="newskuprice" class="newskuprice metaboxfield" /></td>
438 + <td class="item-sku-zaikonum"><input type="text" id="newskuzaikonum" name="newskuzaikonum" class="newskuzaikonum metaboxfield" /></td>
439 + <td class="item-sku-zaiko">
440 + <select id="newskuzaikoselect" name="newskuzaikoselect" class="newskuzaikoselect metaboxfield">
441 + <?php
442 + $zaikoselectarray = get_option( 'usces_zaiko_status', array() );
443 + foreach ( $zaikoselectarray as $v => $l ) {
444 + echo "\n" . '<option value="' . esc_attr( $v ) . '">' . esc_html( $l ) . '</option>';
445 + }
446 + ?>
447 + </select>
448 + </td><?php echo apply_filters( 'usces_filter_newsku_meta_field1', '' ); ?>
449 + </tr>
450 + <tr>
451 + <td class="item-sku-key"><input type="text" id="newskudisp" name="newskudisp" class="newskudisp metaboxfield" /></td>
452 + <td class="item-sku-cprice"><input type="text" id="newskuunit" name="newskuunit" class="newskuunit metaboxfield" /></td>
453 + <?php
454 + $advance_field = '<td class="item-sku-price">&nbsp;</td><td class="item-sku-zaikonum">&nbsp;</td>';
455 + echo apply_filters( 'usces_filter_sku_meta_form_advance_field', $advance_field );
456 + ?>
457 + <td class="item-sku-zaiko">
458 + <select id="newskugptekiyo" name="newskugptekiyo" class="newskugptekiyo metaboxfield">
459 + <option value="0"><?php esc_html_e( 'Not apply', 'usces' ); ?></option>
460 + <option value="1"><?php esc_html_e( 'Apply', 'usces' ); ?></option>
461 + </select>
462 + </td><?php echo apply_filters( 'usces_filter_newsku_meta_field2', '' ); ?>
463 + </tr>
464 + <?php echo apply_filters( 'usces_filter_newsku_meta_row', '' ); ?>
465 + <tr>
466 + <td colspan="<?php echo esc_attr( $sku_colspan - 1 ); ?>" class="submittd">
467 + <div id="newskusubmit" class="submit">
468 + <?php
469 + $add_itemsku_button = '<input name="add_itemsku" type="button" class="button" id="add_itemsku" tabindex="9" value="' . esc_html__( 'Add SKU', 'usces' ) . '" onclick="if( jQuery(\'#post_ID\').val() < 0 ) return; itemSku.post(\'additemsku\', 0);" />';
470 + echo apply_filters( 'usces_filter_newsku_meta_add_button', $add_itemsku_button );
471 + ?>
472 + <?php usces_newsku_meta_row_reduced_taxrate(); ?>
473 + </div>
474 + <div id="newitemsku_loading" class="meta_submit_loading"></div>
475 + </td>
476 + </tr>
477 + </tbody>
478 + </table>
479 + <?php
480 +}
481 +
482 +/**
483 + * Get Post Meta using MetaID.
484 + *
485 + * @since 2.3.3
486 + * @param int $meta_id Meta ID.
487 + * @return MetaData|false
488 + */
489 +function usces_get_post_meta_by_metaid( $meta_id ) {
490 + global $wpdb;
491 +
492 + $res = $wpdb->get_row(
493 + $wpdb->prepare(
494 + "SELECT * FROM $wpdb->postmeta WHERE meta_id = %d",
495 + $meta_id
496 + ),
497 + ARRAY_A
498 + );
499 + return $res;
500 +}
501 +
502 +/**
503 + * Get Post meta.
504 + * Get product metadata by specifying meta_key.
505 + *
506 + * @since 2.3.3
507 + * @param string $post_id Post ID.
508 + * @param string $key Meta key.
509 + * @param boolean $cache Switch of cache.
510 + * @return MetaData|false
511 + */
512 +function usces_get_post_meta( $post_id, $key, $cache = true ) {
513 + global $wpdb;
514 +
515 + $cache_key = 'wel_post_meta_' . $post_id . '_' . $key;
516 + if ( true === $cache ) {
517 + $meta_data = wp_cache_get( $cache_key );
518 + } else {
519 + $meta_data = false;
520 + }
521 + if ( false === $meta_data || is_admin() || wp_doing_ajax() ) {
522 + $meta_data = $wpdb->get_results(
523 + $wpdb->prepare(
524 + "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s",
525 + $post_id,
526 + $key
527 + ),
528 + ARRAY_A
529 + );
530 + if ( null !== $meta_data ) {
531 + wp_cache_set( $cache_key, $meta_data );
532 + }
533 + }
534 +
535 + if ( null === $meta_data ) {
536 + return false;
537 + } else {
538 + return $meta_data;
539 + }
540 +}
541 +
542 +/**
543 + * Sort Options.
544 + * Ajax response when changing the order of optios in the SKU block of the product registration screen.
545 + *
546 + * @since 2.3.3
547 + * @param int $post_id Post ID.
548 + * @param int $metastr Meta ids.
549 + */
550 +function usces_sort_item_opts( $post_id, $metastr ) {
551 + global $wpdb;
552 +
553 + $meta_ids = explode( ',', $metastr );
554 + $opts = wel_get_opts( $post_id, 'meta_id', false );
555 +
556 + if ( ! empty( $meta_ids ) ) {
557 +
558 + $i = 0;
559 + foreach ( $meta_ids as $meta_id ) {
560 +
561 + $opt = $opts[ $meta_id ];
562 + $opt['sort'] = $i;
563 +
564 + wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
565 +
566 + $i++;
567 + }
568 + }
569 +}
570 +
571 +/**
572 + * Sort SKUs.
573 + * Ajax response when changing the order of SKUs in the SKU block of the product registration screen.
574 + *
575 + * @since 2.3.3
576 + * @param int $post_id Post ID.
577 + * @param int $metastr Meta ids.
578 + */
579 +function usces_sort_item_skus( $post_id, $metastr ) {
580 + global $wpdb;
581 +
582 + $meta_ids = explode( ',', $metastr );
583 + $skus = wel_get_skus( $post_id, 'meta_id', false );
584 +
585 + if ( ! empty( $meta_ids ) ) {
586 +
587 + $i = 0;
588 + foreach ( $meta_ids as $meta_id ) {
589 +
590 + $i = 0;
591 + foreach ( $meta_ids as $meta_id ) {
592 +
593 + $sku = $skus[ $meta_id ];
594 + $sku['sort'] = $i;
595 +
596 + wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
597 +
598 + $i++;
599 + }
600 + }
601 + }
602 +}
603 +
604 +/**
605 + * Get all options by post id.
606 + *
607 + * @since 2.3.3
608 + * @param int $post_id Post ID.
609 + * @param string $keyflag Sort key.
610 + * @param bool $cache Cache.
611 + */
612 +function usces_get_opts( $post_id, $keyflag = 'sort', $cache = true ) {
613 + $opts = wel_get_opts( $post_id, $keyflag, $cache );
614 + return $opts;
615 +}
616 +
617 +/**
618 + * Add Item Option.
619 + *
620 + * @since 2.3.3
621 + * @param string $post_id Post ID.
622 + * @param string $new_value SKU data.
623 + * @param boolean $check Switch of check.
624 + * @return New meta id.
625 + */
626 +function usces_add_opt( $post_id, $new_value, $check = true ) {
627 + global $wpdb, $usces;
628 +
629 + if ( $check ) {
630 +
631 + $opts = wel_get_opts( $post_id, 'meta_id', false );
632 +
633 + if ( ! empty( $opts ) && is_array( $opts ) ) {
634 +
635 + $meta_num = count( $opts );
636 + $unique = true;
637 + $sortnull = true;
638 + $sort = array();
639 +
640 + foreach ( (array) $opts as $opt ) {
641 +
642 + if ( $opt['name'] === $new_value['name'] ) {
643 + $unique = false;
644 + }
645 + if ( ! isset( $opt['sort'] ) ) {
646 + $sortnull = false;
647 + }
648 + $sort[] = $opt['sort'];
649 + }
650 +
651 + if ( ! $unique ) {
652 + return -1;
653 + }
654 +
655 + rsort( $sort );
656 + $next_number = reset( $sort ) + 1;
657 + $unique_sort = array_unique( $sort );
658 + if ( $meta_num !== count( $unique_sort ) || $meta_num !== $next_number || ! $sortnull ) {
659 + // To repair the sort data.
660 + $i = 0;
661 + foreach ( (array) $opts as $opt ) {
662 + $opt['sort'] = $i;
663 + wel_update_opt_data_by_id( $opt['meta_id'], $post_id, $opt );
664 + $i++;
665 + }
666 + }
667 + }
668 +
669 + $new_value['sort'] = ! empty( $meta_num ) ? $meta_num : 0;
670 + }
671 +
672 + $id = wel_add_opt_data( $post_id, $new_value );
673 + return $id;
674 +}
675 +
676 +/**
677 + * List item option
678 + *
679 + * @param array $opts Options.
680 + */
681 +function list_item_option_meta( $opts ) {
682 + // Exit if no meta.
683 + if ( ! $opts ) {
684 + ?>
685 + <table id="optlist-table" class="list" style="display: none;">
686 + <thead>
687 + <tr>
688 + <th class="hanldh">&emsp;</th>
689 + <th class="item-opt-key"><?php esc_html_e( 'option name', 'usces' ); ?></th>
690 + <th class="item-opt-value"><?php esc_html_e( 'selected amount', 'usces' ); ?></th>
691 + </tr>
692 + </thead>
693 + <tbody id="item-opt-list">
694 + <tr><td></td></tr>
695 + </tbody>
696 + </table>
697 + <?php
698 + } else {
699 + ?>
700 + <table id="optlist-table" class="list">
701 + <thead>
702 + <tr>
703 + <th class="hanldh">&emsp;</th>
704 + <th class="item-opt-key"><?php esc_html_e( 'option name', 'usces' ); ?></th>
705 + <th class="item-opt-value"><?php esc_html_e( 'selected amount', 'usces' ); ?></th>
706 + </tr>
707 + </thead>
708 + <tbody id="item-opt-list">
709 + <?php
710 + foreach ( $opts as $opt ) {
711 + echo _list_item_option_meta_row( $opt );
712 + }
713 + ?>
714 + </tbody>
715 + </table>
716 + <?php
717 + }
718 +}
719 +
720 +/**
721 + * Option meta row
722 + *
723 + * @param array $opt Options.
724 + */
725 +function _list_item_option_meta_row( $opt ) {
726 + $r = '';
727 + $style = '';
728 + $means = get_option( 'usces_item_option_select', array() );
729 +
730 + $name = $opt['name'];
731 + $meansoption = '';
732 + foreach ( $means as $meankey => $meanvalue ) {
733 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $opt['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
734 + }
735 + $essential = ( 1 === (int) $opt['essential'] ) ? ' checked="checked"' : '';
736 + $value = '';
737 + if ( is_array( $opt['value'] ) ) {
738 + foreach ( $opt['value'] as $k => $v ) {
739 + $value .= $v . "\n";
740 + }
741 + } else {
742 + $value = $opt['value'];
743 + }
744 + $value = trim( $value );
745 + $id = (int) $opt['meta_id'];
746 + $sort = (int) $opt['sort'];
747 +
748 + ob_start();
749 + ?>
750 + <tr class="metastuffrow"><td colspan="3">
751 + <table id="itemopt-<?php echo esc_attr( $id ); ?>" class="metastufftable">
752 + <tr>
753 + <th class="handlb" rowspan="2">&emsp;</th>
754 + <td class="item-opt-key">
755 + <div><input name="itemopt[<?php echo esc_attr( $id ); ?>][name]" id="itemopt[<?php echo esc_attr( $id ); ?>][name]" class="metaboxfield" type="text" size="20" value="<?php echo esc_attr( $name ); ?>" /></div>
756 + <div class="optcheck">
757 + <select name="itemopt[<?php echo esc_attr( $id ); ?>][means]" id="itemopt[<?php echo esc_attr( $id ); ?>][means]"><?php wel_esc_script_e( $meansoption ); ?></select>
758 + <label for="itemopt[<?php echo esc_attr( $id ); ?>][essential]"><input name="itemopt[<?php echo esc_attr( $id ); ?>][essential]" id="itemopt[<?php echo esc_attr( $id ); ?>][essential]" type="checkbox" value="1"<?php echo esc_attr( $essential ); ?> class="metaboxcheckfield" /><?php esc_html_e( 'Required','usces' ); ?></label>
759 + </div>
760 + </td>
761 + <td class="item-opt-value">
762 + <textarea name="itemopt[<?php echo esc_attr( $id ); ?>][value]" id="itemopt[<?php echo esc_attr( $id ); ?>][value]" class="metaboxfield"><?php echo esc_html( $value ); ?></textarea>
763 + </td>
764 + </tr>
765 + <tr>
766 + <td colspan="2" class="submittd">
767 + <div id="itemoptsubmit-<?php echo esc_attr( $id ); ?>" class="submit">
768 + <input name="deleteitemopt[<?php echo esc_attr( $id ); ?>]" id="deleteitemopt[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Delete' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('deleteitemopt', <?php echo esc_attr( $id ); ?>);" />
769 + <input name="updateitemopt[<?php echo esc_attr( $id ); ?>]" id="updateitemopt[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Update' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('updateitemopt', <?php echo esc_attr( $id ); ?>);" />
770 + <input name="itemopt[<?php echo esc_attr( $id ); ?>][sort]" id="itemopt[<?php echo esc_attr( $id ); ?>][sort]" type="hidden" value="<?php echo esc_attr( $sort ); ?>" />
771 + </div>
772 + <div id="itemopt_loading-<?php echo esc_attr( $id ); ?>" class="meta_submit_loading"></div>
773 + </td>
774 + </tr>
775 + </table>
776 + </td></tr>
777 + <?php
778 + $r = ob_get_contents();
779 + ob_end_clean();
780 + return $r;
781 +}
782 +
783 +/**
784 + * Common option meta form
785 + */
786 +function common_option_meta_form() {
787 + $means = get_option( 'usces_item_option_select', array() );
788 + $meansoption = '';
789 + foreach ( $means as $meankey => $meanvalue ) {
790 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '">' . esc_html( $meanvalue ) . "</option>\n";
791 + }
792 + ?>
793 + <div id="itemopt_ajax-response"></div>
794 + <p><strong><?php esc_html_e( 'Add a new option', 'usces' ); ?> : </strong></p>
795 + <table id="newmeta2">
796 + <thead>
797 + <tr>
798 + <th class="left"><label for="metakeyselect"><?php esc_html_e( 'option name', 'usces' ); ?></label></th>
799 + <th><label for="metavalue"><?php esc_html_e( 'selected amount', 'usces' ); ?></label></th>
800 + </tr>
801 + </thead>
802 + <tbody>
803 + <tr>
804 + <td class="item-opt-key">
805 + <input type="text" id="newoptname" name="newoptname" class="metaboxfield" tabindex="7" value="" />
806 + <div class="optcheck">
807 + <select name="newoptmeans" id="newoptmeans" class="metaboxfield long"><?php wel_esc_script_e( $meansoption ); ?></select>
808 + <label for="newoptessential"><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php esc_html_e( 'Required', 'usces' ); ?></label>
809 + </div>
810 + </td>
811 + <td class="item-opt-value"><textarea id="newoptvalue" name="newoptvalue" class="metaboxfield"></textarea></td>
812 + </tr>
813 + <tr>
814 + <td colspan="2" class="submittd">
815 + <div id="newcomoptsubmit" class="submit">
816 + <input name="add_comopt" type="button" class="button" id="add_comopt" tabindex="9" value="<?php esc_attr_e( 'Add common options', 'usces' ); ?>" onclick="itemOpt.post('addcommonopt', 0);" />
817 + </div>
818 + <div id="newcomopt_loading" class="meta_submit_loading"></div>
819 + </td>
820 + </tr>
821 + </tbody>
822 + </table>
823 + <?php
824 +}
825 +
826 +/**
827 + * Item option meta form
828 + */
829 +function item_option_meta_form() {
830 + $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
831 + $cart_number = (int) get_option( 'usces_cart_number' );
832 + $opts = usces_get_opts( $cart_number );
833 + $means = get_option( 'usces_item_option_select', array() );
834 + $meansoption = '';
835 + foreach ( $means as $meankey => $meanvalue ) {
836 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '">' . esc_html( $meanvalue ) . "</option>\n";
837 + }
838 + ?>
839 + <div id="itemopt_ajax-response"></div>
840 + <p><strong><?php esc_html_e( 'Applicable product options', 'usces' ); ?> : </strong></p>
841 + <table id="newmeta2">
842 + <thead>
843 + <tr>
844 + <th class="item-opt-key"><label for="metakeyselect"><?php esc_html_e( 'option name', 'usces' ); ?></label></th>
845 + <th class="item-opt-value"><label for="metavalue"><?php esc_html_e( 'selected amount', 'usces' ); ?></label></th>
846 + </tr>
847 + </thead>
848 + <tbody>
849 + <tr>
850 + <td class='item-opt-key'>
851 + <?php if ( ! empty( $opts ) ) { ?>
852 + <select id="optkeyselect" name="optkeyselect" class="optkeyselect metaboxfield" tabindex="7" onchange="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('keyselect', this.value);">
853 + <option value="#NONE#"><?php esc_html_e( '-- Select --', 'usces' ); ?></option>
854 + <?php foreach ( $opts as $opt ) { ?>
855 + <option value="<?php echo esc_attr( $opt['meta_id'] ); ?>"><?php echo esc_attr( $opt['name'] ); ?></option>
856 + <?php } ?>
857 + </select>
858 + <input type="hidden" id="newoptname" name="newoptname" class="metaboxfield" />
859 + <div class="optcheck">
860 + <select name="newoptmeans" id="newoptmeans"><?php wel_esc_script_e( $meansoption ); ?></select>
861 + <label for="newoptessential"><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php esc_html_e( 'Required', 'usces' ); ?></label>
862 + </div>
863 + <?php } else { ?>
864 + <p><?php esc_html_e( 'Please create a common option.', 'usces' ); ?></p>
865 + <?php } ?>
866 + </td>
867 + <td class="item-opt-value"><textarea id="newoptvalue" name="newoptvalue" class="metaboxfield"></textarea></td>
868 + </tr>
869 + <tr>
870 + <td colspan="2" class="submittd">
871 + <?php if ( is_array( $opts ) ) { ?>
872 + <div id="newitemoptsubmit" class="submit">
873 + <input name="add_itemopt" type="button" class='button' id="add_itemopt" tabindex="9" value="<?php esc_attr_e( 'Apply an option', 'usces' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('additemopt', 0);" />
874 + </div>
875 + <div id="newitemopt_loading" class="meta_submit_loading"></div>
876 + <?php } ?>
877 + </td>
878 + </tr>
879 + </tbody>
880 + </table>
881 + <?php
882 +}
883 +
884 +/**
885 + * Add item option meta
886 + *
887 + * @param int $post_ID Post ID.
888 + * @return mixed
889 + */
890 +function add_item_option_meta( $post_ID ) {
891 + global $usces;
892 +
893 + $post_id = (int) $post_ID;
894 + $value = array();
895 + $opts = array();
896 + $protected = array(
897 + '#NONE#',
898 + '_wp_attached_file',
899 + '_wp_attachment_metadata',
900 + '_wp_old_slug',
901 + '_wp_page_template',
902 + );
903 +
904 + $args = array(
905 + 'newoptcode' => FILTER_SANITIZE_SPECIAL_CHARS,
906 + 'newoptname' => FILTER_SANITIZE_SPECIAL_CHARS,
907 + 'newoptmeans' => array(
908 + 'filter' => FILTER_VALIDATE_INT,
909 + 'flags' => FILTER_REQUIRE_SCALAR,
910 + 'options' => array( 'defalut' => 0 ),
911 + ),
912 + 'newoptessential' => array(
913 + 'filter' => FILTER_VALIDATE_INT,
914 + 'flags' => FILTER_REQUIRE_SCALAR,
915 + 'options' => array( 'defalut' => 0 ),
916 + ),
917 + 'newoptvalue' => FILTER_DEFAULT,
918 + );
919 + $inputs = filter_input_array( INPUT_POST, $args );
920 + $newoptcode = $inputs['newoptcode'];
921 + $newoptname = $inputs['newoptname'];
922 + $newoptmeans = $inputs['newoptmeans'];
923 + $newoptessential = $inputs['newoptessential'];
924 + $newoptvalue = $inputs['newoptvalue'];
925 +
926 + if ( ( $newoptmeans >= 2 || WCUtils::is_zero( $newoptvalue ) || ! empty ( $newoptvalue ) ) && ! empty ( $newoptname ) ) {
927 +
928 + if ( $newoptname ) {
929 + $metakey = $newoptname; // default.
930 + }
931 +
932 + if ( in_array( $metakey, $protected ) ) {
933 + return false;
934 + }
935 +
936 + $WelItem = new Welcart\ItemData( $post_id, false );
937 + $format = $WelItem->get_opt_format();
938 +
939 + $format['post_id'] = $post_id;
940 + $format['code'] = $newoptcode;
941 + $format['name'] = str_replace( "\\", '', $newoptname );
942 + $format['means'] = $newoptmeans;
943 + $format['essential'] = $newoptessential;
944 + $format['value'] = str_replace( "\\", '', $newoptvalue );
945 +
946 + $value = $usces->stripslashes_deep_post( $format );
947 +
948 + $id = usces_add_opt( $post_id, $value );
949 +
950 + return $id;
951 + } else {
952 + return false;
953 + }
954 +}
955 +
956 +/**
957 + * Update Item Option by ajax.
958 + * Ajax response when Option information is updated in the Option block of the product registration screen.
959 + *
960 + * @since 2.3.3
961 + * @param string $post_ID Post ID.
962 + * @return Query result.
963 + */
964 +function up_item_option_meta( $post_ID ) {
965 + global $usces;
966 +
967 + $post_id = (int) $post_ID;
968 + $value = array();
969 +
970 + $args = array(
971 + 'optmetaid' => FILTER_VALIDATE_INT,
972 + 'optcode' => FILTER_DEFAULT,
973 + 'optname' => FILTER_DEFAULT,
974 + 'optmeans' => FILTER_VALIDATE_INT,
975 + 'optessential' => FILTER_VALIDATE_INT,
976 + 'sort' => FILTER_VALIDATE_INT,
977 + 'optvalue' => FILTER_DEFAULT,
978 + );
979 +
980 + $inputs = filter_input_array( INPUT_POST, $args );
981 +
982 + $optmetaid = ( null !== $inputs['optmetaid'] ) ? (int) $inputs['optmetaid'] : '';
983 + $optcode = ( null !== $inputs['optcode'] ) ? $inputs['optcode'] : '';
984 + $optname = ( null !== $inputs['optname'] ) ? $inputs['optname'] : '';
985 + $optmeans = ( null !== $inputs['optmeans'] ) ? (int) $inputs['optmeans'] : 0;
986 + $optessential = ( null !== $inputs['optessential'] ) ? $inputs['optessential'] : 0;
987 + $optsort = ( null !== $inputs['sort'] ) ? $inputs['sort'] : 0;
988 + $optvalue = ( null !== $inputs['optvalue'] ) ? trim( $inputs['optvalue'] ) : '';
989 +
990 + $opts = wel_get_opts( $post_id, 'meta_id', false );
991 + $opt = $opts[ $optmetaid ];
992 +
993 + $opt['meta_id'] = $optmetaid;
994 + $opt['code'] = $optcode;
995 + $opt['name'] = str_replace( "\\", '', $optname );
996 + $opt['means'] = $optmeans;
997 + $opt['essential'] = $optessential;
998 + $opt['value'] = str_replace( "\\", '', $optvalue );
999 + $opt['sort'] = $optsort;
1000 +
1001 + $value = $usces->stripslashes_deep_post( $opt );
1002 +
1003 + $res = wel_update_opt_data_by_id( $optmetaid, $post_id, $value );
1004 +
1005 + return $res;
1006 +}
1007 +
1008 +/**
1009 + * Delete item option meta
1010 + *
1011 + * @param int $post_id Post ID.
1012 + */
1013 +function del_item_option_meta( $post_id ) {
1014 +
1015 + $optmetaid = filter_input( INPUT_POST, 'optmetaid', FILTER_VALIDATE_INT );
1016 + $meta_id = ( null !== $optmetaid ) ? $optmetaid : '';
1017 +
1018 + wel_delete_opt_data_by_id( $meta_id, $post_id );
1019 +
1020 + $opts = wel_get_opts( $post_id, 'sort', false );
1021 + if ( ! empty( $opts ) ) {
1022 + $i = 0;
1023 + foreach ( $opts as $opt ) {
1024 + $opt['sort'] = $i;
1025 + $meta_id = $opt['meta_id'];
1026 + wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
1027 + $i++;
1028 + }
1029 + } else {
1030 + return;
1031 + }
1032 +}
1033 +
1034 +/**
1035 + * Select common option
1036 + *
1037 + * @param int $post_id Post ID.
1038 + * @return array
1039 + */
1040 +function select_common_option( $post_id ) {
1041 +
1042 + $meta_id = filter_input( INPUT_POST, 'meta_id', FILTER_VALIDATE_INT );
1043 + if ( ! $meta_id ) {
1044 + return array();
1045 + }
1046 +
1047 + $opts = wel_get_opts( $post_id, 'meta_id', false );
1048 + $opt = $opts[ $meta_id ];
1049 + $res = array(
1050 + 'means' => $opt['means'],
1051 + 'essential' => $opt['essential'],
1052 + 'value' => $opt['value'],
1053 + );
1054 + return $res;
1055 +}
1056 +
1057 +/**
1058 + * Order item2cart ajax
1059 + */
1060 +function order_item2cart_ajax() {
1061 + global $usces;
1062 +
1063 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1064 +
1065 + if ( 'order_item2cart_ajax' !== $_POST['action'] ) {
1066 + die(0);
1067 + }
1068 +
1069 + $_POST = $usces->stripslashes_deep_post( $_POST );
1070 + $logger = Logger::start( $_POST['order_id'], 'orderedit', 'update' );
1071 + $order_id = usces_add_ordercartdata();
1072 + if ( ! $order_id ) {
1073 + die( 0 );
1074 + }
1075 +
1076 + $cart = usces_get_ordercartdata( $order_id );
1077 + $return = usces_get_ordercart_row( $order_id, $cart );
1078 + $logger->flush();
1079 + die( $return );
1080 +}
1081 +
1082 +/**
1083 + * Order item ajax
1084 + */
1085 +function order_item_ajax() {
1086 + global $usces;
1087 +
1088 + if ( ! current_user_can( 'wel_manage_order' ) ) {
1089 + wp_die( __( 'You do not have sufficient privileges to perform this operation.', 'usces' ) );
1090 + }
1091 +
1092 + if ( 'order_item_ajax' !== wp_unslash( $_POST['action'] ) ) {
1093 + die(0);
1094 + }
1095 +
1096 + $res = false;
1097 + $_POST = $usces->stripslashes_deep_post( $_POST );
1098 + $mode = sanitize_text_field( wp_unslash( $_POST['mode'] ) );
1099 +
1100 + switch ( $mode ) {
1101 + case 'completionMail':
1102 + case 'orderConfirmMail':
1103 + case 'changeConfirmMail':
1104 + case 'receiptConfirmMail':
1105 + case 'mitumoriConfirmMail':
1106 + case 'cancelConfirmMail':
1107 + case 'otherConfirmMail':
1108 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1109 + $order_id = (int) wp_unslash( $_POST['order_id'] );
1110 + $res = usces_order_confirm_message( $order_id );
1111 + break;
1112 + case 'sendmail':
1113 + // Nonce('wc_send_mail_order_nonce') verification is executed in the usces_ajax_send_mail function.
1114 + $res = usces_ajax_send_mail();
1115 + break;
1116 + case 'get_order_item':
1117 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1118 + $itemcode = sanitize_text_field( wp_unslash( $_POST['itemcode'] ) );
1119 + $res = get_order_item( $itemcode );
1120 + break;
1121 + case 'get_item_select_option':
1122 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1123 + $cat_id = (int) wp_unslash( $_POST['cat_id'] );
1124 + $res = usces_get_item_select_option( $cat_id );
1125 + break;
1126 + case 'ordercheckpost':
1127 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1128 + $res = usces_update_ordercheck();
1129 + break;
1130 + case 'getmember':
1131 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1132 + $res = usces_get_member_neworder();
1133 + break;
1134 + case 'recalculation':
1135 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1136 + $change_taxrate = ( isset( $_POST['change_taxrate'] ) ) ? sanitize_text_field( wp_unslash( $_POST['change_taxrate'] ) ) : '';
1137 + $order_id = (int) wp_unslash( $_POST['order_id'] );
1138 + $mem_id = (int) wp_unslash( $_POST['mem_id'] );
1139 + $res = usces_order_recalculation( $order_id, $mem_id, $_POST['post_ids'], $_POST['prices'], $_POST['quants'], $_POST['cart_ids'], $_POST['upoint'], $_POST['shipping_charge'], $_POST['cod_fee'], $_POST['discount'], $change_taxrate );
1140 + break;
1141 + case 'recalculation_reduced':
1142 + check_ajax_referer( 'order_edit', 'wc_nonce' );
1143 + $change_taxrate = ( isset( $_POST['change_taxrate'] ) ) ? sanitize_text_field( wp_unslash( $_POST['change_taxrate'] ) ) : '';
1144 + $order_id = (int) wp_unslash( $_POST['order_id'] );
1145 + $mem_id = (int) wp_unslash( $_POST['mem_id'] );
1146 + $res = usces_order_recalculation_reduced( $order_id, $mem_id, $_POST['post_ids'], $_POST['prices'], $_POST['quants'], $_POST['cart_ids'], $_POST['upoint'], $_POST['shipping_charge'], $_POST['cod_fee'], $_POST['discount_standard'], $_POST['discount_reduced'], $change_taxrate );
1147 + break;
1148 + case 'get_settlement_log':
1149 + check_ajax_referer( 'order_list', 'wc_nonce' );
1150 + $res = usces_get_settlement_log();
1151 + break;
1152 + case 'get_settlement_log_detail':
1153 + check_ajax_referer( 'order_list', 'wc_nonce' );
1154 + $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1155 + $res = usces_get_settlement_log_detail( $log_key );
1156 + break;
1157 + case 'search_settlement_log':
1158 + check_ajax_referer( 'order_list', 'wc_nonce' );
1159 + $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1160 + $res = usces_get_settlement_log( $log_key );
1161 + break;
1162 + case 'delete_settlement_log':
1163 + check_ajax_referer( 'order_list', 'wc_nonce' );
1164 + $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1165 + usces_delete_settlement_log( $log_key );
1166 + $res = usces_get_settlement_log();
1167 + break;
1168 + case 'delete_settlement_log_all':
1169 + check_ajax_referer( 'order_list', 'wc_nonce' );
1170 + usces_delete_settlement_log();
1171 + $res = usces_get_settlement_log();
1172 + break;
1173 + case 'revival_order_data':
1174 + check_ajax_referer( 'order_list', 'wc_nonce' );
1175 + $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1176 + $register_date = sanitize_text_field( wp_unslash( $_POST['register_date'] ) );
1177 + $res = usces_revival_order_data( $log_key, $register_date );
1178 + break;
1179 + case 'get_settlement_error_log':
1180 + check_ajax_referer( 'order_list', 'wc_nonce' );
1181 + $res = usces_get_settlement_error_log();
1182 + break;
1183 + case 'get_settlement_error_log_detail':
1184 + check_ajax_referer( 'order_list', 'wc_nonce' );
1185 + $log_id = sanitize_text_field( wp_unslash( $_POST['log_id'] ) );
1186 + $res = usces_get_settlement_error_log_detail( $log_id );
1187 + break;
1188 + case 'delete_settlement_error_log':
1189 + check_ajax_referer( 'order_list', 'wc_nonce' );
1190 + $log_id = sanitize_text_field( wp_unslash( $_POST['log_id'] ) );
1191 + usces_delete_settlement_error_log( $log_id );
1192 + $res = usces_get_settlement_error_log();
1193 + break;
1194 + case 'delete_settlement_error_log_all':
1195 + check_ajax_referer( 'order_list', 'wc_nonce' );
1196 + usces_delete_settlement_error_log();
1197 + $res = usces_get_settlement_error_log();
1198 + break;
1199 + case 'reset_settlement_notice':
1200 + check_ajax_referer( 'order_list', 'wc_nonce' );
1201 + $res = usces_reset_settlement_notice();
1202 + break;
1203 + }
1204 +
1205 + $res = wel_esc_script( apply_filters( 'usces_filter_order_item_ajax', $res ) );
1206 +
1207 + if ( false === $res ) {
1208 + die(0);
1209 + }
1210 +
1211 + die( $res );
1212 +}
1213 +
1214 +/**
1215 + * Item select field
1216 + *
1217 + * @param int $cat_id Category ID.
1218 + * @return string
1219 + */
1220 +function usces_get_item_select_option( $cat_id ) {
1221 + global $usces;
1222 +
1223 + $number = apply_filters( 'usces_filter_item_select_numberposts', 50, $cat_id );
1224 + $args = array(
1225 + 'category' => $cat_id,
1226 + 'numberposts' => $number,
1227 + 'post_status' => array( 'publish', 'private' ),
1228 + );
1229 + $args = apply_filters( 'usces_filter_item_select_queryargs', $args, $cat_id );
1230 +
1231 + $items = get_posts( $args );
1232 + $option = '<option value="-1">' . __( 'Please select an item', 'usces' ) . '</option>' . "\n";
1233 + foreach ( (array) $items as $item ) {
1234 + $product = wel_get_product( $item->ID );
1235 + $item_name = $product['itemName'];
1236 + $item_code = $product['itemCode'];
1237 + $option .= '<option value="' . urlencode( $item_code ) . '">' . esc_html( $item_name ) . '(' . esc_html( $item_code ) . ')</option>' . "\n";
1238 + }
1239 + return $option;
1240 +}
1241 +
1242 +/**
1243 + * Order Item html
1244 + */
1245 +function usces_get_member_neworder() {
1246 + global $wpdb;
1247 +
1248 + $wpdb->show_errors();
1249 + $res = '';
1250 + $member_table = usces_get_tablename( 'usces_member' );
1251 + $query = $wpdb->prepare( "SELECT * FROM $member_table WHERE mem_email = %s", trim( $_POST['email'] ) );
1252 + $value = $wpdb->get_row( $query, ARRAY_A );
1253 +
1254 + if ( ! $value ) {
1255 + $response = array( 'status_code' => 'none' );
1256 + wp_send_json( $response );
1257 + }
1258 +
1259 + $response = array(
1260 + 'status_code' => 'ok',
1261 + 'member_id' => $value['ID'],
1262 + 'customer[name1]' => $value['mem_name1'],
1263 + 'customer[name2]' => $value['mem_name2'],
1264 + 'customer[name3]' => $value['mem_name3'],
1265 + 'customer[name4]' => $value['mem_name4'],
1266 + 'customer[zipcode]' => $value['mem_zip'],
1267 + 'customer[pref]' => $value['mem_pref'],
1268 + 'customer[address1]' => $value['mem_address1'],
1269 + 'customer[address2]' => $value['mem_address2'],
1270 + 'customer[address3]' => $value['mem_address3'],
1271 + 'customer[tel]' => $value['mem_tel'],
1272 + 'customer[fax]' => $value['mem_fax'],
1273 + 'delivery[name1]' => $value['mem_name1'],
1274 + 'delivery[name2]' => $value['mem_name2'],
1275 + 'delivery[name3]' => $value['mem_name3'],
1276 + 'delivery[name4]' => $value['mem_name4'],
1277 + 'delivery[zipcode]' => $value['mem_zip'],
1278 + 'delivery[pref]' => $value['mem_pref'],
1279 + 'delivery[address1]' => $value['mem_address1'],
1280 + 'delivery[address2]' => $value['mem_address2'],
1281 + 'delivery[address3]' => $value['mem_address3'],
1282 + 'delivery[tel]' => $value['mem_tel'],
1283 + 'delivery[fax]' => $value['mem_fax'],
1284 + );
1285 +
1286 + $member_metetable = usces_get_tablename( 'usces_member_meta' );
1287 + $query = $wpdb->prepare( "SELECT * FROM $member_metetable WHERE meta_key LIKE %s AND member_id = %d", 'csmb_%', $value['ID'] );
1288 + $customs = $wpdb->get_results( $query, ARRAY_A );
1289 + if ( ! empty( $customs ) ) {
1290 + foreach ( $customs as $cusv ) {
1291 + $response[ 'custom_customer[' . substr( $cusv['meta_key'], 5 ) . ']'] = $cusv['meta_value'];
1292 + $response[ 'custom_delivery[' . substr( $cusv['meta_key'], 5 ) . ']'] = $cusv['meta_value'];
1293 + }
1294 + }
1295 +
1296 + $response = apply_filters( 'usces_filter_get_member_neworder', $response );
1297 +
1298 + wp_send_json( $response );
1299 +}
1300 +
1301 +/**
1302 + * Add to order cart
1303 + *
1304 + * @return mixed
1305 + */
1306 +function usces_add_ordercartdata() {
1307 + global $usces, $wpdb;
1308 +
1309 + $res = 0;
1310 +
1311 + $order_id = (int) $_POST['order_id'];
1312 + if ( ! $order_id ) {
1313 + return $res;
1314 + }
1315 +
1316 + $post_id = (int) $_POST['post_id'];
1317 + $sku_code = urldecode( $_POST['sku'] );
1318 + $quantity = 1;
1319 +
1320 + $current_cart = usces_get_ordercartdata( $order_id );
1321 + $temp_arr = array();
1322 + foreach ( $current_cart as $cv ) {
1323 + $temp_arr[] = $cv['row_index'];
1324 + }
1325 + $row_index = ( 0 < count( $temp_arr ) ) ? max( $temp_arr ) + 1 : 0;
1326 +
1327 + $cart_table = $wpdb->prefix . 'usces_ordercart';
1328 + $cart_meta_table = $wpdb->prefix . 'usces_ordercart_meta';
1329 +
1330 + $product = wel_get_product( $post_id );
1331 + $item_name = $product['itemName'];
1332 + $item_code = $product['itemCode'];
1333 + $skus = $usces->get_skus( $post_id, 'code' );
1334 + $sku = apply_filters( 'usces_filter_add_ordercart_sku', $skus[ $sku_code ], $_POST );
1335 + $tax = 0;
1336 +
1337 + $query = $wpdb->prepare(
1338 + "INSERT INTO $cart_table
1339 + (
1340 + order_id, row_index, post_id, item_code, item_name,
1341 + sku_code, sku_name, cprice, price, quantity,
1342 + unit, tax, destination_id, cart_serial
1343 + ) VALUES (
1344 + %d, %d, %d, %s, %s,
1345 + %s, %s, %f, %f, %d,
1346 + %s, %d, %d, %s
1347 + )",
1348 + $order_id, $row_index, $post_id, $item_code, $item_name,
1349 + $sku_code, $sku['name'], $sku['cprice'], $sku['price'], $quantity,
1350 + $sku['unit'], $tax, NULL, NULL
1351 + );
1352 + $res = $wpdb->query( $query );
1353 + $cart_id = NULL;
1354 + if ( false !== $res ) {
1355 + $cart_id = $wpdb->insert_id;
1356 +
1357 + $item_opts = usces_get_opts( $post_id, 'name' );
1358 + foreach ( $item_opts as $key => $iopts ) {
1359 + if ( 3 === (int) $iopts['means'] || 4 === (int) $iopts['means'] ) {
1360 + // POSTが無ければNULLで追加.
1361 + if ( ! isset( $_POST['itemOption'][ $key ] ) ) {
1362 + $query = $wpdb->prepare(
1363 + "INSERT INTO $cart_meta_table
1364 + ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, %s, %s, %s )",
1365 + $cart_id, 'option', $iopts['name'], NULL
1366 + );
1367 + $wpdb->query( $query );
1368 + }
1369 + }
1370 + }
1371 +
1372 + if ( isset( $_POST['itemOption'] ) ) {
1373 + foreach ( (array) $_POST['itemOption'] as $okey => $ovalue ) {
1374 + $means = $item_opts[ $okey ]['means'];
1375 + if ( is_array( $ovalue ) ) {
1376 + $temp = array();
1377 + if ( 4 === (int) $means ) {
1378 + foreach ( $ovalue as $k => $v ) {
1379 + $temp[] = $v;
1380 + }
1381 + } else {
1382 + foreach ( $ovalue as $k => $v ) {
1383 + $temp[ urlencode( $k ) ] = $v;
1384 + }
1385 + }
1386 + $ovalue = serialize( $temp );
1387 + } else {
1388 + $ovalue = $ovalue;
1389 + }
1390 + $aquery = $wpdb->prepare(
1391 + "INSERT INTO $cart_meta_table
1392 + ( cart_id, meta_type, meta_key, meta_value ) VALUES (%d, %s, %s, %s)",
1393 + $cart_id, 'option', $okey, $ovalue
1394 + );
1395 + $wpdb->query( $aquery );
1396 + }
1397 + }
1398 +
1399 + if ( $usces->is_reduced_taxrate() ) {
1400 + if ( isset( $sku['taxrate'] ) && 'reduced' === $sku['taxrate'] ) {
1401 + $tkey = 'reduced';
1402 + $tvalue = $usces->options['tax_rate_reduced'];
1403 + } else {
1404 + $tkey = 'standard';
1405 + $tvalue = $usces->options['tax_rate'];
1406 + }
1407 + $tquery = $wpdb->prepare(
1408 + "INSERT INTO $cart_meta_table
1409 + ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'taxrate', %s, %s )",
1410 + $cart_id, $tkey, $tvalue
1411 + );
1412 + $wpdb->query( $tquery );
1413 + }
1414 + }
1415 +
1416 + $res = apply_filters( 'usces_filter_add_ordercart', $res, $order_id, $cart_id );
1417 +
1418 + if ( $res ) {
1419 + return $order_id;
1420 + } else {
1421 + return $res;
1422 + }
1423 +}
1424 +
1425 +/**
1426 + * Get order item
1427 + *
1428 + * @param string $item_code Item code.
1429 + * @return string
1430 + */
1431 +function get_order_item( $item_code ) {
1432 + global $usces, $post;
1433 +
1434 + $post_id = wel_get_id_by_item_code( $item_code );
1435 + if ( null === $post_id ) {
1436 + return false;
1437 + }
1438 + $product = wel_get_product( $post_id );
1439 + if ( null === $product ) {
1440 + return false;
1441 + }
1442 + $post = $product['_pst'];
1443 + $res = apply_filters( 'usce_action_ajax_get_order_item', false, $post );
1444 + if ( false !== $res ) {
1445 + return $res;
1446 + }
1447 +
1448 + $pict_id = wel_get_main_pict_id_by_code( $item_code );
1449 + $pict_link = wp_get_attachment_image( $pict_id, array( 150, 150 ), true );
1450 + preg_match( "/^\<a .+\>(\<img .+\/\>)\<\/a\>$/", $pict_link, $match );
1451 + $pict = isset( $match[1] ) ? $match[1] : '';
1452 + $skus = $product['_sku'];
1453 + $optkeys = $usces->get_itemOptionKey( $post_id );
1454 + $item_name = esc_html( $product['itemName'] );
1455 +
1456 + $r = '';
1457 + $r .= $pict_link . "\n";
1458 + $r .= '<h3>' . $item_name . "</h3>\n";
1459 + $r .= "<div class=\"skuform\">\n";
1460 +
1461 + $r .= "<table class=\"skumulti\">\n";
1462 + $r .= "<thead>\n";
1463 + $r .= "<tr>\n";
1464 + $r .= '<th>' . __( 'SKU code', 'usces' ) . "</th>\n";
1465 + $r .= '<th>' . __( 'SKU display name ', 'usces' ) . "</th>\n";
1466 +
1467 + $usces_listprice = __( 'List price', 'usces' ) . usces_guid_tax( 'return' );
1468 + $r .= '<th>' . apply_filters( 'usces_filter_listprice_label', $usces_listprice, __( 'List price', 'usces' ), usces_guid_tax( 'return' ) ) . "</th>\n";
1469 +
1470 + $usces_sellingprice = __( 'Sale price', 'usces' ) . usces_guid_tax( 'return' );
1471 + $r .= '<th>' . apply_filters( 'usces_filter_sellingprice_label', $usces_sellingprice, __( 'Sale price', 'usces' ), usces_guid_tax( 'return' ) ) . "</th>\n";
1472 +
1473 + $r .= '<th>' . __( 'stock status', 'usces' ) . "</th>\n";
1474 + $r .= '<th>' . __( 'stock', 'usces' ) . "</th>\n";
1475 + $r .= '<th>' . __( 'unit', 'usces' ) . "</th>\n";
1476 + $r .= "<th>&nbsp;</th>\n";
1477 + $r .= "</tr>\n";
1478 + $r .= "</thead>\n";
1479 + $r .= "<tbody>\n";
1480 + foreach ( $skus as $sku ) :
1481 + $key = urlencode( $sku['code'] );
1482 + $cprice = esc_attr( $sku['cprice'] );
1483 + $price = esc_attr( $sku['price'] );
1484 + $zaiko = esc_attr( $usces->zaiko_status[ $sku['stock'] ] );
1485 + $zaikonum = esc_attr( $sku['stocknum'] );
1486 + $disp = esc_attr( $sku['name'] );
1487 + $unit = esc_attr( $sku['unit'] );
1488 + $gptekiyo = $sku['gp'];
1489 + $sort = (int) $sku['sort'];
1490 +
1491 + $r .= "<tr>\n";
1492 + $r .= '<td rowspan="2">' . esc_js( $sku['code'] ) . "</td>\n";
1493 + $r .= '<td>' . $disp . "</td>\n";
1494 + $r .= '<td><span class="cprice">' . ( ( ! empty( $cprice ) ) ? usces_crform( $cprice, true, false, 'return' ) : '' ) . "</span></td>\n";
1495 + $r .= '<td><span class="price">' . usces_crform( $price, true, false, 'return' ) . "</span></td>\n";
1496 + $r .= '<td>' . $zaiko . "</td>\n";
1497 + $r .= '<td>' . $zaikonum . "</td>\n";
1498 + $r .= '<td>' . $unit . "</td>\n";
1499 + $r .= "<td>\n";
1500 + $r .= "<input name=\"itemNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWName[{$post_id}][{$key}]\" value=\"{$item_name}\" />\n";
1501 + $r .= "<input name=\"itemNEWCode[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWCode[{$post_id}][{$key}]\" value=\"{$item_code}\" />\n";
1502 + $r .= "<input name=\"skuNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWName[{$post_id}][{$key}]\" value=\"{$key}\" />\n";
1503 + $r .= "<input name=\"skuNEWCprice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWCprice[{$post_id}][{$key}]\" value=\"{$cprice}\" />\n";
1504 + $r .= "<input name=\"skuNEWDisp[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWDisp[{$post_id}][{$key}]\" value=\"{$disp}\" />\n";
1505 + $r .= "<input name=\"zaikoNEWnum[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaikoNEWnum[{$post_id}][{$key}]\" value=\"{$zaikonum}\" />\n";
1506 + $r .= "<input name=\"zaiNEWko[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaiNEWko[{$post_id}][{$key}]\" value=\"{$zaiko}\" />\n";
1507 + $r .= "<input name=\"uniNEWt[{$post_id}][{$key}]\" type=\"hidden\" id=\"uniNEWt[{$post_id}][{$key}]\" value=\"{$unit}\" />\n";
1508 + $r .= "<input name=\"gpNEWtekiyo[{$post_id}][{$key}]\" type=\"hidden\" id=\"gpNEWtekiyo[{$post_id}][{$key}]\" value=\"{$gptekiyo}\" />\n";
1509 + $r .= "<input name=\"skuNEWPrice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWPrice[{$post_id}][{$key}]\" value=\"{$price}\" />\n";
1510 + $r .= "<input name=\"inNEWCart[{$post_id}][{$key}]\" type=\"button\" id=\"inNEWCart[{$post_id}][{$key}]\" class=\"skubutton button\" value=\"" . __( 'Add to Whish List', 'usces' ) . "\" onclick=\"orderItem.add2cart('{$post_id}', '{$key}');\" />";
1511 + $r .= "</td>\n";
1512 + $r .= "</tr>\n";
1513 + $r .= "<tr>\n";
1514 + if ( $optkeys ) :
1515 + $r .= "<td colspan=\"7\">\n";
1516 + foreach ( $optkeys as $optkey => $optvalue ) :
1517 + $r .= "<div>\n";
1518 +
1519 + $name = esc_attr( $optvalue );
1520 + $optcode = urlencode( $name );
1521 + $opts = usces_get_opts( $post_id, 'name' );
1522 + $opt = $opts[ $optvalue ];
1523 + $opt['value'] = usces_change_line_break( $opt['value'] );
1524 + $means = (int) $opt['means'];
1525 + $essential = (int) $opt['essential'];
1526 +
1527 + $r .= "\n<label for=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_label\">{$name}</label>\n";
1528 + switch ( $means ) {
1529 + case 0: // Single-select.
1530 + case 1: // Multi-select.
1531 + $selects = explode( "\n", $opt['value'] );
1532 + $multiple = ( 0 === $means ) ? '' : ' multiple';
1533 + $multiple_array = ( 0 === $means ) ? '' : '_multiple';
1534 + $r .= "\n<select name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_select{$multiple_array}\"{$multiple}>\n";
1535 + if ( 1 === $essential ) {
1536 + $r .= "\t<option value=\"#NONE#\" selected=\"selected\">" . __( 'Choose', 'usces' ) . "</option>\n";
1537 + }
1538 + $s = 0;
1539 + foreach ( $selects as $v ) {
1540 + $v = trim( $v );
1541 + if ( 0 === $s && 0 === $essential ) {
1542 + $selected = ' selected="selected"';
1543 + } else {
1544 + $selected = '';
1545 + }
1546 + $r .= "\t<option value=\"{$v}\"{$selected}>{$v}</option>\n";
1547 + $s++;
1548 + }
1549 + $r .= "</select>\n";
1550 + break;
1551 + case 2: // Text.
1552 + $r .= "\n<input name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" type=\"text\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_text\" onKeyDown=\"if (event.keyCode == 13) {return false;}\" value=\"\" />\n";
1553 + break;
1554 + case 3: // Radio-button.
1555 + $selects = explode( "\n", $opt['value'] );
1556 + $i = 0;
1557 + foreach ( $selects as $v ) {
1558 + $r .= '<label for="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_radio_label"><input name="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']" type="radio" id="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_radio" value="' . urlencode($v) . '">' . esc_html($v) . "</label>\n";
1559 + $i++;
1560 + }
1561 + break;
1562 + case 4: // Check-box.
1563 + $selects = explode( "\n", $opt['value'] );
1564 + $i = 0;
1565 + foreach ( $selects as $v ) {
1566 + $r .= '<label for="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_checkbox_label"><input name="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']" type="checkbox" id="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_checkbox" value="' . urlencode($v) . '">' . esc_html($v) . "</label>\n";
1567 + $i++;
1568 + }
1569 + break;
1570 + case 5: // Text-area.
1571 + $r .= "\n<textarea name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_textarea\"></textarea>\n";
1572 + break;
1573 + }
1574 + $r .= "<input name=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" value=\"{$optcode}\" />\n";
1575 + $r .= "<input name=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" value=\"{$essential}\" />\n";
1576 + $r .= "</div>\n";
1577 + endforeach;
1578 + $r .= "</td>\n";
1579 + endif;
1580 + $r .= "</tr>\n";
1581 + endforeach;
1582 + $r .= "</tbody>\n";
1583 + $r .= "</table>\n";
1584 +
1585 + $r .= "</div>\n";
1586 +
1587 + $r = apply_filters( 'usces_filter_get_order_item', $r, $item_code, $post_id );
1588 +
1589 + return $r;
1590 +}
1591 +
1592 +/**
1593 + * Item option ajax
1594 + */
1595 +function item_option_ajax() {
1596 +
1597 + $args = array(
1598 + 'ID' => FILTER_VALIDATE_INT,
1599 + 'action' => FILTER_DEFAULT,
1600 + 'update' => FILTER_DEFAULT,
1601 + 'delete' => FILTER_DEFAULT,
1602 + 'meta' => FILTER_DEFAULT,
1603 + 'select' => FILTER_DEFAULT,
1604 + 'sort' => FILTER_VALIDATE_INT,
1605 + );
1606 +
1607 + $inputs = filter_input_array( INPUT_POST, $args );
1608 +
1609 + check_admin_referer( 'admin_setup', 'wc_nonce' );
1610 + if ( ! current_user_can( 'edit_posts' ) ) {
1611 + wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
1612 + }
1613 +
1614 + if ( 'item_option_ajax' !== $inputs['action'] || null === $inputs['ID'] ) {
1615 + die(0);
1616 + }
1617 +
1618 + $post_id = $inputs['ID'];
1619 +
1620 + if ( null !== $inputs['update'] ) {
1621 +
1622 + $id = up_item_option_meta( $post_id );
1623 +
1624 + } elseif ( null !== $inputs['delete'] ) {
1625 +
1626 + $id = del_item_option_meta( $post_id );
1627 +
1628 + } elseif ( null !== $inputs['select'] ) {
1629 +
1630 + $res = select_common_option( $post_id );
1631 + wp_send_json( $res );
1632 +
1633 + } elseif ( null !== $inputs['sort'] ) {
1634 +
1635 + $id = usces_sort_item_opts( $post_id, $inputs['meta'] );
1636 +
1637 + } else {
1638 +
1639 + $id = add_item_option_meta( $post_id );
1640 +
1641 + }
1642 +
1643 + $opts = usces_get_opts( $post_id, 'sort', false );
1644 +
1645 + $r = '';
1646 + foreach ( $opts as $opt ) {
1647 + $r .= _list_item_option_meta_row( $opt );
1648 + }
1649 +
1650 + $response = array(
1651 + 'meta_id' => $id,
1652 + 'meta_row' => $r,
1653 + );
1654 + wp_send_json( $response );
1655 +}
1656 +
1657 +/**
1658 + * Item SKU ajax
1659 + */
1660 +function item_sku_ajax() {
1661 + global $usces;
1662 +
1663 + $id = '';
1664 + if ( 'item_sku_ajax' !== $_POST['action'] ) {
1665 + die(0);
1666 + }
1667 +
1668 + $post_id = (int) $_POST['ID'];
1669 + $msg = '';
1670 +
1671 + if ( ! current_user_can( 'wel_publish_products', $post_id ) ) {
1672 + $response = array(
1673 + 'meta_id' => 0,
1674 + 'meta_row' => 0,
1675 + 'meta_msg' => __( 'You are not allowed to edit this item.', 'usces' ),
1676 + );
1677 + wp_send_json( $response );
1678 + }
1679 +
1680 + if ( isset( $_POST['update'] ) ) {
1681 + check_ajax_referer( 'admin_setup', 'wc_nonce' );
1682 + $id = up_item_sku_meta( $post_id );
1683 +
1684 + } elseif ( isset( $_POST['delete'] ) ) {
1685 + check_ajax_referer( 'admin_setup', 'wc_nonce' );
1686 + $id = del_item_sku_meta( $post_id );
1687 +
1688 + } elseif ( isset( $_POST['select'] ) ) {
1689 + check_ajax_referer( 'admin_setup', 'wc_nonce' );
1690 + $response = select_item_sku( $post_id );
1691 + wp_send_json( $response );
1692 +
1693 + } elseif ( isset( $_POST['sort'] ) ) {
1694 + check_ajax_referer( 'admin_setup', 'wc_nonce' );
1695 + $id = usces_sort_item_skus( $post_id, $_POST['meta'] );
1696 +
1697 + } else {
1698 + check_ajax_referer( 'admin_setup', 'wc_nonce' );
1699 + $id = add_item_sku_meta( $post_id );
1700 +
1701 + }
1702 + $msg .= apply_filters( 'usces_filter_item_sku_message', $msg, $id, $post_id );
1703 + // $skus = $usces->get_skus( $post_id );
1704 + $skus = wel_get_skus( $post_id, 'sort', false );
1705 +
1706 + $r = '';
1707 +
1708 + foreach ( (array) $skus as $sku ) {
1709 + $r .= _list_item_sku_meta_row( $sku );
1710 + }
1711 + $response = array(
1712 + 'meta_id' => $id,
1713 + 'meta_row' => $r,
1714 + 'meta_msg' => $msg,
1715 + );
1716 +
1717 + wp_send_json( $response );
1718 +}
1719 +
1720 +/**
1721 + * Item save metadata
1722 + *
1723 + * @param int $post_id Post ID.
1724 + * @param object $post Post data.
1725 + */
1726 +function item_save_metadata( $post_id, $post ) {
1727 + global $usces, $wpdb;
1728 +
1729 + $message = '';
1730 + $item_data = wel_get_item( $post_id, false );
1731 + $args = array(
1732 + 'usces_nonce' => FILTER_SANITIZE_SPECIAL_CHARS,
1733 + 'itemCode' => FILTER_SANITIZE_SPECIAL_CHARS,
1734 + 'itemName' => FILTER_DEFAULT,
1735 + 'itemRestriction' => FILTER_SANITIZE_SPECIAL_CHARS,
1736 + 'itemPointrate' => FILTER_VALIDATE_INT,
1737 + 'itemGpNum1' => FILTER_VALIDATE_INT,
1738 + 'itemGpNum2' => FILTER_VALIDATE_INT,
1739 + 'itemGpNum3' => FILTER_VALIDATE_INT,
1740 + 'itemGpDis1' => FILTER_VALIDATE_INT,
1741 + 'itemGpDis2' => FILTER_VALIDATE_INT,
1742 + 'itemGpDis3' => FILTER_VALIDATE_INT,
1743 + 'itemOrderAcceptable' => FILTER_DEFAULT,
1744 + 'itemShipping' => FILTER_VALIDATE_INT,
1745 + 'itemDeliveryMethod' => array(
1746 + 'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
1747 + 'flags' => FILTER_REQUIRE_ARRAY,
1748 + ),
1749 + 'itemShippingCharge' => FILTER_VALIDATE_INT,
1750 + 'itemIndividualSCharge' => FILTER_DEFAULT,
1751 + 'item_charging_type' => FILTER_VALIDATE_INT,
1752 + 'item_division' => FILTER_SANITIZE_SPECIAL_CHARS,
1753 + 'dlseller_date' => FILTER_SANITIZE_SPECIAL_CHARS,
1754 + 'dlseller_file' => FILTER_SANITIZE_SPECIAL_CHARS,
1755 + 'dlseller_interval' => FILTER_VALIDATE_INT,
1756 + 'dlseller_validity' => FILTER_VALIDATE_INT,
1757 + 'dlseller_version' => FILTER_SANITIZE_SPECIAL_CHARS,
1758 + 'dlseller_author' => FILTER_SANITIZE_SPECIAL_CHARS,
1759 + 'dlseller_purchases' => FILTER_VALIDATE_INT,
1760 + 'dlseller_downloads' => FILTER_VALIDATE_INT,
1761 + 'item_chargingday' => FILTER_DEFAULT,
1762 + 'item_frequency' => FILTER_DEFAULT,
1763 + 'wcad_regular_unit' => FILTER_DEFAULT,
1764 + 'wcad_regular_interval' => FILTER_DEFAULT,
1765 + 'wcad_regular_frequency' => FILTER_DEFAULT,
1766 + 'select_sku_switch' => FILTER_VALIDATE_INT,
1767 + 'select_sku_display' => FILTER_VALIDATE_INT,
1768 + 'select_sku' => array(
1769 + 'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
1770 + 'flags' => FILTER_REQUIRE_ARRAY,
1771 + ),
1772 + 'deferred_payment_propriety' => FILTER_VALIDATE_INT,
1773 + 'atodene_propriety' => FILTER_VALIDATE_INT,
1774 + 'structuredDataSku' => FILTER_SANITIZE_SPECIAL_CHARS,
1775 + 'lower_limit' => FILTER_SANITIZE_SPECIAL_CHARS,
1776 + 'popularity' => FILTER_SANITIZE_SPECIAL_CHARS,
1777 + 'main_price' => FILTER_SANITIZE_SPECIAL_CHARS,
1778 + 'itemPicts' => FILTER_DEFAULT,
1779 + 'itemAdvanced' => array(
1780 + 'filter' => FILTER_DEFAULT,
1781 + 'flags' => FILTER_REQUIRE_ARRAY,
1782 + ),
1783 + 'itemsku' => array(
1784 + 'filter' => FILTER_DEFAULT,
1785 + 'flags' => FILTER_REQUIRE_ARRAY,
1786 + ),
1787 + 'itemopt' => array(
1788 + 'filter' => FILTER_DEFAULT,
1789 + 'flags' => FILTER_REQUIRE_ARRAY,
1790 + ),
1791 + );
1792 +
1793 + $inputs = filter_input_array( INPUT_POST, $args );
1794 + $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS );
1795 + // Permission check.
1796 + if ( 'usces_itemedit' === $page || 'usces_itemnew' === $page ) {
1797 + if ( ! current_user_can( 'edit_post', $post_id ) ) {
1798 + $usces->set_action_status( 'error', 'ERROR : ' . __( 'Sorry, you do not have the right to edit this post.' ) );
1799 + return $post_id;
1800 + }
1801 +
1802 + // Do nothing when saving auto-draft.
1803 + if ( isset( $post->post_status ) && 'auto-draft' === $post->post_status ) {
1804 + return $post_id;
1805 + }
1806 + $usces->set_item_mime( $post_id, 'item' );
1807 +
1808 + } else {
1809 +
1810 + return $post_id;
1811 +
1812 + }
1813 +
1814 + if ( ! isset( $inputs['usces_nonce'] ) || ! wp_verify_nonce( $inputs['usces_nonce'], 'usc-e-shop' ) ) {
1815 + return $post_id;
1816 + }
1817 +
1818 + // Check if it is an automatic save routine. If so, do not submit the form (do nothing).
1819 + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
1820 + return $post_id;
1821 + }
1822 +
1823 + // Do nothing when saving other plugins.
1824 + if ( isset( $post->post_type ) && 'post' !== $post->post_type ) {
1825 + return $post_id;
1826 + }
1827 +
1828 + if ( preg_match( '/[^0-9a-zA-Z\-_]/', $inputs['itemCode'] ) ) {
1829 + // Do nothing.
1830 + }
1831 +
1832 + if ( empty( $inputs['itemCode'] ) ) {
1833 + $inputs['itemCode'] = '';
1834 + $message .= __( 'Product code has not been entered.', 'usces' ) . '<br />';
1835 + } elseif ( $res = usces_is_same_itemcode( $post_id, $inputs['itemCode'] ) ) {
1836 + $message .= 'post_ID ';
1837 + foreach ( $res as $postid ) {
1838 + $message .= $postid . ', ';
1839 + }
1840 + $message .= __( 'Same product code is registered here.', 'usces' ) . '<br />';
1841 + $usces->set_action_status( 'error', 'ERROR : ' . $message );
1842 + }
1843 + $item_data['itemCode'] = $inputs['itemCode'];
1844 +
1845 + if ( null !== $inputs['itemName'] ) {
1846 + $item_data['itemName'] = trim( $inputs['itemName'] );
1847 + }
1848 + if ( null !== $inputs['itemRestriction'] ) {
1849 + $item_data['itemRestriction'] = trim( $inputs['itemRestriction'] );
1850 + }
1851 + if ( null !== $inputs['itemPointrate'] ) {
1852 + $item_data['itemPointrate'] = (int) $inputs['itemPointrate'];
1853 + }
1854 + if ( null !== $inputs['itemGpNum1'] ) {
1855 + $item_data['itemGpNum1'] = (int) $inputs['itemGpNum1'];
1856 + }
1857 + if ( null !== $inputs['itemGpNum2'] ) {
1858 + $item_data['itemGpNum2'] = (int) $inputs['itemGpNum2'];
1859 + }
1860 + if ( null !== $inputs['itemGpNum3'] ) {
1861 + $item_data['itemGpNum3'] = (int) $inputs['itemGpNum3'];
1862 + }
1863 + if ( null !== $inputs['itemGpDis1'] ) {
1864 + $item_data['itemGpDis1'] = (int) $inputs['itemGpDis1'];
1865 + }
1866 + if ( null !== $inputs['itemGpDis2'] ) {
1867 + $item_data['itemGpDis2'] = (int) $inputs['itemGpDis2'];
1868 + }
1869 + if ( null !== $inputs['itemGpDis3'] ) {
1870 + $item_data['itemGpDis3'] = (int) $inputs['itemGpDis3'];
1871 + }
1872 +
1873 + $item_data['itemOrderAcceptable'] = null !== $inputs['itemOrderAcceptable'] ? 1 : 0;
1874 +
1875 + if ( null !== $inputs['itemShipping'] ) {
1876 + $item_data['itemShipping'] = (int) $inputs['itemShipping'];
1877 + }
1878 + if ( null !== $inputs['itemDeliveryMethod'] ) {
1879 + $dmethod = array();
1880 + foreach ( (array) $inputs['itemDeliveryMethod'] as $dmid ) {
1881 + $dmethod[] = $dmid;
1882 + }
1883 + $item_data['itemDeliveryMethod'] = $dmethod;
1884 + }
1885 + if ( null !== $inputs['itemShippingCharge'] ) {
1886 + $item_data['itemShippingCharge'] = (int) $inputs['itemShippingCharge'];
1887 + }
1888 +
1889 + $item_data['itemIndividualSCharge'] = null !== $inputs['itemIndividualSCharge'] ? 1 : 0;
1890 +
1891 + $options = get_option( 'usces', array() );
1892 + $acting_settings = ( isset( $options['acting_settings'] ) ) ? $options['acting_settings'] : array();
1893 + if ( isset( $acting_settings['welcart'] ) && isset( $acting_settings['welcart']['atodene_byitem'] ) && 'on' === $acting_settings['welcart']['atodene_byitem'] ) {
1894 + $item_data['atodene_propriety'] = null !== $inputs['atodene_propriety'] ? (int) $inputs['atodene_propriety'] : 0;
1895 + }
1896 + $options_ex = get_option( 'usces_ex', array() );
1897 + $system_settings = ( isset( $options_ex['system'] ) ) ? $options_ex['system'] : array();
1898 + if ( isset( $system_settings['atobaraicsv'] ) && isset( $system_settings['atobaraicsv']['each_item'] ) && 1 === (int) $system_settings['atobaraicsv']['each_item'] ) {
1899 + $item_data['atobarai_propriety'] = null !== $inputs['deferred_payment_propriety'] ? (int) $inputs['deferred_payment_propriety'] : 0;
1900 + }
1901 +
1902 + if ( 1 === (int) USCES_STRUCTURED_DATA_PRODUCT::$opts['status'] ) {
1903 + $item_data['structuredDataSku'] = ( isset( $inputs['structuredDataSku'] ) ) ? trim( $inputs['structuredDataSku'] ) : '';
1904 + }
1905 +
1906 + wel_update_item_data( $item_data, $post_id, true );
1907 + $item_data = wel_get_item( $post_id, false );
1908 +
1909 + // SKU.
1910 + if ( null !== $inputs['itemsku'] ) {
1911 +
1912 + $meta_ids = array();
1913 + $codes = array();
1914 + $uniq_code = false;
1915 + $irreg_code = false;
1916 + $irreg_price = false;
1917 +
1918 + $skus = wel_get_skus( $post_id, 'meta_id', false );
1919 +
1920 + foreach ( $inputs['itemsku'] as $mid => $temp ) {
1921 + $meta_ids[] = $mid;
1922 + }
1923 + $meta_ids = array_unique( $meta_ids );
1924 + foreach ( $meta_ids as $meta_id ) {
1925 +
1926 + $skucode = isset( $inputs['itemsku'][ $meta_id ]['key'] ) ? trim( $inputs['itemsku'][ $meta_id ]['key'] ) : '';
1927 + $msgsku = apply_filters( 'usces_filter_before_item_save_metadata_sku', null, $post_id, $meta_id, $skucode );
1928 + if ( ! empty( $msgsku ) ) {
1929 + $message .= $msgsku;
1930 + continue;
1931 + }
1932 +
1933 + $skucprice = isset( $inputs['itemsku'][ $meta_id ]['cprice'] ) ? trim( $inputs['itemsku'][ $meta_id ]['cprice'] ) : 0;
1934 + $skuprice = isset( $inputs['itemsku'][ $meta_id ]['price'] ) ? trim( $inputs['itemsku'][ $meta_id ]['price'] ) : 0;
1935 + $skustocknum = isset( $inputs['itemsku'][ $meta_id ]['zaikonum'] ) ? trim( $inputs['itemsku'][ $meta_id ]['zaikonum'] ) : 0;
1936 + $skustock = isset( $inputs['itemsku'][ $meta_id ]['zaiko'] ) ? (int) $inputs['itemsku'][ $meta_id ]['zaiko'] : '';
1937 + $skuname = isset( $inputs['itemsku'][ $meta_id ]['skudisp'] ) ? trim( $inputs['itemsku'][ $meta_id ]['skudisp'] ) : '';
1938 + $skuunit = isset( $inputs['itemsku'][ $meta_id ]['skuunit'] ) ? trim( $inputs['itemsku'][ $meta_id ]['skuunit'] ) : '';
1939 + $skugp = isset( $inputs['itemsku'][ $meta_id ]['skugptekiyo'] ) ? (int) $inputs['itemsku'][ $meta_id ]['skugptekiyo'] : 0;
1940 + $skusort = isset( $inputs['itemsku'][ $meta_id ]['sort'] ) ? $inputs['itemsku'][ $meta_id ]['sort'] : 0;
1941 + $skutaxrate = isset( $inputs['itemsku'][ $meta_id ]['applicable_taxrate'] ) ? $inputs['itemsku'][ $meta_id ]['applicable_taxrate'] : '';
1942 +
1943 + $sku = $skus[ $meta_id ];
1944 +
1945 + $sku['code'] = $skucode;
1946 + $sku['name'] = $skuname;
1947 + $sku['cprice'] = $skucprice;
1948 + $sku['price'] = $skuprice;
1949 + $sku['unit'] = $skuunit;
1950 + $sku['stocknum'] = $skustocknum;
1951 + $sku['stock'] = $skustock;
1952 + $sku['gp'] = $skugp;
1953 + $sku['sort'] = $skusort;
1954 + if ( ! empty( $skutaxrate ) ) {
1955 + $sku['taxrate'] = $skutaxrate;
1956 + }
1957 + $sku = $usces->stripslashes_deep_post( $sku );
1958 + $sku = apply_filters( 'usces_filter_item_save_sku_metadata', $sku, $meta_id );
1959 +
1960 + wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1961 +
1962 + if ( in_array( $skucode, $codes ) ) {
1963 + $uniq_code = true;
1964 + }
1965 +
1966 + if ( WCUtils::is_blank( $skucode ) ) {
1967 + $irreg_code = true;
1968 + }
1969 +
1970 + if ( WCUtils::is_blank( $skuprice ) || preg_match( '/[^0-9.]/', $skuprice ) || 1 < substr_count( $skuprice, '.' ) ) {
1971 + $irreg_price = true;
1972 + }
1973 +
1974 + $codes[] = $skucode;
1975 + }
1976 +
1977 + if ( $uniq_code ) {
1978 + $message .= __( 'SKU code is duplicated.', 'usces' ) . '<br />';
1979 + }
1980 + if ( $irreg_code ) {
1981 + $message .= __( 'SKU code is invalid.', 'usces' ) . '<br />';
1982 + }
1983 + if ( $irreg_price ) {
1984 + $message .= __( 'SKU of invalid selling price exists.', 'usces' ) . '<br />';
1985 + }
1986 + }
1987 +
1988 + // OPT.
1989 + if ( null !== $inputs['itemopt'] ) {
1990 + $meta_ids = array();
1991 + $names = array();
1992 + $uniq_name = false;
1993 + $irreg_name = false;
1994 + $irreg_value = false;
1995 +
1996 + $opts = wel_get_opts( $post_id, 'meta_id', false );
1997 +
1998 + foreach ( $inputs['itemopt'] as $mid => $temp ) {
1999 + $meta_ids[] = $mid;
2000 + }
2001 + $meta_ids = array_unique( $meta_ids );
2002 + foreach ( $meta_ids as $meta_id ) {
2003 +
2004 + $optname = isset( $inputs['itemopt'][ $meta_id ]['name'] ) ? $inputs['itemopt'][ $meta_id ]['name'] : '';
2005 + $optmeans = isset( $inputs['itemopt'][ $meta_id ]['means'] ) ? (int) $inputs['itemopt'][ $meta_id ]['means'] : 0;
2006 + $optessential = isset( $inputs['itemopt'][ $meta_id ]['essential'] ) ? $inputs['itemopt'][ $meta_id ]['essential'] : 0;
2007 + $optsort = isset( $inputs['itemopt'][ $meta_id ]['sort'] ) ? $inputs['itemopt'][ $meta_id ]['sort'] : 0;
2008 + $optvalue = isset( $inputs['itemopt'][ $meta_id ]['value'] ) ? trim( $inputs['itemopt'][ $meta_id ]['value'] ) : '';
2009 +
2010 + $opt = $opts[ $meta_id ];
2011 +
2012 + $opt['name'] = str_replace( "\\", '', $optname );
2013 + $opt['value'] = str_replace( "\\", '', $optvalue );
2014 + $opt['means'] = $optmeans;
2015 + $opt['essential'] = $optessential;
2016 + $opt['sort'] = $optsort;
2017 +
2018 + $opt = $usces->stripslashes_deep_post( $opt );
2019 +
2020 + wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
2021 +
2022 + if ( in_array( $optname, $names ) ) {
2023 + $uniq_name = true;
2024 + }
2025 +
2026 + if ( WCUtils::is_blank( $optname ) ) {
2027 + $irreg_name = true;
2028 + }
2029 +
2030 + if ( WCUtils::is_blank( $optvalue ) && 1 >= $optmeans ) {
2031 + $irreg_value = true;
2032 + }
2033 +
2034 + $names[] = $optname;
2035 + }
2036 +
2037 + if ( $uniq_name ) {
2038 + $message .= __( 'Commodity option option name duplicates exist.', 'usces' ) . '<br />';
2039 + }
2040 + if ( $irreg_name ) {
2041 + $message .= __( 'Commodity option not entered there is the option name.', 'usces' ) . '<br />';
2042 + }
2043 + if ( $irreg_value ) {
2044 + $message .= __( "If you select 'single select' and 'multi-select' the trade option, please enter the select value.", 'usces' ) . '<br />';
2045 + }
2046 + }
2047 +
2048 + do_action( 'usces_action_save_product', $post_id, $post );
2049 + $message = apply_filters( 'usces_filter_save_product_message', $message, $post_id );
2050 +
2051 + if ( $message ) {
2052 + $usces->set_action_status( 'error', 'ERROR : ' . $message );
2053 + } else {
2054 + $usces->set_action_status( 'success', __( 'Registration of the product is complete.', 'usces' ) );
2055 + }
2056 +}
2057 +
2058 +/**
2059 + * Link replace
2060 + *
2061 + * @param string $para Parameters.
2062 + * @return string
2063 + */
2064 +function usces_link_replace( $para ) {
2065 + $str = 'admin.php?page=usces_itemedit&';
2066 + $url = preg_replace( '|post\.php\?|i', $str, $para );
2067 + return $url;
2068 +}
2069 +
2070 +/**
2071 + * Count posts
2072 + *
2073 + * @param string $type Type.
2074 + * @param string $perm perm.
2075 + * @return object
2076 + */
2077 +function usces_count_posts( $type = 'post', $perm = '' ) {
2078 + global $wpdb;
2079 +
2080 + $user = wp_get_current_user();
2081 +
2082 + $cache_key = $type;
2083 +
2084 + $query = "SELECT post_status, COUNT( * ) AS `num_posts` FROM {$wpdb->posts} WHERE post_type = %s AND post_mime_type = 'item'";
2085 + if ( 'readable' == $perm && is_user_logged_in() ) {
2086 + if ( ! current_user_can( "read_private_{$type}s" ) ) {
2087 + $cache_key .= '_' . $perm . '_' . $user->ID;
2088 + $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
2089 + }
2090 + }
2091 + $query .= ' GROUP BY post_status';
2092 +
2093 + $count = wp_cache_get( $cache_key, 'counts' );
2094 + if ( false !== $count ) {
2095 + $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
2096 + }
2097 +
2098 + $stats = array();
2099 + foreach ( (array) $count as $row_num => $row ) {
2100 + $stats[ $row['post_status'] ] = $row['num_posts'];
2101 + }
2102 +
2103 + $stats = (object) $stats;
2104 + wp_cache_set( $cache_key, $stats, 'counts' );
2105 +
2106 + return $stats;
2107 +}
2108 +
2109 +/**
2110 + * Custom order meta row
2111 + *
2112 + * @param string $key Key.
2113 + * @param array $entry Entry data.
2114 + * @return string
2115 + */
2116 +function _list_custom_order_meta_row( $key, $entry ) {
2117 + $r = '';
2118 + $style = '';
2119 + $key = esc_attr( $key );
2120 +
2121 + $name = esc_attr( $entry['name'] );
2122 + $means = get_option( 'usces_custom_order_select', array() );
2123 + $meansoption = '';
2124 + foreach ( $means as $meankey => $meanvalue ) {
2125 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2126 + }
2127 + $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2128 + $value = '';
2129 + if ( is_array( $entry['value'] ) ) {
2130 + foreach ( $entry['value'] as $k => $v ) {
2131 + $value .= $v . "\n";
2132 + }
2133 + }
2134 + $value = esc_attr( trim( $value ) );
2135 +
2136 + $r .= "\n\t<tr id=\"csod-{$key}\" class=\"{$style}\">";
2137 + $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csod[{$key}][key]\" id=\"csod[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2138 + $r .= "\n\t\t<div><input type=\"text\" name=\"csod[{$key}][name]\" id=\"csod[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2139 + $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csod[{$key}][means]\" id=\"csod[{$key}][means]\">" . $meansoption . "</select>\n";
2140 + $r .= "<input type=\"checkbox\" name=\"csod[{$key}][essential]\" id=\"csod[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csod[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . '</label></div>';
2141 + $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csod[{$key}]\" id=\"del_csod[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delOrder('{$key}');\" />";
2142 + $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csod[{$key}]\" id=\"upd_csod[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updOrder('{$key}');\" /></div>";
2143 + $r .= "\n\t\t<div id=\"csod_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2144 + $r .= '</td>';
2145 + $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csod[{$key}][value]\" id=\"csod[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2146 + return $r;
2147 +}
2148 +
2149 +/**
2150 + * Has custom field meta
2151 + *
2152 + * @param string $fieldname Field name.
2153 + * @return mixed
2154 + */
2155 +function usces_has_custom_field_meta( $fieldname ) {
2156 + switch ( $fieldname ) {
2157 + case 'order':
2158 + $field = 'usces_custom_order_field';
2159 + break;
2160 + case 'customer':
2161 + $field = 'usces_custom_customer_field';
2162 + break;
2163 + case 'delivery':
2164 + $field = 'usces_custom_delivery_field';
2165 + break;
2166 + case 'member':
2167 + $field = 'usces_custom_member_field';
2168 + break;
2169 + case 'admin_member':
2170 + $field = 'usces_admin_custom_member_field';
2171 + break;
2172 + default:
2173 + return array();
2174 + }
2175 + $fields = get_option( $field );
2176 + if ( empty( $fields ) ) {
2177 + $meta = array();
2178 + } elseif ( is_array( $fields ) ) {
2179 + $meta = $fields;
2180 + } else {
2181 + $meta = wel_safe_unserialize( $fields );
2182 + }
2183 + return $meta;
2184 +}
2185 +
2186 +/**
2187 + * Get info ajax
2188 + *
2189 + * @return void
2190 + */
2191 +function usces_getinfo_ajax() {
2192 + global $wp_version;
2193 +
2194 + $wcex = usces_get_wcex();
2195 + $wcex_str = join( ',', array_map(
2196 + function( $key, $values ) {
2197 + return $key . '-' . $values['version'];
2198 + },
2199 + array_keys( $wcex ), $wcex )
2200 + );
2201 +
2202 + $theme_data = wp_get_theme();
2203 + $theme = urlencode( $theme_data->get( 'Name' ) . '-' . $theme_data->get( 'Version' ) );
2204 +
2205 + $args = array(
2206 + 'timeout' => 30,
2207 + 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
2208 + 'body' => array(
2209 + 'v' => urlencode( USCES_VERSION ),
2210 + 'wcid' => urlencode( get_option( 'usces_wcid' ) ),
2211 + 'locale'=> urlencode( get_locale() ),
2212 + 'theme' => $theme,
2213 + 'wcex' => urlencode( $wcex_str ),
2214 + 'wcurl' => urlencode( get_home_url() )
2215 + )
2216 + );
2217 +
2218 + $response = wp_remote_post( 'https://www.welcart.com/util/welcart_information2.php', $args );
2219 +
2220 + if ( is_wp_error( $response ) ) {
2221 + die( 'ERROR' );
2222 + }
2223 +
2224 + $body = wp_remote_retrieve_body( $response );
2225 + if ( empty( $body ) ) {
2226 + die( 'ERROR' );
2227 + }
2228 +
2229 + $res = '';
2230 + preg_match_all( "/<(title|data)>(.*?)<\/(title|data)>/s", $body, $matches, PREG_SET_ORDER );
2231 +
2232 + foreach ( $matches as $match ) {
2233 + switch ( $match[1] ) {
2234 + case 'title':
2235 + $res .= '<div style="text-align: center;border-bottom: 1px dotted #CCCCCC;width: 80%;margin-bottom: 10px;padding-bottom: 3px; margin-right: auto; margin-left: auto;"><strong>' . $match[2] . '</strong></div><ul>';
2236 + break;
2237 + case 'data':
2238 + $res .= '<li>' . $match[2] . '</li>';
2239 + break;
2240 + }
2241 + }
2242 + $res .= '</ul>';
2243 +
2244 + die( $res );
2245 +}
2246 +
2247 +/**
2248 + * Custom field ajax
2249 + */
2250 +function custom_field_ajax() {
2251 + global $usces;
2252 +
2253 + check_admin_referer( 'custom_field_ajax', 'wc_nonce' );
2254 + if ( ! current_user_can( 'wel_manage_setting' ) ) {
2255 + wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
2256 + }
2257 +
2258 + $_POST = $usces->stripslashes_deep_post( $_POST );
2259 + $data = array(
2260 + 'status' => 'OK',
2261 + 'msg' => '',
2262 + );
2263 +
2264 + if ( 'custom_field_ajax' !== $_POST['action'] ) {
2265 + $data['status'] = 'NG';
2266 + wp_send_json( $data );
2267 + }
2268 +
2269 + switch ( $_POST['field'] ) {
2270 + case 'order':
2271 + $field = 'usces_custom_order_field';
2272 + break;
2273 + case 'customer':
2274 + $field = 'usces_custom_customer_field';
2275 + break;
2276 + case 'delivery':
2277 + $field = 'usces_custom_delivery_field';
2278 + break;
2279 + case 'member':
2280 + $field = 'usces_custom_member_field';
2281 + break;
2282 + case 'admin_member':
2283 + $field = 'usces_admin_custom_member_field';
2284 + break;
2285 + default:
2286 + $data['status'] = 'NG';
2287 + wp_send_json( $data );
2288 + }
2289 +
2290 + $meta = usces_has_custom_field_meta( $_POST['field'] );
2291 + $dupkey = 0;
2292 + $dupname = 0;
2293 +
2294 + if ( isset( $_POST['add'] ) ) {
2295 + $newkey = ( isset( $_POST['newkey'] ) ) ? trim( $_POST['newkey'] ) : '';
2296 + $newname = ( isset( $_POST['newname'] ) ) ? trim( $_POST['newname'] ) : '';
2297 + $newmeans = ( isset( $_POST['newmeans'] ) ) ? $_POST['newmeans'] : 0;
2298 + $newessential = ( isset( $_POST['newessential'] ) ) ? $_POST['newessential'] : 0;
2299 + $newposition = ( isset( $_POST['newposition'] ) ) ? trim( $_POST['newposition'] ) : '';
2300 +
2301 + if ( 2 === (int) $newmeans || 5 === (int) $newmeans ) { // Text or Textarea.
2302 + $newvalue = '';
2303 + $nv = $newvalue;
2304 + $required_entry = ( ! empty( $newkey ) && ! empty( $newname ) ) ? true : false;
2305 + } else {
2306 + $newvalue = ( isset( $_POST['newvalue'] ) ) ? explode( "\n", trim( $_POST['newvalue'] ) ) : '';
2307 + foreach ( (array) $newvalue as $v ) {
2308 + if ( ! WCUtils::is_blank( $v ) ) {
2309 + $nv[] = trim( $v );
2310 + }
2311 + }
2312 + $required_entry = ( ( ! empty( $newvalue ) ) && ! empty( $newkey ) && ! empty( $newname ) ) ? true : false;
2313 + }
2314 +
2315 + if ( ! array_key_exists( $newkey, $meta ) ) {
2316 + if ( $required_entry ) {
2317 + $meta[ $newkey ]['name'] = $newname;
2318 + $meta[ $newkey ]['means'] = $newmeans;
2319 + $meta[ $newkey ]['essential'] = $newessential;
2320 + $meta[ $newkey ]['value'] = $nv;
2321 + if ( ! WCUtils::is_blank( $newposition ) ) {
2322 + $meta[ $newkey ]['position'] = $newposition;
2323 + } elseif ( ( 'admin_member' === $_POST['field'] ) && WCUtils::is_blank( $newposition ) ) {
2324 + $meta[ $newkey ]['position'] = 'fax_after';
2325 + }
2326 + update_option( $field, $meta );
2327 + }
2328 + } else {
2329 + $dupkey = 1;
2330 + }
2331 +
2332 + } elseif ( isset( $_POST['update'] ) ) {
2333 + $key = ( isset( $_POST['key'] ) ) ? trim( $_POST['key'] ) : '';
2334 + $name = ( isset( $_POST['name'] ) ) ? trim( $_POST['name'] ) : '';
2335 + $means = ( isset( $_POST['means'] ) ) ? $_POST['means'] : 0;
2336 + $essential = ( isset( $_POST['essential'] ) ) ? $_POST['essential'] : 0;
2337 + $position = ( isset( $_POST['position'] ) ) ? trim( $_POST['position'] ) : '';
2338 +
2339 + if ( 2 === (int) $means || 5 === (int) $means ) { // Text or Textarea.
2340 + $value = '';
2341 + $nv = $value;
2342 + $required_entry = ( ! empty( $key ) && ! empty( $name ) ) ? true : false;
2343 +
2344 + } else {
2345 + $value = ( isset( $_POST['value'] ) ) ? explode( "\n", trim( $_POST['value'] ) ) : '';
2346 + foreach ( (array) $value as $v ) {
2347 + if ( ! WCUtils::is_blank( $v ) ) {
2348 + $nv[] = trim( $v );
2349 + }
2350 + }
2351 + $required_entry = ( ( ! empty( $value ) ) && ! empty( $key ) && ! empty( $name ) ) ? true : false;
2352 + }
2353 +
2354 + if ( $required_entry ) {
2355 + $meta[ $key ]['name'] = $name;
2356 + $meta[ $key ]['means'] = $means;
2357 + $meta[ $key ]['essential'] = $essential;
2358 + $meta[ $key ]['value'] = $nv;
2359 + if ( ! WCUtils::is_blank( $position ) ) {
2360 + $meta[ $key ]['position'] = $position;
2361 + }
2362 + update_option( $field, $meta );
2363 + }
2364 +
2365 + } elseif ( isset( $_POST['delete'] ) ) {
2366 + $key = ( isset( $_POST['key'] ) ) ? trim( $_POST['key'] ) : '';
2367 + unset( $meta[ $key ] );
2368 + update_option( $field, $meta );
2369 + }
2370 +
2371 + $r = '';
2372 + switch ( $_POST['field'] ) {
2373 + case 'order':
2374 + foreach ( $meta as $key => $entry ) {
2375 + $r .= _list_custom_order_meta_row( $key, $entry );
2376 + }
2377 + break;
2378 + case 'customer':
2379 + foreach ( $meta as $key => $entry ) {
2380 + $r .= _list_custom_customer_meta_row( $key, $entry );
2381 + }
2382 + break;
2383 + case 'delivery':
2384 + foreach ( $meta as $key => $entry ) {
2385 + $r .= _list_custom_delivery_meta_row( $key, $entry );
2386 + }
2387 + break;
2388 + case 'member':
2389 + foreach ( $meta as $key => $entry ) {
2390 + $r .= _list_custom_member_meta_row( $key, $entry );
2391 + }
2392 + break;
2393 + case 'admin_member':
2394 + foreach ( $meta as $key => $entry ) {
2395 + $r .= _list_admin_custom_member_meta_row( $key, $entry );
2396 + }
2397 + break;
2398 + }
2399 +
2400 + $data['list'] = $r;
2401 + $data['dupkey'] = $dupkey;
2402 + $data['dupname'] = $dupname;
2403 + wp_send_json( $data );
2404 +}
2405 +
2406 +/**
2407 + * List custom customer meta row
2408 + *
2409 + * @param string $key Key.
2410 + * @param array $entry Entry data.
2411 + * @return string
2412 + */
2413 +function _list_custom_customer_meta_row( $key, $entry ) {
2414 + $r = '';
2415 + $style = '';
2416 + $key = esc_attr( $key );
2417 +
2418 + $name = esc_attr( $entry['name'] );
2419 + $means = get_option( 'usces_custom_customer_select', array() );
2420 + $meansoption = '';
2421 + foreach ( $means as $meankey => $meanvalue ) {
2422 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2423 + }
2424 + $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2425 + $value = '';
2426 + if ( is_array( $entry['value'] ) ) {
2427 + foreach ( $entry['value'] as $k => $v ) {
2428 + $value .= $v . "\n";
2429 + }
2430 + }
2431 + $value = esc_attr( trim( $value ) );
2432 + $positions = get_option( 'usces_custom_field_position_select', array() );
2433 + $positionsoption = '';
2434 + foreach ( $positions as $poskey => $posvalue ) {
2435 + $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2436 + }
2437 +
2438 + $r .= "\n\t<tr id=\"cscs-{$key}\" class=\"{$style}\">";
2439 + $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"cscs[{$key}][key]\" id=\"cscs[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2440 + $r .= "\n\t\t<div><input type=\"text\" name=\"cscs[{$key}][name]\" id=\"cscs[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2441 + $r .= "\n\t\t<div class=\"optcheck\"><select name=\"cscs[{$key}][means]\" id=\"cscs[{$key}][means]\">" . $meansoption . "</select>\n";
2442 + $r .= "<input type=\"checkbox\" name=\"cscs[{$key}][essential]\" id=\"cscs[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"cscs[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2443 + $r .= "<select name=\"cscs[{$key}][position]\" id=\"cscs[{$key}][position]\">" . $positionsoption . '</select></div>';
2444 + $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_cscs[{$key}]\" id=\"del_cscs[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delCustomer('{$key}');\" />";
2445 + $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_cscs[{$key}]\" id=\"upd_cscs[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updCustomer('{$key}');\" /></div>";
2446 + $r .= "\n\t\t<div id=\"cscs_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2447 + $r .= '</td>';
2448 + $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"cscs[{$key}][value]\" id=\"cscs[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2449 + return $r;
2450 +}
2451 +
2452 +/**
2453 + * List custom delivery meta row
2454 + *
2455 + * @param string $key Key.
2456 + * @param array $entry Entry data.
2457 + * @return string
2458 + */
2459 +function _list_custom_delivery_meta_row( $key, $entry ) {
2460 + $r = '';
2461 + $style = '';
2462 + $key = esc_attr( $key );
2463 +
2464 + $name = esc_attr( $entry['name'] );
2465 + $means = get_option( 'usces_custom_delivery_select', array() );
2466 + $meansoption = '';
2467 + foreach ( $means as $meankey => $meanvalue ) {
2468 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2469 + }
2470 + $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2471 + $value = '';
2472 + if ( is_array( $entry['value'] ) ) {
2473 + foreach ( $entry['value'] as $k => $v ) {
2474 + $value .= $v . "\n";
2475 + }
2476 + }
2477 + $value = esc_attr( trim( $value ) );
2478 + $positions = get_option( 'usces_custom_field_position_select', array() );
2479 + $positionsoption = '';
2480 + foreach ( $positions as $poskey => $posvalue ) {
2481 + $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2482 + }
2483 +
2484 + $r .= "\n\t<tr id=\"csde-{$key}\" class=\"{$style}\">";
2485 + $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csde[{$key}][key]\" id=\"csde[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2486 + $r .= "\n\t\t<div><input type=\"text\" name=\"csde[{$key}][name]\" id=\"csde[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2487 + $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csde[{$key}][means]\" id=\"csde[{$key}][means]\">" . $meansoption . "</select>\n";
2488 + $r .= "<input type=\"checkbox\" name=\"csde[{$key}][essential]\" id=\"csde[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csde[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2489 + $r .= "<select name=\"csde[{$key}][position]\" id=\"csde[{$key}][position]\">" . $positionsoption . '</select></div>';
2490 + $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csde[{$key}]\" id=\"del_csde[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delDelivery('{$key}');\" />";
2491 + $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csde[{$key}]\" id=\"upd_csde[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updDelivery('{$key}');\" /></div>";
2492 + $r .= "\n\t\t<div id=\"csde_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2493 + $r .= '</td>';
2494 + $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csde[{$key}][value]\" id=\"csde[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2495 + return $r;
2496 +}
2497 +
2498 +/**
2499 + * List custom member meta row
2500 + *
2501 + * @param string $key Key.
2502 + * @param array $entry Entry data.
2503 + * @return string
2504 + */
2505 +function _list_custom_member_meta_row( $key, $entry ) {
2506 + $r = '';
2507 + $style = '';
2508 + $key = esc_attr( $key );
2509 +
2510 + $name = esc_attr( $entry['name'] );
2511 + $means = get_option( 'usces_custom_member_select', array() );
2512 + $meansoption = '';
2513 + foreach ( $means as $meankey => $meanvalue ) {
2514 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2515 + }
2516 + $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2517 + $value = '';
2518 + if ( is_array( $entry['value'] ) ) {
2519 + foreach ( $entry['value'] as $k => $v ) {
2520 + $value .= $v . "\n";
2521 + }
2522 + }
2523 + $value = esc_attr( trim( $value ) );
2524 + $positions = get_option( 'usces_custom_field_position_select', array() );
2525 + $positionsoption = '';
2526 + foreach ( $positions as $poskey => $posvalue ) {
2527 + $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2528 + }
2529 + $r .= "\n\t<tr id=\"csmb-{$key}\" class=\"{$style}\">";
2530 + $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csmb[{$key}][key]\" id=\"csmb[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2531 + $r .= "\n\t\t<div><input type=\"text\" name=\"csmb[{$key}][name]\" id=\"csmb[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2532 + $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csmb[{$key}][means]\" id=\"csmb[{$key}][means]\">" . $meansoption . "</select>\n";
2533 + $r .= "<input type=\"checkbox\" name=\"csmb[{$key}][essential]\" id=\"csmb[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csmb[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2534 + $r .= "<select name=\"csmb[{$key}][position]\" id=\"csmb[{$key}][position]\">" . $positionsoption . '</select></div>';
2535 + $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csmb[{$key}]\" id=\"del_csmb[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delMember('{$key}');\" />";
2536 + $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csmb[{$key}]\" id=\"upd_csmb[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updMember('{$key}');\" /></div>";
2537 + $r .= "\n\t\t<div id=\"csmb_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2538 + $r .= '</td>';
2539 + $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csmb[{$key}][value]\" id=\"csmb[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2540 + return $r;
2541 +}
2542 +
2543 +/**
2544 + * List admin custom member meta row
2545 + *
2546 + * @param string $key Key.
2547 + * @param array $entry Entry data.
2548 + * @return string
2549 + */
2550 +function _list_admin_custom_member_meta_row( $key, $entry ) {
2551 + $r = '';
2552 + $style = '';
2553 + $key = esc_attr( $key );
2554 +
2555 + $name = esc_attr( $entry['name'] );
2556 + $means = get_option( 'usces_custom_member_select', array() );
2557 + $meansoption = '';
2558 + foreach ( $means as $meankey => $meanvalue ) {
2559 + $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2560 + }
2561 + $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2562 + $value = '';
2563 + if ( is_array( $entry['value'] ) ) {
2564 + foreach ( $entry['value'] as $k => $v ) {
2565 + $value .= $v . "\n";
2566 + }
2567 + }
2568 + $value = esc_attr( trim( $value ) );
2569 + $r .= "\n\t<tr id=\"admb-{$key}\" class=\"{$style}\">";
2570 + $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"admb[{$key}][key]\" id=\"admb[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2571 + $r .= "\n\t\t<div><input type=\"text\" name=\"admb[{$key}][name]\" id=\"admb[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2572 + $r .= "\n\t\t<div class=\"optcheck\"><select name=\"admb[{$key}][means]\" id=\"admb[{$key}][means]\">" . $meansoption . "</select>\n";
2573 + $r .= "<input type=\"checkbox\" name=\"admb[{$key}][essential]\" id=\"admb[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"admb[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2574 + $r .= '</div>';
2575 + $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_admb[{$key}]\" id=\"del_admb[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delAdmb('{$key}');\" />";
2576 + $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_admb[{$key}]\" id=\"upd_admb[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updAdmb('{$key}');\" /></div>";
2577 + $r .= "\n\t\t<div id=\"admb_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2578 + $r .= '</td>';
2579 + $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"admb[{$key}][value]\" id=\"admb[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2580 + return $r;
2581 +}
2582 +
2583 +/**
2584 + * Change State/Prefecture
2585 + */
2586 +function change_states_ajax() {
2587 + global $usces, $usces_states;
2588 +
2589 + $_POST = $usces->stripslashes_deep_post( $_POST );
2590 +
2591 + $c = $_POST['country'];
2592 + $res = '';
2593 + $prefs = get_usces_states( $c );
2594 + if ( is_array( $prefs ) && 0 < count( $prefs ) ) {
2595 + foreach ( (array) $prefs as $state ) {
2596 + $res .= '<option value="' . $state . '">' . $state . '</option>';
2597 + }
2598 + } else {
2599 + die( 'error' );
2600 + }
2601 + die( $res );
2602 +}
2603 +
2604 +/**
2605 + * State/Prefecture
2606 + *
2607 + * @param string $country Country.
2608 + * @return array
2609 + */
2610 +function get_usces_states( $country ) {
2611 + global $usces, $usces_states;
2612 +
2613 + $states = array();
2614 + $prefs = wel_safe_maybe_unserialize( $usces->options['province'] );
2615 + if ( ! isset( $prefs[ $country ] ) || empty( $prefs[ $country ] ) ) {
2616 + if ( $country == $usces->options['system']['base_country'] ) {
2617 + foreach ( (array) $prefs as $state ) {
2618 + if ( ! is_array( $state ) ) {
2619 + array_push( $states, esc_html( $state ) );
2620 + }
2621 + }
2622 + if ( 0 === count( $states ) ) {
2623 + if ( ! empty( $usces_states[ $country ] ) ) {
2624 + $prefs = $usces_states[ $country ];
2625 + if ( is_array( $prefs ) ) {
2626 + $states = array_map( 'esc_html', $prefs );
2627 + }
2628 + }
2629 + }
2630 + } else {
2631 + if ( ! empty( $usces_states[ $country ] ) ) {
2632 + $prefs = $usces_states[ $country ];
2633 + if ( is_array( $prefs ) ) {
2634 + $states = array_map( 'esc_html', $prefs );
2635 + }
2636 + }
2637 + }
2638 + } else {
2639 + $states = is_array( $prefs[ $country ] ) ? array_map( 'esc_html', $prefs[ $country ] ) : esc_html( $prefs[ $country ] );
2640 + }
2641 + return $states;
2642 +}
2643 +
2644 +/**
2645 + * Target market countries
2646 + */
2647 +function target_market_ajax() {
2648 + global $usces;
2649 +
2650 + $_POST = $usces->stripslashes_deep_post( $_POST );
2651 + $response = array();
2652 + $target = explode( ',', $_POST['target'] );
2653 + foreach ( (array) $target as $country ) {
2654 + $prefs = get_usces_states( $country );
2655 + if ( is_array( $prefs ) && ! empty( $prefs ) ) {
2656 + $pos = strpos( $prefs[0], '--' );
2657 + if ( false !== $pos ) {
2658 + array_shift( $prefs );
2659 + }
2660 + $response[] = $country . ',' . implode( "\n", $prefs );
2661 + } else {
2662 + $response[] = $country;
2663 + }
2664 + }
2665 + wp_send_json( $response );
2666 +}
2667 +
2668 +/**
2669 + * Admin ajax
2670 + */
2671 +function usces_admin_ajax() {
2672 + switch ( $_POST['mode'] ) {
2673 + case 'options_backup':
2674 + check_admin_referer( 'options_backup', 'wc_nonce' );
2675 + $options = get_option( 'usces' );
2676 + $res = true;
2677 + if ( is_array( $options ) ) {
2678 + $usces_backup_date = current_time( 'mysql' );
2679 + update_option( 'usces_backup', $options );
2680 + update_option( 'usces_backup_date', $usces_backup_date );
2681 + $res = $usces_backup_date;
2682 + } else {
2683 + $res = false;
2684 + }
2685 + die( $res );
2686 + break;
2687 + case 'options_restore':
2688 + check_admin_referer( 'options_restore', 'wc_nonce' );
2689 + $options = get_option( 'usces_backup' );
2690 + $res = true;
2691 + if ( is_array( $options ) ) {
2692 + update_option( 'usces', $options );
2693 + } else {
2694 + $res = false;
2695 + }
2696 + die( $res );
2697 + break;
2698 + }
2699 + do_action( 'usces_action_admin_ajax' );
2700 +}
2701 +
2702 +/**
2703 + * Order amount recalculation
2704 + *
2705 + * @param int $order_id Order ID.
2706 + * @param int $mem_id Member ID.
2707 + * @param int $post_id Post ID.
2708 + * @param float $price Price.
2709 + * @param int $quant Quantity.
2710 + * @param array $cart_id Cart ID.
2711 + * @param int $use_point Use point.
2712 + * @param float $shipping_charge Shipping charge.
2713 + * @param float $cod_fee Fee.
2714 + * @param float $discount Discount.
2715 + * @param string $change_taxrate Change applicable tax rate.
2716 + */
2717 +function usces_order_recalculation( $order_id, $mem_id, $post_id, $price, $quant, $cart_id, $use_point, $shipping_charge, $cod_fee, $discount = 0, $change_taxrate = '' ) {
2718 + global $usces;
2719 +
2720 + $data = array();
2721 + $res = 'ok';
2722 +
2723 + $cart = array();
2724 + $post_id_count = ( is_array( $post_id ) ) ? count( $post_id ) : 0;
2725 + for ( $i = 0; $i < $post_id_count; $i++ ) {
2726 + if ( $post_id[ $i ] ) {
2727 + $cart[] = array(
2728 + 'post_id' => $post_id[ $i ],
2729 + 'price' => (float) $price[ $i ],
2730 + 'quantity' => (float) $quant[ $i ],
2731 + );
2732 + }
2733 + }
2734 +
2735 + if ( 'change' === $change_taxrate ) {
2736 + if ( usces_is_reduced_taxrate() ) {
2737 + $usces_tax = Welcart_Tax::get_instance();
2738 + $usces_tax->set_order_condition_reduced_taxrate( $order_id );
2739 + for ( $i = 0; $i < $post_id_count; $i++ ) {
2740 + if ( $cart_id[ $i ] ) {
2741 + $taxrate = usces_get_ordercart_meta( 'taxrate', $cart_id[ $i ] );
2742 + if ( ! $taxrate ) {
2743 + $ordercart = usces_get_ordercartdata_row( $cart_id[ $i ] );
2744 + $sku['taxrate'] = $usces_tax->get_sku_applicable_taxrate( $post_id[ $i ], $ordercart['sku_code'] );
2745 + $usces_tax->set_ordercart_applicable_taxrate( $cart_id[ $i ], $sku );
2746 + }
2747 + }
2748 + }
2749 + $data['status'] = $res;
2750 + $data['tax_mode'] = 'reduced';
2751 + wp_send_json( $data );
2752 + }
2753 + $condition = $usces->get_condition();
2754 + } else {
2755 + if ( ! empty( $order_id ) ) {
2756 + $condition = usces_get_order_condition( $order_id );
2757 + } else {
2758 + $condition = $usces->get_condition();
2759 + }
2760 + }
2761 +
2762 + $tax_display = ( isset( $condition['tax_display'] ) ) ? $condition['tax_display'] : usces_get_tax_display();
2763 + $member_system = ( isset( $condition['membersystem_state'] ) ) ? $condition['membersystem_state'] : $usces->options['membersystem_state'];
2764 + $member_system_point = ( isset( $condition['membersystem_point'] ) ) ? $condition['membersystem_point'] : $usces->options['membersystem_point'];
2765 + $tax_mode = ( isset( $condition['tax_mode'] ) ) ? $condition['tax_mode'] : usces_get_tax_mode();
2766 + $tax_target = ( isset( $condition['tax_target'] ) ) ? $condition['tax_target'] : usces_get_tax_target();
2767 + $point_coverage = ( isset( $condition['point_coverage'] ) ) ? $condition['point_coverage'] : usces_point_coverage();
2768 + $tax_method = ( isset( $condition['tax_method'] ) ) ? $condition['tax_method'] : $usces->options['tax_method'];
2769 +
2770 + $total_items_price = 0;
2771 + foreach ( $cart as $cart_row ) {
2772 + $total_items_price += $cart_row['price'] * $cart_row['quantity'];
2773 + }
2774 + $meminfo = $usces->get_member_info( $mem_id );
2775 +
2776 + if ( empty( $discount ) || 'NaN' == $discount ) {
2777 + $discount = 0;
2778 + }
2779 + if ( 'change' == $change_taxrate ) {
2780 + $discount = 0;
2781 + if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2782 + if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2783 + if ( 0 === (int) $condition['campaign_category'] ) {
2784 + $discount = (float) sprintf( '%.3f', $total_items_price * (float) $condition['privilege_discount'] / 100 );
2785 + } else {
2786 + foreach ( $cart as $cart_row ) {
2787 + if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) {
2788 + $discount += (float) sprintf( '%.3f', $cart_row['price'] * $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 );
2789 + }
2790 + }
2791 + }
2792 + }
2793 + }
2794 + if ( 0 != $discount ) {
2795 + $decimal = $usces->get_currency_decimal();
2796 + if ( 0 == $decimal ) {
2797 + $discount = ceil( $discount );
2798 + } else {
2799 + $decipad = (int) str_pad( '1', $decimal + 1, '0', STR_PAD_RIGHT );
2800 + $discount = ceil( $discount * $decipad ) / $decipad;
2801 + }
2802 + $discount = $discount * -1;
2803 + }
2804 + }
2805 + $discount = apply_filters( 'usces_filter_order_discount_recalculation', $discount, $cart, $condition, $order_id );
2806 +
2807 + $point = 0;
2808 + if ( empty( $use_point ) || 'NaN' == $use_point ) {
2809 + $use_point = 0;
2810 + }
2811 + if ( 'activate' === $member_system && 'activate' === $member_system_point && ! empty( $meminfo['ID'] ) ) {
2812 + if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2813 + if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2814 + foreach ( $cart as $cart_row ) {
2815 + $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2816 + if ( ! in_array( $condition['campaign_category'], $cats ) ) {
2817 + $product = wel_get_product( $cart_row['post_id'] );
2818 + $rate = (float) $product['itemPointrate'];
2819 + $price = $cart_row['price'] * $cart_row['quantity'];
2820 + $point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2821 + }
2822 + }
2823 + } elseif ( isset( $condition['campaign_privilege'] ) && 'point' === $condition['campaign_privilege'] ) {
2824 + foreach ( $cart as $cart_row ) {
2825 + $product = wel_get_product( $cart_row['post_id'] );
2826 + $rate = (float) $product['itemPointrate'];
2827 + $price = $cart_row['price'] * $cart_row['quantity'];
2828 + $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2829 + if ( in_array( $condition['campaign_category'], $cats ) ) {
2830 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 * (float) $condition['privilege_point'] ) );
2831 + } else {
2832 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2833 + }
2834 + }
2835 + }
2836 + } else {
2837 + foreach ( $cart as $cart_row ) {
2838 + $product = wel_get_product( $cart_row['post_id'] );
2839 + $rate = (float) $product['itemPointrate'];
2840 + $price = $cart_row['price'] * $cart_row['quantity'];
2841 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2842 + }
2843 + }
2844 +
2845 + if ( 0 < $use_point ) {
2846 + $point = (float) sprintf( '%.3f', $point - ( $point * (int) $use_point / $total_items_price ) );
2847 + $point = ceil( $point );
2848 + if ( 0 > $point ) {
2849 + $point = 0;
2850 + }
2851 + } else {
2852 + if ( 0 < $point ) {
2853 + $point = ceil( $point );
2854 + }
2855 + }
2856 + }
2857 + $point = apply_filters( 'usces_filter_set_point_recalculation', $point, $condition, $cart, $meminfo, $use_point, $order_id );
2858 +
2859 + $total_price = $total_items_price - $use_point + $discount + $shipping_charge + $cod_fee;
2860 + if ( $total_price < 0 ) {
2861 + $total_price = 0;
2862 + }
2863 + $total_price = apply_filters( 'usces_filter_set_cart_fees_total_price', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
2864 + $total_price = apply_filters( 'usces_filter_order_total_price_recalculation', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
2865 + $materials = compact( 'total_items_price', 'shipping_charge', 'discount', 'cod_fee', 'use_point', 'condition' );
2866 + if ( 'activate' === $tax_display ) {
2867 + if ( 'include' === $tax_mode ) {
2868 + $tax = 0;
2869 + $include_tax = usces_internal_tax( $materials, 'return' );
2870 + } else {
2871 + // $tax = $usces->getTax( $total_price, $materials );
2872 + if ( 1 === (int) $point_coverage ) {
2873 + if ( 'products' === $tax_target ) {
2874 + $total = (float) $total_items_price + (float) $discount;
2875 + } else {
2876 + $total = (float) $total_items_price + (float) $discount + (float) $shipping_charge + (float) $cod_fee;
2877 + }
2878 + } else {
2879 + if ( 'products' === $tax_target ) {
2880 + $total = (float) $total_items_price + (float) $discount;
2881 + } else {
2882 + if ( empty( $use_point ) ) {
2883 + $use_point = 0;
2884 + }
2885 + $total = (float) $total_items_price + (float) $discount - (int) $use_point + (float) $shipping_charge + (float) $cod_fee;
2886 + }
2887 + }
2888 + $tax = (float) sprintf( '%.3f', (float) $total * (float) $condition['tax_rate'] / 100 );
2889 + $tax = usces_tax_rounding_off( $tax, $tax_method );
2890 + $include_tax = 0;
2891 + }
2892 + } else {
2893 + $tax = 0;
2894 + $include_tax = 0;
2895 + }
2896 + $total_full_price = $total_price + $tax;
2897 + $total_full_price = apply_filters( 'usces_filter_set_cart_fees_total_full_price', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
2898 + $total_full_price = apply_filters( 'usces_filter_order_total_full_price_recalculation', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
2899 +
2900 + $data['status'] = $res;
2901 + $data['tax_mode'] = 'standard';
2902 + $data['discount'] = $discount;
2903 + $data['tax'] = usces_crform( $tax, false, false, 'return', false );
2904 + $data['include_tax'] = ( 0 < $include_tax ) ? '(' . usces_crform( $include_tax, false, false, 'return', true ) . ')' : '';
2905 + $data['point'] = $point;
2906 + $data['total_full_price'] = usces_crform( $total_full_price, false, false, 'return', false );
2907 + wp_send_json( $data );
2908 +}
2909 +
2910 +/**
2911 + * Order amount recalculation ( for Reduced tax rate )
2912 + *
2913 + * @param int $order_id Order ID.
2914 + * @param int $mem_id Member ID.
2915 + * @param int $post_id Post ID.
2916 + * @param float $price Price.
2917 + * @param int $quant Quantity.
2918 + * @param array $cart_id Cart ID.
2919 + * @param int $use_point Use point.
2920 + * @param float $shipping_charge Shipping charge.
2921 + * @param float $cod_fee Fee.
2922 + * @param float $discount_standard Standard tax rate discount.
2923 + * @param float $discount_reduced Reduced tax rate discount.
2924 + * @param string $change_taxrate Change applicable tax rate.
2925 + * @return void
2926 + */
2927 +function usces_order_recalculation_reduced( $order_id, $mem_id, $post_id, $price, $quant, $cart_id, $use_point, $shipping_charge, $cod_fee, $discount_standard = 0, $discount_reduced = 0, $change_taxrate = '' ) {
2928 + global $usces;
2929 +
2930 + $usces_tax = Welcart_Tax::get_instance();
2931 +
2932 + $data = array();
2933 + $res = 'ok';
2934 +
2935 + if ( 'change' === $change_taxrate ) {
2936 + $condition = $usces->get_condition();
2937 + $usces_tax->set_order_condition_reduced_taxrate( $order_id );
2938 + } else {
2939 + if ( ! empty( $order_id ) ) {
2940 + $condition = usces_get_order_condition( $order_id );
2941 + } else {
2942 + $condition = $usces->get_condition();
2943 + }
2944 + }
2945 +
2946 + $tax_rate = ( isset( $condition['tax_rate'] ) ) ? (float) $condition['tax_rate'] : (float) $usces->options['tax_rate'];
2947 + $tax_rate_reduced = ( isset( $condition['tax_rate_reduced'] ) ) ? (float) $condition['tax_rate_reduced'] : (float) $usces->options['tax_rate_reduced'];
2948 + $tax_display = ( isset( $condition['tax_display'] ) ) ? $condition['tax_display'] : usces_get_tax_display();
2949 + $member_system = ( isset( $condition['membersystem_state'] ) ) ? $condition['membersystem_state'] : $usces->options['membersystem_state'];
2950 + $member_system_point = ( isset( $condition['membersystem_point'] ) ) ? $condition['membersystem_point'] : $usces->options['membersystem_point'];
2951 + $tax_mode = ( isset( $condition['tax_mode'] ) ) ? $condition['tax_mode'] : usces_get_tax_mode();
2952 + $tax_target = ( isset( $condition['tax_target'] ) ) ? $condition['tax_target'] : usces_get_tax_target();
2953 + $point_coverage = ( isset( $condition['point_coverage'] ) ) ? $condition['point_coverage'] : usces_point_coverage();
2954 + $tax_method = ( isset( $condition['tax_method'] ) ) ? $condition['tax_method'] : $usces->options['tax_method'];
2955 +
2956 + $cart = array();
2957 + $post_id_count = ( is_array( $post_id ) ) ? count( $post_id ) : 0;
2958 + for ( $i = 0; $i < $post_id_count; $i++ ) {
2959 + if ( $post_id[ $i ] ) {
2960 + $ordercart = usces_get_ordercartdata_row( $cart_id[ $i ] );
2961 + $applicable_taxrate = ( 'change' === $change_taxrate ) ? $usces_tax->get_sku_applicable_taxrate( $post_id[ $i ], $ordercart['sku_code'] ) : $usces_tax->get_ordercart_applicable_taxrate( $cart_id[ $i ], $post_id[ $i ], $ordercart['sku_code'] );
2962 + $cart[] = array(
2963 + 'post_id' => $post_id[ $i ],
2964 + 'sku_code' => $ordercart['sku_code'],
2965 + 'price' => (float) $price[ $i ],
2966 + 'quantity' => (float) $quant[ $i ],
2967 + 'taxrate' => $applicable_taxrate,
2968 + );
2969 + }
2970 + }
2971 +
2972 + $subtotal_standard = 0;
2973 + $subtotal_reduced = 0;
2974 + foreach ( $cart as $cart_row ) {
2975 + if ( 'reduced' === $cart_row['taxrate'] ) {
2976 + $subtotal_reduced += (float) $cart_row['price'] * (float) $cart_row['quantity'];
2977 + } else {
2978 + $subtotal_standard += (float) $cart_row['price'] * (float) $cart_row['quantity'];
2979 + }
2980 + }
2981 + $total_items_price = $subtotal_standard + $subtotal_reduced;
2982 + $meminfo = $usces->get_member_info( $mem_id );
2983 +
2984 + if ( 'change' === $change_taxrate ) {
2985 + $discount = 0;
2986 + $discount_standard = 0;
2987 + $discount_reduced = 0;
2988 + if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2989 + if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2990 + if ( 0 === (int) $condition['campaign_category'] ) {
2991 + $discount_standard = (float) sprintf( '%.3f', (float) $subtotal_standard * (float) $condition['privilege_discount'] / 100 );
2992 + $discount_reduced = (float) sprintf( '%.3f', (float) $subtotal_reduced * (float) $condition['privilege_discount'] / 100 );
2993 + } else {
2994 + foreach ( $cart as $cart_row ) {
2995 + if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) {
2996 + $items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 );
2997 + if ( 'reduced' === $cart_row['taxrate'] ) {
2998 + $discount_reduced += $items_discount;
2999 + } else {
3000 + $discount_standard += $items_discount;
3001 + }
3002 + }
3003 + }
3004 + }
3005 + if ( 0 != $discount_standard || 0 != $discount_reduced ) {
3006 + $decimal = $usces->get_currency_decimal();
3007 + if ( 0 == $decimal ) {
3008 + $discount_standard = ceil( $discount_standard );
3009 + $discount_reduced = ceil( $discount_reduced );
3010 + } else {
3011 + $decipad = (int) str_pad( '1', $decimal + 1, '0', STR_PAD_RIGHT );
3012 + $discount_standard = ceil( $discount_standard * $decipad ) / $decipad;
3013 + $discount_reduced = ceil( $discount_reduced * $decipad ) / $decipad;
3014 + }
3015 + $discount_standard *= -1;
3016 + $discount_reduced *= -1;
3017 + $discount = $discount_standard + $discount_reduced;
3018 + }
3019 + }
3020 + }
3021 + }
3022 + $discount = $discount_standard + $discount_reduced;
3023 + $discount = apply_filters( 'usces_filter_order_discount_recalculation', $discount, $cart, $condition, $order_id );
3024 +
3025 + $point = 0;
3026 + if ( empty( $use_point ) || 'NaN' == $use_point ) {
3027 + $use_point = 0;
3028 + }
3029 + if ( 'activate' === $member_system && 'activate' === $member_system_point && ! empty( $meminfo['ID'] ) ) {
3030 + if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
3031 + if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
3032 + foreach ( $cart as $cart_row ) {
3033 + $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
3034 + if ( ! in_array( $condition['campaign_category'], $cats ) ) {
3035 + $product = wel_get_product( $cart_row['post_id'] );
3036 + $rate = (float) $product['itemPointrate'];
3037 + $price = $cart_row['price'] * $cart_row['quantity'];
3038 + $point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3039 + }
3040 + }
3041 + } elseif ( isset( $condition['campaign_privilege'] ) && 'point' === $condition['campaign_privilege'] ) {
3042 + foreach ( $cart as $cart_row ) {
3043 + $product = wel_get_product( $cart_row['post_id'] );
3044 + $rate = (float) $product['itemPointrate'];
3045 + $price = $cart_row['price'] * $cart_row['quantity'];
3046 + $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
3047 + if ( in_array( $condition['campaign_category'], $cats ) ) {
3048 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 * (float) $condition['privilege_point'] ) );
3049 + } else {
3050 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3051 + }
3052 + }
3053 + }
3054 + } else {
3055 + foreach ( $cart as $cart_row ) {
3056 + $product = wel_get_product( $cart_row['post_id'] );
3057 + $rate = (float) $product['itemPointrate'];
3058 + $price = $cart_row['price'] * $cart_row['quantity'];
3059 + $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3060 + }
3061 + }
3062 +
3063 + if ( 0 < $use_point ) {
3064 + $point = (float) sprintf( '%.3f', $point - ( $point * (int) $use_point / $total_items_price ) );
3065 + $point = ceil( $point );
3066 + if ( 0 > $point ) {
3067 + $point = 0;
3068 + }
3069 + } else {
3070 + if ( 0 < $point ) {
3071 + $point = ceil( $point );
3072 + }
3073 + }
3074 + }
3075 + $point = apply_filters( 'usces_filter_set_point_recalculation', $point, $condition, $cart, $meminfo, $use_point, $order_id );
3076 +
3077 + $total_price = $total_items_price - $use_point + $discount + $shipping_charge + $cod_fee;
3078 + if ( $total_price < 0 ) {
3079 + $total_price = 0;
3080 + }
3081 + $total_price = apply_filters( 'usces_filter_set_cart_fees_total_price', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
3082 + $total_price = apply_filters( 'usces_filter_order_total_price_recalculation', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
3083 +
3084 + $tax = 0;
3085 + $tax_standard = 0;
3086 + $tax_reduced = 0;
3087 + $include_tax = 0;
3088 + if ( 'activate' === $tax_display ) {
3089 + if ( 'all' === $tax_target ) {
3090 + if ( 0 < $shipping_charge ) {
3091 + $subtotal_standard += (float) $shipping_charge;
3092 + }
3093 + if ( 0 < $cod_fee ) {
3094 + $subtotal_standard += (float) $cod_fee;
3095 + }
3096 + }
3097 +
3098 + if ( 'include' === $tax_mode ) {
3099 + if ( 0 < $subtotal_standard ) {
3100 + $tax_standard = (float) sprintf( '%.3f', ( (float) $subtotal_standard + (float) $discount_standard ) * $tax_rate / ( 100 + $tax_rate ) );
3101 + }
3102 + if ( 0 < $subtotal_reduced ) {
3103 + $tax_reduced = (float) sprintf( '%.3f', ( (float) $subtotal_reduced + (float) $discount_reduced ) * $tax_rate_reduced / ( 100 + $tax_rate_reduced ) );
3104 + }
3105 + } else {
3106 + if ( 0 < $subtotal_standard ) {
3107 + $tax_standard = (float) sprintf( '%.3f', ( (float) $subtotal_standard + (float) $discount_standard ) * $tax_rate / 100 );
3108 + }
3109 + if ( 0 < $subtotal_reduced ) {
3110 + $tax_reduced = (float) sprintf( '%.3f', ( (float) $subtotal_reduced + (float) $discount_reduced ) * $tax_rate_reduced / 100 );
3111 + }
3112 + }
3113 +
3114 + $tax_standard = usces_tax_rounding_off( $tax_standard, $tax_method );
3115 + $tax_reduced = usces_tax_rounding_off( $tax_reduced, $tax_method );
3116 +
3117 + $materials = compact( 'total_items_price', 'shipping_charge', 'discount', 'cod_fee', 'use_point', 'cart' );
3118 + if ( 'include' === $tax_mode ) {
3119 + $include_tax = $tax_standard + $tax_reduced;
3120 + $total_full_price = $total_price;
3121 + } else {
3122 + $tax = apply_filters( 'usces_filter_order_tax_recalculation', $tax_standard + $tax_reduced, $materials );
3123 + $total_full_price = $total_price + $tax;
3124 + }
3125 + } else {
3126 + $total_full_price = $total_price;
3127 + }
3128 + $total_full_price = apply_filters( 'usces_filter_set_cart_fees_total_full_price', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
3129 + $total_full_price = apply_filters( 'usces_filter_order_total_full_price_recalculation', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
3130 +
3131 + $data['status'] = $res;
3132 + $data['tax_mode'] = 'reduced';
3133 + $data['discount'] = $discount;
3134 + $data['tax'] = $tax;
3135 + $data['point'] = $point;
3136 + $data['total_full_price'] = $total_full_price;
3137 + $data['subtotal_standard'] = $subtotal_standard;
3138 + $data['subtotal_reduced'] = $subtotal_reduced;
3139 + //if ( 'change' == $change_taxrate ) {
3140 + $data['discount_standard'] = $discount_standard;
3141 + $data['discount_reduced'] = $discount_reduced;
3142 + //}
3143 + $data['tax_standard'] = $tax_standard;
3144 + $data['tax_reduced'] = $tax_reduced;
3145 + $data['include_tax'] = $include_tax;
3146 + wp_send_json( $data );
3147 +}
3148 +
3149 +/**
3150 + * Application of reduced tax rate
3151 + *
3152 + * @param array $sku SKU data.
3153 + * @return void
3154 + */
3155 +function usces_sku_meta_row_reduced_taxrate( $sku ) {
3156 + global $usces;
3157 +
3158 + if ( $usces->is_reduced_taxrate() ) :
3159 + $standard = $usces->options['tax_rate'];
3160 + $reduced = $usces->options['tax_rate_reduced'];
3161 + $taxrate = ( isset( $sku['taxrate'] ) ) ? $sku['taxrate'] : '';
3162 + ?>
3163 + <select id="itemsku[<?php echo esc_attr( $sku['meta_id'] ); ?>][applicable_taxrate]" name="itemsku[<?php echo esc_attr( $sku['meta_id'] ); ?>][applicable_taxrate]" class="sku_applicable_taxrate" >
3164 + <option value="standard"<?php selected( $taxrate, 'reduced' ); ?>><?php esc_html_e( 'Standard tax rate', 'usces' ); ?>(<?php echo esc_html( $standard ); ?>%)</option>
3165 + <option value="reduced"<?php selected( $taxrate, 'reduced' ); ?>><?php esc_html_e( 'Reduced tax rate', 'usces' ); ?>(<?php echo esc_html( $reduced ); ?>%)</option>
3166 + </select>
3167 + <?php
3168 + endif;
3169 +}
3170 +
3171 +/**
3172 + * Application of reduced tax rate
3173 + */
3174 +function usces_newsku_meta_row_reduced_taxrate() {
3175 + global $usces;
3176 +
3177 + if ( $usces->is_reduced_taxrate() ) :
3178 + $standard = $usces->options['tax_rate'];
3179 + $reduced = $usces->options['tax_rate_reduced'];
3180 + ?>
3181 + <select id="newsku_applicable_taxrate" name="newsku_applicable_taxrate" class="newsku_applicable_taxrate" >
3182 + <option value="standard"><?php esc_html_e( 'Standard tax rate', 'usces' ); ?>(<?php echo esc_html( $standard ); ?>%)</option>
3183 + <option value="reduced"><?php esc_html_e( 'Reduced tax rate', 'usces' ); ?>(<?php echo esc_html( $reduced ); ?>%)</option>
3184 + </select>
3185 + <?php
3186 + endif;
3187 +}
3188 +
3189 +/**
3190 + * When deleting product data from the trash bin, also delete "_item", "_sku", and "_opt".
3191 + *
3192 + * @param int $post_id Post ID.
3193 + */
3194 +function usces_delete_all_item_data( $post_id ) {
3195 + $action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_SPECIAL_CHARS );
3196 +
3197 + if ( ! empty( $action ) && 'delete' === $action && usces_is_item( $post_id ) ) {
3198 + wel_delete_all_sku_data( $post_id );
3199 + wel_delete_all_opt_data( $post_id );
3200 + wel_delete_item_data( $post_id );
3201 + }
3202 +}