PluginProbe
Welcart e-Commerce / 2.11.32
Welcart e-Commerce v2.11.32
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
usc-e-shop / includes / product / wel-item-functions.php

wel-item-functions.php in Welcart e-Commerce 2.11.32, at includes/product/wel-item-functions.php

656 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart Item Functions
4 *
5 * Functions for product related.
6 *
7 * @package Welcart
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Main function for returning products, uses the Welcart_Item class.
14 *
15 * @since 2.2.2
16 *
17 * @param mixed $the_item Post object or post ID of the item.
18 * @param boolean $cache Switch of cache.
19 * @return ProductData|null|false
20 */
21 function wel_get_product( $the_item = false, $cache = true ) {
22 $WelItem = new Welcart\ItemData( $the_item, $cache );
23 $product = $WelItem->get_product( $cache );
24 $post_id = $WelItem->get_id();
25 $product = apply_filters( 'wel_get_product', $product, $post_id );
26 return $product;
27 }
28
29 /**
30 * Function for returning products by item code.
31 *
32 * @since 2.2.2
33 *
34 * @param string $item_code Item code of the item.
35 * @param boolean $cache Switch of cache.
36 * @return ProductData|null|false
37 */
38 function wel_get_product_by_code( $item_code, $cache = true ) {
39 $WelItem = new Welcart\ItemData( null, $cache );
40 $post_id = $WelItem->get_id_by_item_code( $item_code, $cache );
41 $WelItem->set_id( $post_id );
42 $WelItem->set_data( $post_id, $cache );
43 $product = $WelItem->get_product( $cache );
44 $product = apply_filters( 'usces_filter_get_item', $product, $post_id );
45 return $product;
46 }
47
48 /**
49 * Function for returning Post ID by item code.
50 *
51 * @since 2.2.2
52 *
53 * @param string $item_code Item code of the item.
54 * @param boolean $cache Switch of cache.
55 * @return Post ID|null|false
56 */
57 function wel_get_id_by_item_code( $item_code, $cache = true ) {
58 $WelItem = new Welcart\ItemData( null, $cache );
59 $post_id = $WelItem->get_id_by_item_code( $item_code, $cache );
60 return $post_id;
61 }
62
63 /**
64 * The function for returning item data, uses the Welcart_Item class.
65 *
66 * @since 2.2.2
67 *
68 * @param mixed $the_item Post ID of the item.
69 * @param boolean $cache Switch of cache.
70 * @return itemData|null|false
71 */
72 function wel_get_item( $the_item, $cache = true ) {
73 $WelItem = new Welcart\ItemData( $the_item, $cache );
74 return $WelItem->get_item();
75 }
76
77 /**
78 * The function for returning sku data, uses the Welcart_Item class.
79 *
80 * @since 2.2.2
81 *
82 * @param mixed $the_item Post ID of the item.
83 * @param string $sku_code SKU code of the item.
84 * @param boolean $cache Switch of cache.
85 * @return skuData|null|false
86 */
87 function wel_get_sku( $the_item, $sku_code, $cache = true ) {
88 $WelItem = new Welcart\ItemData( $the_item, $cache );
89 return $WelItem->get_sku_by_code( $sku_code );
90 }
91
92 /**
93 * The function for returning sku data by meta_id, uses the Welcart_Item class.
94 *
95 * @since 2.6
96 *
97 * @param int $meta_id meta_id of the item.
98 * @param boolean $cache Switch of cache.
99 * @return skuData|null|false
100 */
101 function wel_get_sku_by_id( $meta_id, $cache = true ) {
102 global $wpdb;
103 $sku_table = $wpdb->prefix . 'usces_skus';
104
105 $res = $wpdb->get_row(
106 $wpdb->prepare(
107 "SELECT * FROM {$sku_table} WHERE meta_id = %d",
108 $meta_id
109 ),
110 ARRAY_A
111 );
112 if ( ! $res ) {
113 return false;
114 }
115
116 $WelItem = new Welcart\ItemData( $res['post_id'], $cache );
117 return $WelItem->get_sku_by_id( $meta_id );
118 }
119
120 /**
121 * The function for returning all sku data in a product, uses the Welcart_Item class.
122 *
123 * @since 2.3.2
124 *
125 * @param mixed $the_item Post object or post ID of the item.
126 * @param string $sortkey Key for sorting.
127 * @param boolean $cache Switch of cache.
128 * @return skuDatas|null|false
129 */
130 function wel_get_skus( $the_item = false, $sortkey = 'sort', $cache = true ) {
131 $WelItem = new Welcart\ItemData( $the_item, $cache );
132 $skus = $WelItem->get_skus( $sortkey );
133 $post_id = $WelItem->get_id();
134 $skus = apply_filters( 'usces_filter_get_skus', $skus, $post_id, $sortkey );
135 return $skus;
136 }
137
138 /**
139 * The function for returning all option data in a product, uses the Welcart_Item class.
140 *
141 * @since 2.3.2
142 *
143 * @param mixed $the_item Post object or post ID of the item.
144 * @param string $sortkey Key for sorting.
145 * @param boolean $cache Switch of cache.
146 * @return OptionDatas|null|false
147 */
148 function wel_get_opts( $the_item = false, $sortkey = 'sort', $cache = true ) {
149 $WelItem = new Welcart\ItemData( $the_item, $cache );
150 $opts = $WelItem->get_opts( $sortkey, $cache );
151 $post_id = $WelItem->get_id();
152 $opts = apply_filters( 'usces_filter_get_opts', $opts, $post_id, $sortkey );
153
154 return $opts;
155 }
156
157 /**
158 * The function for returning extra data in a product, uses the Welcart_Item class.
159 *
160 * @since 2.3.2
161 *
162 * @param mixed $the_item Post object or post ID of the item.
163 * @param boolean $cache Switch of cache.
164 * @return ExtraDatas|null|false
165 */
166 function wel_get_extra_data( $the_item = false, $cache = true ) {
167 $WelItem = new Welcart\ItemData( $the_item, $cache );
168 $extra = $WelItem->get_ext();
169 $post_id = $WelItem->get_id();
170 $extra = apply_filters( 'usces_filter_get_extra_data', $extra, $extra, $post_id );
171 return $extra;
172 }
173
174 /**
175 * Check if the item sku is out of stock.
176 *
177 * @since 2.2.2
178 *
179 * @param mixed $the_item Post object or post ID of the item.
180 * @param string $sku_code SKU code of the item.
181 * @param boolean $cache Switch of cache.
182 * @return boolean Return true if in stock, false otherwise.
183 */
184 function wel_has_stock( $the_item, $sku_code, $cache = true ) {
185 global $usces;
186
187 $WelItem = new Welcart\ItemData( $the_item, $cache );
188 $product = $WelItem->get_product( $cache );
189 $sku = $WelItem->get_sku_by_code( $sku_code );
190 $status = (int) $sku['stock'];
191 $stock = $sku['stocknum'];
192 $iOAp = $product['itemOrderAcceptable'];
193
194 if ( 1 !== $iOAp ) {
195
196 if ( false !== $stock
197 && ( 0 < (int) $stock || WCUtils::is_blank( $stock ) )
198 && false !== $status
199 && 2 > $status
200 ) {
201 $res = true;
202 } else {
203 $res = false;
204 }
205 } else {
206
207 if ( false !== $status && 2 > $status ) {
208 $res = true;
209 } else {
210 $res = false;
211 }
212 }
213
214 return $res;
215 }
216
217 /**
218 * The function for updating core data for the Item object.
219 * The data should consist only of the keys you want to update, not the ones you don't want to update.
220 * For details of data, refer to the member variable ($data) of ItemData class.
221 *
222 * @since 3.0.0
223 *
224 * @param array $data Core data associative array.
225 * @param int $post_id Post ID.
226 * @param bool $delete True if you want to delete before updating.
227 * @return true|false Execution result.
228 */
229 function wel_update_item_data( $data, $post_id, $delete = false ) {
230 $WelItem = new Welcart\ItemData( $post_id, false );
231 return $WelItem->update_item_data( $data, $delete );
232 }
233
234 /**
235 * The function for delete item data.
236 *
237 * @since 2.7
238 *
239 * @param int $post_id Post ID.
240 * @return boolean Result.
241 */
242 function wel_delete_item_data( $post_id ) {
243 $WelItem = new Welcart\ItemData( $post_id, false );
244 return $WelItem->delete_item_data();
245 }
246
247 /**
248 * The function for updating sku data by meta_id.
249 *
250 * @since 3.0.0
251 *
252 * @param array $meta_id Meta ID.
253 * @param int $post_id Post ID.
254 * @param int $sku SKU data associative array.
255 * @return true|false Execution result.
256 */
257 function wel_update_sku_data_by_id( $meta_id, $post_id, $sku ) {
258 $WelItem = new Welcart\ItemData( $post_id, false );
259 $sku['meta_id'] = $meta_id;
260 return $WelItem->update_sku_data( $sku );
261 }
262
263 /**
264 * The function for adding sku data.
265 *
266 * @since 3.0.0
267 *
268 * @param int $post_id Post ID.
269 * @param int $sku SKU data associative array.
270 * @return int New meta_id.
271 */
272 function wel_add_sku_data( $post_id, $sku ) {
273 $WelItem = new Welcart\ItemData( $post_id, false );
274 return $WelItem->add_sku_data( $sku );
275 }
276
277 /**
278 * The function for deleting sku data.
279 *
280 * @since 3.0.0
281 *
282 * @param int $meta_id Meta ID.
283 * @param int $post_id Post ID.
284 * @return boolean Result.
285 */
286 function wel_delete_sku_data_by_id( $meta_id, $post_id ) {
287 $WelItem = new Welcart\ItemData( $post_id, false );
288 return $WelItem->delete_sku_data( $meta_id );
289 }
290
291 /**
292 * The function for deleting all sku data in a product.
293 *
294 * @since 3.0.0
295 *
296 * @param int $post_id Post ID.
297 * @return boolean Result.
298 */
299 function wel_delete_all_sku_data( $post_id ) {
300 $WelItem = new Welcart\ItemData( $post_id, false );
301 return $WelItem->delete_all_sku_data();
302 }
303
304 /**
305 * The function for updating option data by meta_id.
306 *
307 * @since 3.0.0
308 *
309 * @param array $meta_id Meta ID.
310 * @param int $post_id Post ID.
311 * @param int $opt Option data associative array.
312 * @return true|false Execution result.
313 */
314 function wel_update_opt_data_by_id( $meta_id, $post_id, $opt ) {
315 $WelItem = new Welcart\ItemData( $post_id, false );
316 $opt['meta_id'] = $meta_id;
317 return $WelItem->update_opt_data( $opt );
318 }
319
320 /**
321 * The function for adding option data.
322 *
323 * @since 3.0.0
324 *
325 * @param int $post_id Post ID.
326 * @param int $opt Option data associative array.
327 * @return int New meta_id.
328 */
329 function wel_add_opt_data( $post_id, $opt ) {
330 $WelItem = new Welcart\ItemData( $post_id, false );
331 return $WelItem->add_opt_data( $opt );
332 }
333
334 /**
335 * The function for deleting option data.
336 *
337 * @since 3.0.0
338 *
339 * @param int $meta_id Meta ID.
340 * @param int $post_id Post ID.
341 * @return boolean Result.
342 */
343 function wel_delete_opt_data_by_id( $meta_id, $post_id ) {
344 $WelItem = new Welcart\ItemData( $post_id, false );
345 return $WelItem->delete_opt_data( $meta_id );
346 }
347
348 /**
349 * The function for deleting all option data in a product.
350 *
351 * @since 3.0.0
352 *
353 * @param int $post_id Post ID.
354 * @return boolean Result.
355 */
356 function wel_delete_all_opt_data( $post_id ) {
357 $WelItem = new Welcart\ItemData( $post_id, false );
358 return $WelItem->delete_all_opt_data();
359 }
360
361 /**
362 * Main product image id.
363 *
364 * @since 3.0.0
365 *
366 * @param int $item_code Item Code.
367 * @param boolean $cache Switch of cache.
368 * @return int Pict ID.
369 */
370 function wel_get_main_pict_id_by_code( $item_code, $cache = true ) {
371
372 $product = wel_get_product_by_code( $item_code, $cache );
373 $pctid = 0;
374
375 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
376
377 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
378
379 } else {
380
381 global $usces, $wpdb;
382
383 $cache_key = 'wel_main_pictid_by_code_' . $item_code;
384
385 $pctid = wp_cache_get( $cache_key );
386 if ( false === $pctid ) {
387 if ( ! empty( $item_code ) ) {
388 $pctid = $wpdb->get_var(
389 $wpdb->prepare(
390 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' LIMIT 1",
391 $item_code
392 )
393 );
394 if ( null !== $pctid ) {
395 wp_cache_set( $cache_key, $pctid );
396 } else {
397 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
398 }
399 }
400 }
401 if ( null === $pctid ) {
402 $pctid = false;
403 } else {
404 $pctid = (int) apply_filters( 'usces_filter_get_mainpictid', $pctid, $item_code );
405 }
406 }
407
408 return $pctid;
409 }
410
411 /**
412 * Sub product image ids.
413 *
414 * @since 3.0.0
415 *
416 * @param int $item_code Item Code.
417 * @param boolean $cache Switch of cache.
418 * @return array Pict IDs.
419 */
420 function wel_get_sub_pict_ids_by_code( $item_code, $cache = true ) {
421
422 $product = wel_get_product_by_code( $item_code, $cache );
423 $pctids = array();
424
425 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
426
427 if ( isset( $product['itemPicts'] ) ) {
428 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
429 array_shift( $product['itemPicts'] );
430 }
431 $pctids = $product['itemPicts'];
432 }
433
434 } else {
435
436 global $usces, $wpdb;
437
438 $cache_key = 'wel_sub_pictids_' . $item_code;
439
440 $pctids = wp_cache_get( $cache_key );
441 if ( false === $pctids ) {
442 if ( ! empty( $item_code ) ) {
443 if ( ! $usces->options['system']['subimage_rule'] ) {
444 $codestr = $wpdb->esc_like( $item_code ) . '-%';
445 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_title <> %s AND post_type = 'attachment' ORDER BY post_title", $codestr, $item_code );
446 } else {
447 $codestr = $wpdb->esc_like( $item_code ) . '--%';
448 $codestr2 = $wpdb->esc_like( $item_code ) . '\_\_%';
449 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_title LIKE %s OR post_title LIKE %s) AND post_type = 'attachment' ORDER BY post_title", $codestr, $codestr2 );
450 }
451 $pctids = $wpdb->get_col( $query );
452 if ( null !== $pctids ) {
453 wp_cache_set( $cache_key, $pctids );
454 } else {
455 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
456 array_shift( $product['itemPicts'] );
457 }
458 $pctids = $product['itemPicts'];
459 }
460 }
461 }
462 if ( null === $pctids ) {
463 $pctids = false;
464 } else {
465 $pctids = (array) apply_filters( 'usces_filter_get_pictids', $pctids, $item_code );
466 }
467 }
468
469 return $pctids;
470 }
471
472 /**
473 * Main product image id.
474 *
475 * @since 3.0.0
476 *
477 * @param int $post_id item id.
478 * @param boolean $cache Switch of cache.
479 * @return int Pict ID.
480 */
481 function wel_get_main_pict_id( $post_id, $cache = true ) {
482
483 if ( 0 === $post_id ) {
484 return false;
485 }
486
487 $product = wel_get_product( $post_id, $cache );
488 $item_code = isset( $product['itemCode'] ) ? $product['itemCode'] : '';
489 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
490
491 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
492 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
493
494 } else {
495
496 global $wpdb;
497
498 $cache_key = 'wel_main_pictid_by_code_' . $item_code;
499
500 $pctid = wp_cache_get( $cache_key );
501 if ( false === $pctid ) {
502 if ( ! empty( $item_code ) ) {
503 $pctid = $wpdb->get_var(
504 $wpdb->prepare(
505 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' LIMIT 1",
506 $item_code
507 )
508 );
509 if ( null !== $pctid && $cache ) {
510 wp_cache_set( $cache_key, $pctid );
511 }
512 }
513 }
514 if ( null === $pctid ) {
515 $pctid = false;
516 } else {
517 $pctid = (int) apply_filters( 'usces_filter_get_mainpictid', $pctid, $item_code );
518 }
519 }
520
521 return $pctid;
522 }
523
524 /**
525 * Sub product image ids.
526 *
527 * @since 3.0.0
528 *
529 * @param int $post_id Item id.
530 * @param boolean $cache Switch of cache.
531 * @return array Pict IDs.
532 */
533 function wel_get_sub_pict_ids( $post_id, $cache = true ) {
534
535 if ( 0 === $post_id ) {
536 return false;
537 }
538 $pctids = array();
539 $product = wel_get_product( $post_id, $cache );
540 $item_code = isset( $product['itemCode'] ) ? $product['itemCode'] : '';
541
542 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
543
544 if ( isset( $product['itemPicts'] ) ) {
545 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
546 array_shift( $product['itemPicts'] );
547 }
548 $pctids = $product['itemPicts'];
549 }
550
551 } else {
552
553 global $usces, $wpdb;
554
555 $cache_key = 'wel_sub_pictids_' . $item_code;
556
557 $pctids = wp_cache_get( $cache_key );
558 if ( false === $pctids ) {
559 if ( ! empty( $item_code ) ) {
560 if ( ! $usces->options['system']['subimage_rule'] ) {
561 $codestr = $wpdb->esc_like( $item_code ) . '-%';
562 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_title <> %s AND post_type = 'attachment' ORDER BY post_title", $codestr, $item_code );
563 } else {
564 $codestr = $wpdb->esc_like( $item_code ) . '--%';
565 $codestr2 = $wpdb->esc_like( $item_code ) . '\_\_%';
566 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_title LIKE %s OR post_title LIKE %s) AND post_type = 'attachment' ORDER BY post_title", $codestr, $codestr2 );
567 }
568 $pctids = $wpdb->get_col( $query );
569 if ( null !== $pctids && $cache ) {
570 wp_cache_set( $cache_key, $pctids );
571 }
572 }
573 }
574 if ( null === $pctids ) {
575 $pctids = false;
576 } else {
577 $pctids = (array) apply_filters( 'usces_filter_get_pictids', $pctids, $item_code );
578 }
579 }
580 return $pctids;
581 }
582
583 /**
584 * List all item pict ID by item post id.
585 *
586 * @param integer $post_id item post id.
587 * @param boolean $cache Switch of cache.
588 * @return array
589 */
590 function wel_get_item_pict_ids( $post_id, $cache = false ) {
591 $arr_pict_ids = array();
592 $main_pict_id = (int) wel_get_main_pict_id( $post_id, $cache );
593
594 if ( false !== $main_pict_id && 0 < $main_pict_id ) {
595 $arr_pict_ids[] = $main_pict_id;
596 }
597
598 $sub_item_pict_ids = wel_get_sub_pict_ids( $post_id, $cache );
599
600 if ( is_array( $sub_item_pict_ids ) && ! empty( $sub_item_pict_ids ) ) {
601 $arr_pict_ids = array_merge( $arr_pict_ids, $sub_item_pict_ids );
602 }
603
604 $result = array();
605 foreach ( $arr_pict_ids as $pict_id ) {
606 if ( 0 < $pict_id ) {
607 $result[] = (int) $pict_id;
608 }
609 }
610 return $result;
611 }
612
613 /**
614 * Get image id by title.
615 *
616 * @since 3.0.0
617 *
618 * @param integer $post_id item post id.
619 * @param string $title Image post_title.
620 * @param boolean $cache Switch of cache.
621 * @return array $pctid Pict ID.
622 */
623 function wel_pict_id_by_title( $post_id, $title, $cache = true ) {
624 if ( empty( $title ) ) {
625 return false;
626 }
627
628 global $usces, $wpdb;
629
630 $cache_key = 'wel_pict_id_by_title_' . $post_id . $title;
631
632 $pctid = wp_cache_get( $cache_key );
633 if ( false === $pctid ) {
634
635 $candidate = $wpdb->get_col(
636 $wpdb->prepare(
637 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' ORDER BY post_title",
638 $title
639 )
640 );
641 if ( ! empty( $candidate ) ) {
642 $item_ids = wel_get_item_pict_ids( $post_id, $cache );
643 foreach ( $candidate as $can ) {
644 if ( in_array( $can, $item_ids ) ) {
645 $pctid = $can;
646 break;
647 }
648 }
649 }
650 if ( false !== $pctid && $cache ) {
651 wp_cache_set( $cache_key, $pctid );
652 }
653 }
654 return $pctid;
655 }
656