forked from clifgriffin/shopp
/
product.php
executable file
·2557 lines (2270 loc) · 91.6 KB
/
product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Product API
*
* Plugin api for manipulating products in the catalog.
*
* @copyright Ingenesis Limited, June 30, 2011
* @license GNU GPL version 3 (or later) {@see license.txt}
* @package Shopp/API/Product
* @version 1.0
* @since 1.2
**/
defined( 'WPINC' ) || header( 'HTTP/1.1 403' ) & exit; // Prevent direct access
/**
* Comprehensive product creation through Product Developer API.
*
* This function will do everything needed for creating a product except
* attach product images and products. That is done in the Asset API. :)
* You should be able to build an importer from another system using this function.
*
* It is also possible to update an existing product (by passing the
* existing id as part of the $data array) or else you can alternatively
* use shopp_update_product() for that.
*
* @todo possibly remove the capability of passing in an id to update a product
*
* @api
* @since 1.2
*
* @param array $data (required) associative array structure containing a single product definition, see _validate_product_data for how this array is structured/validated.
* @return Product the created product object, or boolean false on a failure.
**/
function shopp_add_product ( $data = array() ) {
if ( empty($data) ) {
shopp_debug(__FUNCTION__ . " failed: Empty data parameter.");
return false;
}
$problems = _validate_product_data ( $data );
if ( ! empty($problems) ) {
shopp_debug("Problems detected: ".Shopp::object_r($problems));
return false;
}
$Product = new ShoppProduct();
// Set Product publish status
if ( isset($data['publish']) ) {
$Product->publish = _shopp_product_publish_date($data['publish']);
if ( $Product->publish > 0 ) $Product->status = 'future';
}
// Set Product name
if ( empty($data['name']) ) {
shopp_debug(__FUNCTION__ . " failed: Missing product name.");
}
$Product->name = $data['name'];
// Set Product slug
if ( ! empty($data['slug'])) $Product->slug = $data['slug'];
if (empty($Product->slug)) $Product->slug = sanitize_title($Product->name);
$Product->slug = wp_unique_post_slug($Product->slug, $Product->id, $Product->status, ShoppProduct::posttype(), 0);
$Product->updates($data, array('meta','categories','prices','tags', 'publish'));
$Product->save();
ShoppProduct::publishset(array($Product->id), $data['publish']['flag'] ? 'publish' : 'draft');
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Failure to create new Product object.");
return false;
}
// Product-wide settings
$Product->featured = ( isset($data['featured']) && true === $data['featured'] ? "on" : "off" );
$Product->variants = ( isset($data['variants']) ? "on" : "off" );
$Product->addons = ( isset($data['addons']) ? "on" : "off" );
if ( isset($data['packaging']) ) {
$packaging_set = shopp_product_set_packaging($Product->id, $data['packaging']);
if ( ! $packaging_set ) {
shopp_debug(__FUNCTION__ . " failed: Failure to set packaging setting.");
return false;
}
}
// Save Taxonomies
// Categories
if ( isset($data['categories']) && isset($data['categories']['terms']) ) {
$cats_set = shopp_product_add_categories ( $Product->id, $data['categories']['terms'] );
if ( ! $cats_set ) {
shopp_debug(__FUNCTION__ . " failed: Failure to add product categories to product.");
return false;
}
}
// Tags
if ( isset($data['tags']) && isset($data['tags']['terms']) ) {
$tags_set = shopp_product_add_tags ( $Product->id, $data['tags']['terms'] );
if ( ! $tags_set ) {
shopp_debug(__FUNCTION__ . " failed: Failure to add product tags to product.");
return false;
}
}
// Terms
if ( isset($data['terms']) && isset($data['terms']['terms']) && isset($data['terms']['taxonomy']) ) {
$terms_set = shopp_product_add_terms ( $Product->id, $data['terms']['terms'], $data['terms']['taxonomy'] );
if ( ! $terms_set ) {
shopp_debug(__FUNCTION__ . " failed: Failure to add product taxonomy terms to product.");
return false;
}
}
// Create Specs
if ( isset($data['specs']) ) {
$specs_set = shopp_product_set_specs ( $Product->id, $data['specs'] );
if ( ! $specs_set ) {
shopp_debug(__FUNCTION__ . " failed: Failure to add product specs to product.");
return false;
}
}
$subjects = array();
$prices = array();
// Create Prices
if ( isset($data['single']) ) {
if ( ! empty($data['single']) ) $subjects['product'] = array($data['single']);
} else if ( isset($data['variants']) ) { // Construct and Populate variants
if ( ! isset($data['variants']['menu']) || empty($data['variants']['menu']) ) {
shopp_debug(__FUNCTION__ . " failed: variants menu is empty.");
return false;
}
$new_variants = shopp_product_set_variant_options ( $Product->id, $data['variants']['menu'], false );
$pricekeys = $prices = array();
foreach ( $new_variants as $Price ) $prices[$Price->id] = $pricekeys[$Price->optionkey] = $Price;
if ( ! $prices ) {
shopp_debug(__FUNCTION__ . " failed: Unable to set variant options.");
return false;
}
$subjects['variants'] = $data['variants'];
}
// Create the "product" Price
$Price = new ShoppPrice();
$Price->label = __('Price & Delivery', 'Shopp');
$Price->context = 'product';
$Price->product = $Product->id;
if ( isset($subjects['variants']) ) $Price->type = 'N/A'; // disabled
$Price->save();
$prices[$Price->id] = $productprice = $Price;
// Create Addons
if ( isset($data['addons']) ) {
if ( ! isset($data['addons']['menu']) || empty($data['addons']['menu']) ) {
shopp_debug(__FUNCTION__ . " failed: addons menu is empty");
return false;
}
$new_addons = shopp_product_set_addon_options ( $Product->id, $data['addons']['menu'], false );
$addon_prices = array();
foreach ( $new_addons as $Addon ) $addon_prices[$Addon->id] = $Addon;
if ( ! $addon_prices ) {
shopp_debug(__FUNCTION__ . " failed: Unable to set addon options.");
return false;
}
$prices = $prices + $addon_prices;
$subjects['addons'] = $data['addons'];
}
$contexts = array( 'addons' => 'addon', 'product' => 'product', 'variants' => 'variant' );
foreach ( $subjects as $pricetype => $variants ) {
// apply settings for each priceline
foreach ( $variants as $key => $variant ) {
if ( ! is_numeric($key) ) continue;
$price = null;
if ( 'product' == $pricetype ) {
$price = $productprice->id;
} else {
// 'option' => 'array', // array option example: Color=>Blue, Size=>Small
if ( ! isset($variant['option']) || empty($variant['option']) ) {
shopp_debug(__FUNCTION__ . " failed: variant $key missing variant options.");
return false;
}
list( $optionkey, $options, $label, $mapping ) = $Product->optionmap( $variant['option'], $variants['menu'], ('variants' == $pricetype ? 'variant' : 'addon') );
if ( 'variants' == $pricetype && isset($pricekeys[$optionkey]) ) $price = $pricekeys[$optionkey]->id;
else {
// Find the correct Price
foreach ( $addon_prices as $index => $Price ) {
if ( $Price->options == $options && $Price->label == $label ) {
$price = $index;
break;
}
}
}
}
if ( null === $price || ! isset($prices[$price]) ) {
shopp_debug(__FUNCTION__ . " failed: Variant $key not valid for this option set.");
return false;
}
// modify each priceline
$prices[$price] = shopp_product_set_variant ( $prices[$price], $variant, $contexts[$pricetype] );
if ( ! $prices[$price] ) {
shopp_debug(__FUNCTION__ . " failed: Product variant setup failed.");
return false;
}
// save priceline settings
if ( isset($prices[$price]->settings) )
shopp_set_meta ( $prices[$price]->id, 'price', 'settings', $prices[$price]->settings );
// We have everything we need to complete this price line
$prices[$price]->save();
} //end variants foreach
} // end subjects foreach
// Reset rollup figures for prices.
$Product->resum();
// Calculates aggregate product stats
// foreach ( $prices as $Price ) {
// $Product->sumprice($Price);
// }
// Skeleton summary
$Summary = new ProductSummary();
$sum_props = array_keys($Summary->_datatypes);
// init default summary items
foreach ( $sum_props as $prop ) {
if ( ! isset($Product->$prop) ) $Product->$prop = NULL;
}
// Process pricing stats
$records = null;
foreach ( $prices as $Price ) {
$Product->pricing($records, $Price);
}
// Saves generated stats to the product summary
$Product->sumup();
return shopp_product($Product->id);
} // end function shopp_add_product
/**
* Allows the properties of an existing product to be updated.
*
* This only applies to "core properties". Prices, taxonomy terms and other attributes can be modified
* using other API functions that exist for those specific purposes.
*
* @since 1.3
* @param int $product (required) ShoppProduct object or ID
* @param array $data (required) associative array structure representing product properties
* @return Product the created product object, or boolean false on a failure.
**/
function shopp_update_product ( $product, $data = array() ) {
if ( empty($data) ) {
shopp_debug(__FUNCTION__ . " failed: revisions to the product definition must be passed.");
return false;
}
if ( is_object($product) && is_a($product, 'ShoppProduct') ) {
$Product = $product;
}
elseif ( ! ( $Product = shopp_product($product) ) ) {
shopp_debug(__FUNCTION__ . " failed: invalid product or product ID specified.");
return false;
}
$Product->updates($data);
$Product->save();
}
/**
* Duplicate a product
*
* @api
* @since 1.3
*
* @param mixed $product (required) the product id to load. Also possible to specify the name or slug. See the $load_by parameter.
* @param string $load_by (optional default=id) id for loading the product by id, name for loading by name, and slug for loading by slug
* @return Product The duplicated product object, false on failure
**/
function shopp_duplicate_product ( $product = false, $load_by = 'id' ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product, $load_by);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$original = $Product->id;
$Product->duplicate();
if ( $Product->id == $original ) {
shopp_debug(__FUNCTION__ . " failed: Unable to duplicate product $product.");
return false;
}
return $Product;
}
/**
* Publish a product (by default), schedule it or unpubblish it
*
* @api
* @since 1.3
*
* @param int $product (required) the product id to publish/unpublish
* @param bool $flag (optional default: true) true for publish, false for unpublish
* @param int $timestamp (optional) A UNIX timestamp via current_time('timestamp')
* @return bool true on success, false on failure
**/
function shopp_publish_product ( $product = false, $flag = true, $timestamp = false ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->status = 'draft';
$Product->publish = 0;
if ( $flag ) {
$Product->status = 'publish';
$Product->publish = null;
if ( $timestamp && $timestamp > $Product->publish ) {
$Product->publish = $timestamp;
$Product->status = 'future';
}
}
$Product->save();
return true;
}
/**
* Remove a product
*
* @api
* @since 1.2
*
* @param int $product the product id
* @return bool true on success, false on failure
**/
function shopp_rmv_product ( $product = false ) {
if ( ! $product || ! ( $Product = shopp_product($product) ) ) return false;
$Product->delete();
return true;
}
/**
* @ignore Helper function for shopp_add_product that can be called recursively to validate the associative data array needed to build a product object.
* @since 1.2
*
* @param array $data the associative array being used to build the product object
* @param string $types (optional default:data) Sets the _type that will be evaluated for proper types. $_data is the top level, and each non-built in type is described
* in subsequent _type arrays
* @param array $problems array of problems that have been found in the data through recursive calls
* @return array list of problems with the data preventing proper product object construction
**/
function _validate_product_data ( $data, $types = 'data', $problems = array() ) {
$t = '_'.$types;
if ( ! is_array($data) ) {
$problems["$types must be an array."] = true;
return $problems;
}
// data properties needed to populate a product
$_data = array(
'name' => 'string', // string - the product name
'slug' => 'string', // string - the product slug (optional)
'publish' => 'publish', // array - flag => bool, publishtime => array(month => int, day => int, year => int, hour => int, minute => int, meridian => AM/PM)
'categories' => 'terms', // array of shopp category terms
'tags' => 'terms', // array of shopp tag terms
'terms' => 'terms', // array of taxonomy_type => type, terms => array of terms
'description' => 'string', // string - the product description text
'summary' => 'string', // string - the product summary text
'specs' => 'array', // array - spec name => spec value pairs
'single' => 'variant', // array - single variant
'variants' => 'variants', // array - menu => options, count => # of variants, 0-# => variant
'addons' => 'variants', // array of addon arrays
'featured' => 'bool', // bool - product flag
'packaging' => 'bool', // bool - packaging flag
'processing' => 'processing'// array - flag => bool, min => days, max => days)
);
$_publish = array(
'flag' => 'bool', // bool - publish or not
'publishtime' => 'timestamp'// array - array(month => int, day => int, year => int, hour => int, minute => int, meridian => AM/PM)
);
$_timestamp = array(
'month' => 'int', // int - month
'day' => 'int', // int - day
'year' => 'int', // int - year
'hour' => 'int', // int - hour
'minute' => 'int', // int - minute
'meridian' => 'enum' // array (AM, PM)
);
$_meridian = array('AM', 'PM');
$_terms = array(
'terms' => 'array', // array of terms
'taxonomy' => 'string' // string - name of taxonomy (not needed for categories and tags)
);
// variants structure
$_variants = array(
'menu' => 'array', // two dimensional array creates option permutations
// examples:
// $option['Color']['Blue']
// $option['Color']['Red]
// $option['Size']['Large']
// $option['Size']['Small']
'count' => 'int', // Number of variants
'#' => 'variant' // number indexed elements are each a variant
);
// single/variant/addon structure
$_variant = array(
'option' => 'array', // array option example: Color=>Blue, Size=>Small
'type' => 'enum', // string - Shipped, Virtual, Download, Donation, Subscription, Disabled ( ShoppPrice::types() )
'taxed' => 'bool', // bool - flag variant as taxable
'price' => 'float', // float - Price of variant
'sale' => 'sale', // array - flag => bool, price => Sale price of variant
'shipping' => 'shipping', // array - flag => bool, fee, weight, height, width, length
'inventory'=> 'inventory', // array - flag => bool, stock, sku
'donation'=> 'donation', // (optional - needed only for Donation type) array of settings (variable, minumum)
'subscription'=>'subscription' // (optional - needed only for Subscription type) array of subscription settings
);
// order processing estimate
$_processing = array(
'flag'=>'bool', // bool - processing time setting on/off
'min'=>'cycle', // array('interval'=># of time units, 'period'=> one of d, w, m, y)
'max'=>'cycle' // array('interval'=># of time units, 'period'=> one of d, w, m, y)
);
// variant types
$_types = ShoppPrice::types();
$_type = array();
foreach ( $_types as $type ) {
$_type[] = $type['value'];
}
// sale price
$_sale = array(
'flag' => 'bool', // sale price on/off
'price' => 'float' // sale price
);
// variant shipping settings
$_shipping = array(
'flag'=>'bool', // bool - charge shipping on or off
'fee'=>'float', // float - shipping fee for variant
'weight'=>'float', // float - weight of variant
'height'=>'float', // float - height of variant
'width'=>'float', // float - width of variatn
'length'=>'float' // float - length of variant
);
// variant inventory settings
$_inventory = array(
'flag' => 'bool', // bool - inventory settings on/off
'stock' => 'int', // int - stock level
'sku' => 'string' // sku - stock keeping unit label
);
// variant donation settings
$_donation = array(
'variable' => 'bool', // bool - variable prices allowed
'minimum' => 'bool' // bool - price is the minimum allowed
);
// variant subscription settings
$_subscription = array(
'trial' => 'trial',
'billcycle' => 'billcycle'
);
// subscriptions billing cycle
$_billcycle = array(
'cycle' => 'cycle', // billing cycle
'cycles' => 'int' // number of cycles
);
// subscription trial settings
$_trial = array(
'cycle' => 'cycle', // trial cycle
'price' => 'float' // price during trial
);
// time cycles
$_cycle = array(
'interval' => 'int', // int number of units
'period' => 'enum' // string d for day, w for week, m for month, y for year
);
$_periods = ShoppPrice::periods();
$_period = array();
foreach ( $_periods[0] as $p ) $_period[] = $p['value'];
$known_types = array( 'int' => 'is_numeric', 'float' => 'is_numeric', 'bool' => 'is_bool', 'string' => 'is_string', 'array' => 'is_array' );
foreach ( $data as $key => $value ) {
if ( is_numeric($key) && 'variants' == $types ) {
$key = '#';
}
$k = '_'.$key;
$recurse = ${$t}[$key];
$r = '_'.$recurse;
if ( in_array(${$t}[$key], array_keys($known_types) ) ) { // check known types first
if ( ! $known_types[${$t}[$key]]( $value ) ) {
if ( ! isset($problems['type mismatch']) ) $problems['type mismatch'] = array();
if ( ! isset($problems['type mismatch'][$types]) ) $problems['type mismatch'][$types] = array();
$problems['type mismatch'][$types][$key] = ${$t}[$key];
}
} else if ( 'enum' == ${$t}[$key] && ! in_array( $value, $$k) ) { // check enumerated types
if ( ! isset($problems['out of range']) ) $problems['out of range'] = array();
if ( ! isset($problems['out of range'][$types]) ) $problems['out of range'][$types] = array();
$problems['out of range'][$types][$key] = implode(', ', $$k);
} else if ( isset($$r) ) { // recurse into provided data structure, and validate
$problems = _validate_product_data($value, $recurse, $problems);
} else if ( ! in_array($key, array_keys($$t) ) ) { // unknown data type
if ( ! isset($problems['unknown data type']) ) $problems['unknown data type'] = array();
if ( ! isset($problems['unknown data type'][$types]) ) $problems['unknown data type'][$types] = array();
$problems['unknown data type'][$types][] = $key;
}
if ( 'single' == $key && isset($data['variants']) || 'variants' == $key && isset($data['single']) ) {
$problems['both single and variant price definitions detected'] = true;
}
}
return $problems;
}
// Product-wide getters
/**
* Retrieve a Shopp product by id
*
* @api
* @since 1.2
*
* @param mixed $product (required) the product id to load. Also possible to specify the name or slug. See the $load_by parameter.
* @param string $load_by (optional default=id) id for loading the product by id, name for loading by name, and slug for loading by slug
* @return Product a product object, false on failure
**/
function shopp_product ( $product = false, $load_by = 'id' ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product, $load_by);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->load_data();
return $Product;
}
/**
* Publish a product (by default), schedule it or unpubblish it
*
* @deprecated Used shopp_publish_product()
* @since 1.3
*
* @param int $product (required) the product id to publish/unpublish
* @param bool $flag (optional default: true) true for publish, false for unpublish
* @param int $datetime (optional) A UNIX timestamp via current_time('timestamp')
* @return bool true on success, false on failure
**/
function shopp_product_publish ( $product = false, $flag = false, $datetime = false ) {
shopp_debug(__FUNCTION__ . " has been deprecated. Use shopp_publish_product() instead.");
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->status = 'draft';
$Product->publish = 0;
if ( $flag ) {
$Product->status = 'publish';
$Product->publish = null;
if ( $datetime && $datetime > $Product->publish ) {
$Product->publish = $datetime;
$Product->status = 'future';
}
}
$Product->save();
return true;
}
/**
* Get a list of the product specs for a given product
*
* @api
* @since 1.2
*
* @param int $product product id to load
* @return array array of product specs, bool false on failure
**/
function shopp_product_specs ( $product = false ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Specs = shopp_product_meta ( $product, false, 'spec' );
foreach ( $Specs as $id => $spec) {
$Specs[$spec->name] = $spec;
unset($Specs[$id]);
}
return ! empty($Specs) ? $Specs : array();
}
/**
* Get a list of variants for the product
*
* @api
* @since 1.2
*
* @param int $product the product id to get the variants for
* @param string $load_by The record column to use to find the product
* @return array of variant ShoppPrice objects, empty array if no variants, false on error
**/
function shopp_product_variants ( $product = false, $load_by = 'id' ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product,$load_by);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->load_data(array('prices'));
$prices = array();
foreach( $Product->prices as $Price ) {
if ( 'variation' != $Price->context ) continue;
$prices[] = $Price;
}
return $prices;
}
/**
* Get a list of addons for the product
*
* @api
* @since 1.2
*
* @param int $product the product id to get the addons for
* @param string $load_by The record column to use to find the product
* @return array of addon ShoppPrice objects, empty array if no addons, false on error
**/
function shopp_product_addons ( $product = false, $load_by = 'id' ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product,$load_by);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->load_data(array('prices','summary'));
$prices = array();
foreach( $Product->prices as $Price ) {
if ( 'addon' != $Price->context ) continue;
$prices[] = $Price;
}
return $prices;
}
/**
* Determines if the specified addon (which should be a numeric ID) belongs to the specified product.
*
* @param int $product the product id to get the addons for
* @param int $addon The ShoppPrice addon id to match
* @return bool
*/
function shopp_product_has_addon ( $product = false, $addon ) {
if ( false === ( $addons = shopp_product_addons($product) ) ) {
return false; // Debug message already created in shopp_product_addons()
}
foreach ( $addons as $Addon ) if ( (int) $addon === (int) $Addon->id ) return true;
return false;
}
/**
* Get a specific Price object
*
* @api
* @since 1.2
*
* @param mixed $variant the id of the variant, or array('product'=>int, 'option' => array('menu1name'=>'option', 'menu2name'=>'option') ) to specify variant by product id and option
* @param string $pricetype (optional default:variant) product, variant, or addon
* @return ShoppPrice Price object or false on error
**/
function shopp_product_variant ( $variant = false, $pricetype = 'variant' ) {
if ( false === $variant ) {
shopp_debug(__FUNCTION__ . " failed: Variant id required.");
return false;
}
if ( is_numeric($variant) ) {
$Price = new ShoppPrice($variant);
if ( empty($Price->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load variant $variant.");
return false;
}
} else if ( is_array($variant) ) { // specifying variant by product id and option
$Product = new stdClass;
if ( isset($variant['product']) && is_numeric($variant['product']) ) {
$Product = new ShoppProduct($variant['product']);
$Product->load_data(array('prices','meta','summary'));
}
if ( empty($Product->id) || empty($Product->prices) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load variant. Invalid Product.");
return false;
}
$pricetype = ($pricetype == 'variant' ? 'variation' : $pricetype);
$pricetypes = array('product', 'variation', 'addon');
if ( ! in_array($pricetype, $pricetypes) ) {
shopp_debug(__FUNCTION__ . " failed: Invalid pricetype. Can be product, variant, or addon.");
return false;
}
if ( 'product' == $pricetype ) {
// No product context for product with variants
if ( 'on' == $Product->variants ) {
shopp_debug(__FUNCTION__ . " failed: Invalid pricetype for this product.");
return false;
}
foreach ( $Product->prices as $price ) {
if ( 'product' == $price->context ) {
$Price = new ShoppPrice();
$Price->populate($price);
$Price->load_settings();
$Price->load_download();
break;
}
}
} else { // addon or variant
if ( ! isset($variant['option']) || ! is_array($variant['option']) || empty($variant['option']) ) {
shopp_debug(__FUNCTION__ . " failed: Missing option array.");
return false;
}
$menukey = substr($pricetype, 0, 1);
$flag = ($pricetype == 'variation' ? 'variants' : 'addons');
if ( ! isset($Product->options[$menukey]) || $Product->$flag == 'off' ) {
shopp_debug(__FUNCTION__ . " failed: No product variant options of type $pricetype for product {$Product->id}");
return false;
}
// build simple option menu array
$menu = array();
foreach ( $Product->options[$menukey] as $optionmenu ) {
$key = $optionmenu['name'];
$menu[$key] = array();
foreach ( $optionmenu['options'] as $option ) {
$menu[$key][] = $option['name'];
}
}
list( $optionkey, $options, $label, $mapping ) = $Product->optionmap( $variant['option'], $menu , $pricetype );
if ( 'variation' == $pricetype && ! isset($Product->pricekey[$optionkey]) || ! $options ) {
shopp_debug(__FUNCTION__ . " failed: Invalid option.");
return false;
}
if ( 'variation' == $pricetype ) $price = $Product->pricekey[$optionkey];
else {
// Find the option
foreach ( $Product->prices as $price ) {
if ( $price->context == $pricetype && $price->options == $options ) {
break;
}
}
}
$Price = new Price;
$Price->populate($price);
$Price->load_settings();
$Price->load_download();
} // end if product type / addon/variants type
}
if ( ! isset($Price) ) {
shopp_debug(__FUNCTION__ . " failed: Product, Variant, or Addon Price object could not be found.");
return false;
}
return $Price;
} // end shopp_product_variant
/**
* shopp_product_variant_to_item
*
* Convert a variant Price object to an Item object
*
* @api
* @since 1.2
*
* @param ShoppPrice $Variant a product or variant Price object to create the item from.
* @param int $quantity (optional default:1) quantity of the variant the Item object will represent
* @return Item|bool Item object on success, false on failure
**/
function shopp_product_variant_to_item ( $Variant, $quantity = 1 ) {
$quantity = (int) $quantity;
if ( ! $quantity ) $quantity = 1;
if ( is_object($Variant) && is_a($Variant, 'ShoppPrice') && $Variant->product && $Variant->id && in_array($Variant->context, array('product', 'variation')) ) {
$Product = shopp_product( $Variant->product );
$Item = new Item( $Product, $Variant->id );
$Item->quantity($quantity);
return $Item;
}
shopp_debug(__FUNCTION__ . " failed: Variant object missing or invalid.");
return false;
}
/**
* shopp_product_addon - get a specific addon Price object.
*
* @api
* @since 1.2
*
* @param mixed $addon the id of the addon, or array('product'=>int, 'option' => array('addonmenu'=>'optionname') ) to specify addon by product id and option
* @return ShoppPrice The ShoppPrice object of the addon or false on error
**/
function shopp_product_addon ( $addon = false ) {
return shopp_product_variant( $addon, 'addon' );
}
/**
* shopp_product_variant_options - get an associative array of the option types keys and array of options associated with a product
*
* @api
* @since 1.2
*
* @param int $product (required) product id of the product to retrieve the options for
* @return array of options, false on error or non-variant product
**/
function shopp_product_variant_options ( $product = false ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->load_data(array('summary'));
if ( "off" == $Product->variants ) return false;
$meta = shopp_product_meta($product, 'options');
$v = $meta['v'];
$options = array();
foreach ( $v as $menus ) {
$options[$menus['name']] = array();
foreach ( $menus['options'] as $option ) {
$options[$menus['name']][] = $option['name'];
}
}
return $options;
}
/**
* shopp_product_addon_options - get an associative array of the addon option groups and array of associated addon options for a product
*
* @api
* @since 1.2
*
* @param int $product (required) product id of the product to retrieve the addon options for
* @return array of options, false on error or product without addon options
**/
function shopp_product_addon_options ( $product = false ) {
if ( false === $product ) {
shopp_debug(__FUNCTION__ . " failed: Product id required.");
return false;
}
$Product = new ShoppProduct($product);
if ( empty($Product->id) ) {
shopp_debug(__FUNCTION__ . " failed: Unable to load product $product.");
return false;
}
$Product->load_data(array('summary'));
if ( "off" == $Product->addons ) return false;
$meta = shopp_product_meta($product, 'options');
$a = $meta['a'];
$options = array();
foreach ( $a as $menus ) {
$options[$menus['name']] = array();
foreach ( $menus['options'] as $option ) {
$options[$menus['name']][] = $option['name'];
}
}
return $options;
}
// Product-wide setters/mutators
/**
* shopp_product_add_categories - add shopp product categories to a product
*
* @api
* @since 1.2
*
* @param int $product (required) Product id to add the product categories to.
* @param array $categories array of integer category term ids to add the product to. Names are not unique.
* @return bool true for success, false otherwise
**/
function shopp_product_add_categories ( $product = false, $categories = array() ) {
return shopp_product_add_terms( $product, $categories, ProductCategory::$taxon );
}
/**
* shopp_product_add_tags - add shopp product tags to a product
*
* @api
* @since 1.2
*
* @param int $product (required) Product id to add the product tags to.
* @param array $tags array of tags/(tag ids) to add to the product
* @return bool true for success, false otherwise
**/
function shopp_product_add_tags ( $product = false, $tags = array() ) {
return shopp_product_add_terms( $product, $tags, ProductTag::$taxon );
}
/**
* shopp_product_add_terms - add/set taxonomical terms to a product
*
* @api