-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.php
8359 lines (7356 loc) · 300 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
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
// Deprecated since 1.5.0.1 use Product::CUSTOMIZE_FILE
define('_CUSTOMIZE_FILE_', 0);
// Deprecated since 1.5.0.1 use Product::CUSTOMIZE_TEXTFIELD
define('_CUSTOMIZE_TEXTFIELD_', 1);
use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
use PrestaShop\PrestaShop\Core\Domain\Product\ProductSettings;
use PrestaShop\PrestaShop\Core\Domain\Product\Stock\ValueObject\OutOfStockType;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\Ean13;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\Isbn;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductType;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\RedirectType;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\Reference;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\Upc;
use PrestaShop\PrestaShop\Core\Product\ProductInterface;
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;
class ProductCore extends ObjectModel
{
/**
* @var string Tax name
*
* @deprecated Since 1.4
*/
public $tax_name;
/** @var float Tax rate */
public $tax_rate;
/** @var int Manufacturer identifier */
public $id_manufacturer;
/** @var int Supplier identifier */
public $id_supplier;
/** @var int default Category identifier */
public $id_category_default;
/** @var int default Shop identifier */
public $id_shop_default;
/** @var string Manufacturer name */
public $manufacturer_name;
/** @var string Supplier name */
public $supplier_name;
/** @var string|array Name or array of names by id_lang */
public $name;
/** @var string|array Long description or array of long description by id_lang */
public $description;
/** @var string|array Short description or array of short description by id_lang */
public $description_short;
/**
* @deprecated since 1.7.8
* @see StockAvailable::$quantity instead
*
* @var int Quantity available
*/
public $quantity = 0;
/** @var int Minimal quantity for add to cart */
public $minimal_quantity = 1;
/** @var int|null Low stock for mail alert */
public $low_stock_threshold = null;
/** @var bool Low stock mail alert activated */
public $low_stock_alert = false;
/** @var string|array Text when in stock or array of text by id_lang */
public $available_now;
/** @var string|array Text when not in stock but available to order or array of text by id_lang */
public $available_later;
/** @var float Price */
public $price = 0;
/** @var array|int|null Will be filled by reference by priceCalculation() */
public $specificPrice = 0;
/** @var string Additional shipping cost */
public $additional_shipping_cost = 0;
/** @var string Wholesale Price in euros */
public $wholesale_price = 0;
/** @var bool on_sale */
public $on_sale = false;
/** @var bool online_only */
public $online_only = false;
/** @var string unity */
public $unity = null;
/** @var float price for product's unity */
public $unit_price = 0;
/** @var float price for product's unity ratio */
public $unit_price_ratio = 0;
/** @var float Ecotax */
public $ecotax = 0;
/** @var string Reference */
public $reference;
/**
* @var string Supplier Reference
*
* @deprecated since 1.7.7.0
*/
public $supplier_reference;
/**
* @deprecated since 1.7.8
* @see StockAvailable::$location instead
*
* @var string Location
*/
public $location = '';
/** @var string|float Width in default width unit */
public $width = 0;
/** @var string|float Height in default height unit */
public $height = 0;
/** @var string|float Depth in default depth unit */
public $depth = 0;
/** @var string|float Weight in default weight unit */
public $weight = 0;
/** @var string Ean-13 barcode */
public $ean13;
/** @var string ISBN */
public $isbn;
/** @var string Upc barcode */
public $upc;
/** @var string MPN */
public $mpn;
/** @var string|array Friendly URL or array of friendly URL by id_lang */
public $link_rewrite;
/** @var string|array Meta description or array of meta description by id_lang */
public $meta_description;
/**
* @deprecated
*/
public $meta_keywords;
/** @var string|array Meta title or array of meta title by id_lang */
public $meta_title;
/**
* @var mixed
*
* @deprecated Unused
*/
public $quantity_discount = 0;
/** @var bool|int Product customization */
public $customizable;
/** @var bool|null Product is new */
public $new = null;
/** @var int Number of uploadable files (concerning customizable products) */
public $uploadable_files;
/** @var int Number of text fields */
public $text_fields;
/** @var bool Product status */
public $active = true;
/**
* @var string Redirection type
*
* @see RedirectType
*/
public $redirect_type = RedirectType::TYPE_NOT_FOUND;
/**
* @var int Product identifier or Category identifier depends on redirect_type
*/
public $id_type_redirected = 0;
/** @var bool Product available for order */
public $available_for_order = true;
/** @var string Available for order date in mysql format Y-m-d */
public $available_date = DateTimeUtil::NULL_DATE;
/** @var bool Will the condition select should be visible for this product ? */
public $show_condition = false;
/** @var string Enumerated (enum) product condition (new, used, refurbished) */
public $condition;
/** @var bool Show price of Product */
public $show_price = true;
/** @var bool is the product indexed in the search index? */
public $indexed = 0;
/** @var string ENUM('both', 'catalog', 'search', 'none') front office visibility */
public $visibility;
/** @var string Object creation date in mysql format Y-m-d H:i:s */
public $date_add;
/** @var string Object last modification date in mysql format Y-m-d H:i:s */
public $date_upd;
/** @var array Tags data */
public $tags;
/** @var int temporary or saved object */
public $state = self::STATE_SAVED;
/**
* @var float Base price of the product
*
* @deprecated 1.6.0.13
*/
public $base_price;
/**
* @var int TaxRulesGroup identifier
*/
public $id_tax_rules_group;
/**
* @var int
* We keep this variable for retrocompatibility for themes
*
* @deprecated 1.5.0
*/
public $id_color_default = 0;
/**
* @deprecated since 1.7.8
* The advanced stock management feature is not maintained anymore
*
* @var bool Tells if the product uses the advanced stock management
*/
public $advanced_stock_management = 0;
/**
* @deprecated since 1.7.8
* @see StockAvailable::$out_of_stock instead
*
* @var int
* - O Deny orders
* - 1 Allow orders
* - 2 Use global setting
*/
public $out_of_stock = OutOfStockType::OUT_OF_STOCK_DEFAULT;
/**
* @deprecated since 1.7.8
* This property was only relevant to advanced stock management and that feature is not maintained anymore
*
* @var bool
*/
public $depends_on_stock;
/**
* @var bool
*/
public $isFullyLoaded = false;
/**
* @var bool
*/
public $cache_is_pack;
/**
* @var bool
*/
public $cache_has_attachments;
/**
* @var bool
*/
public $is_virtual;
/**
* @var int
*/
public $id_pack_product_attribute;
/**
* @var int
*/
public $cache_default_attribute;
/**
* @var string|string[] If product is populated, this property contain the rewrite link of the default category
*/
public $category;
/**
* @var int tell the type of stock management to apply on the pack
*/
public $pack_stock_type = Pack::STOCK_TYPE_DEFAULT;
/**
* Type of delivery time.
*
* Choose which parameters use for give information delivery.
* 0 - none
* 1 - use default information
* 2 - use product information
*
* @var int
*/
public $additional_delivery_times = 1;
/**
* Delivery in-stock information.
*
* Long description for delivery in-stock product information.
*
* @var string[]
*/
public $delivery_in_stock;
/**
* Delivery out-stock information.
*
* Long description for delivery out-stock product information.
*
* @var string[]
*/
public $delivery_out_stock;
/**
* For now default value remains undefined, to keep compatibility with page v1 and former products.
* But once the v2 is merged the default value should be ProductType::TYPE_STANDARD
*
* @var string
*/
public $product_type = ProductType::TYPE_UNDEFINED;
/**
* @var int|null
*/
public static $_taxCalculationMethod = null;
/** @var array Price cache */
protected static $_prices = [];
/** @var array */
protected static $_pricesLevel2 = [];
/** @var array */
protected static $_incat = [];
/** @var array */
protected static $_combinations = [];
/**
* @deprecated Since 1.5.6.1
*
* @var array
*/
protected static $_cart_quantity = [];
/**
* @deprecated Since 1.5.0.9
*
* @var array
*/
protected static $_tax_rules_group = [];
/** @var array */
protected static $_cacheFeatures = [];
/** @var array */
protected static $_frontFeaturesCache = [];
/** @var array */
protected static $productPropertiesCache = [];
/**
* @deprecated Since 1.5.0.1 Unused
*
* @var array cache stock data in getStock() method
*/
protected static $cacheStock = [];
/**
* Product can be temporary saved in database
*/
const STATE_TEMP = 0;
const STATE_SAVED = 1;
/**
* @var array Contains object definition
*
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'multilang_shop' => true,
'fields' => [
/* Classic fields */
'id_shop_default' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'id_manufacturer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'id_supplier' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'reference' => ['type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => Reference::MAX_LENGTH],
'supplier_reference' => ['type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64],
'location' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 255],
'width' => ['type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'],
'height' => ['type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'],
'depth' => ['type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'],
'weight' => ['type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'],
'quantity_discount' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'ean13' => ['type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => Ean13::MAX_LENGTH],
'isbn' => ['type' => self::TYPE_STRING, 'validate' => 'isIsbn', 'size' => Isbn::MAX_LENGTH],
'upc' => ['type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => Upc::MAX_LENGTH],
'mpn' => ['type' => self::TYPE_STRING, 'validate' => 'isMpn', 'size' => ProductSettings::MAX_MPN_LENGTH],
'cache_is_pack' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'cache_has_attachments' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'is_virtual' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'state' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'additional_delivery_times' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'delivery_in_stock' => [
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isGenericName',
'size' => 255,
],
'delivery_out_stock' => [
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isGenericName',
'size' => 255,
],
'product_type' => [
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
// For now undefined value is still allowed, in 179 we should use ProductType::AVAILABLE_TYPES here
'values' => [
ProductType::TYPE_STANDARD,
ProductType::TYPE_PACK,
ProductType::TYPE_VIRTUAL,
ProductType::TYPE_COMBINATIONS,
ProductType::TYPE_UNDEFINED,
],
// This default value should be replaced with ProductType::TYPE_STANDARD in 179 when the v2 page is fully migrated
'default' => ProductType::TYPE_UNDEFINED,
],
/* Shop fields */
'id_category_default' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'],
'id_tax_rules_group' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'],
'on_sale' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'online_only' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'ecotax' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'],
'minimal_quantity' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'],
'low_stock_threshold' => ['type' => self::TYPE_INT, 'shop' => true, 'allow_null' => true, 'validate' => 'isInt'],
'low_stock_alert' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'price' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'required' => true],
'wholesale_price' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'],
'unity' => ['type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'],
'unit_price_ratio' => ['type' => self::TYPE_FLOAT, 'shop' => true],
'additional_shipping_cost' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'],
'customizable' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'],
'text_fields' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'],
'uploadable_files' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'],
'active' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'redirect_type' => ['type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'],
'id_type_redirected' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'],
'available_for_order' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'available_date' => ['type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'],
'show_condition' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'condition' => ['type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isGenericName', 'values' => ['new', 'used', 'refurbished'], 'default' => 'new'],
'show_price' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'indexed' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'visibility' => ['type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isProductVisibility', 'values' => ['both', 'catalog', 'search', 'none'], 'default' => 'both'],
'cache_default_attribute' => ['type' => self::TYPE_INT, 'shop' => true],
'advanced_stock_management' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
'date_add' => ['type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDate'],
'pack_stock_type' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'],
/* Lang fields */
'meta_description' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 512],
'meta_keywords' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255],
'meta_title' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255],
'link_rewrite' => [
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isLinkRewrite',
'required' => false,
'size' => 128,
'ws_modifier' => [
'http_method' => WebserviceRequest::HTTP_POST,
'modifier' => 'modifierWsLinkRewrite',
],
],
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => false, 'size' => ProductSettings::MAX_NAME_LENGTH],
'description' => ['type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'],
'description_short' => ['type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'],
'available_now' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255],
'available_later' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'IsGenericName', 'size' => 255],
],
'associations' => [
'manufacturer' => ['type' => self::HAS_ONE],
'supplier' => ['type' => self::HAS_ONE],
'default_category' => ['type' => self::HAS_ONE, 'field' => 'id_category_default', 'object' => 'Category'],
'tax_rules_group' => ['type' => self::HAS_ONE],
'categories' => ['type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'],
'stock_availables' => ['type' => self::HAS_MANY, 'field' => 'id_stock_available', 'object' => 'StockAvailable', 'association' => 'stock_availables'],
'attachments' => ['type' => self::HAS_MANY, 'field' => 'id_attachment', 'object' => 'Attachment', 'association' => 'product_attachment'],
],
];
/** @var array */
protected $webserviceParameters = [
'objectMethods' => [
'add' => 'addWs',
'update' => 'updateWs',
],
'objectNodeNames' => 'products',
'fields' => [
'id_manufacturer' => [
'xlink_resource' => 'manufacturers',
],
'id_supplier' => [
'xlink_resource' => 'suppliers',
],
'id_category_default' => [
'xlink_resource' => 'categories',
],
'new' => [],
'cache_default_attribute' => [],
'id_default_image' => [
'getter' => 'getCoverWs',
'setter' => 'setCoverWs',
'xlink_resource' => [
'resourceName' => 'images',
'subResourceName' => 'products',
],
],
'id_default_combination' => [
'getter' => 'getWsDefaultCombination',
'setter' => 'setWsDefaultCombination',
'xlink_resource' => [
'resourceName' => 'combinations',
],
],
'id_tax_rules_group' => [
'xlink_resource' => [
'resourceName' => 'tax_rule_groups',
],
],
'position_in_category' => [
'getter' => 'getWsPositionInCategory',
'setter' => 'setWsPositionInCategory',
],
'manufacturer_name' => [
'getter' => 'getWsManufacturerName',
'setter' => false,
],
'quantity' => [
'getter' => false,
'setter' => false,
],
'type' => [
'getter' => 'getWsType',
'setter' => 'setWsType',
],
],
'associations' => [
'categories' => [
'resource' => 'category',
'fields' => [
'id' => ['required' => true],
],
],
'images' => [
'resource' => 'image',
'fields' => ['id' => []],
],
'combinations' => [
'resource' => 'combination',
'fields' => [
'id' => ['required' => true],
],
],
'product_option_values' => [
'resource' => 'product_option_value',
'fields' => [
'id' => ['required' => true],
],
],
'product_features' => [
'resource' => 'product_feature',
'fields' => [
'id' => ['required' => true],
'id_feature_value' => [
'required' => true,
'xlink_resource' => 'product_feature_values',
],
],
],
'tags' => ['resource' => 'tag',
'fields' => [
'id' => ['required' => true],
], ],
'stock_availables' => ['resource' => 'stock_available',
'fields' => [
'id' => ['required' => true],
'id_product_attribute' => ['required' => true],
],
'setter' => false,
],
'attachments' => [
'resource' => 'attachment',
'api' => 'attachments',
'fields' => [
'id' => ['required' => true],
],
],
'accessories' => [
'resource' => 'product',
'api' => 'products',
'fields' => [
'id' => [
'required' => true,
'xlink_resource' => 'products', ],
],
],
'product_bundle' => [
'resource' => 'product',
'api' => 'products',
'fields' => [
'id' => ['required' => true],
'id_product_attribute' => [],
'quantity' => [],
],
],
],
];
const CUSTOMIZE_FILE = 0;
const CUSTOMIZE_TEXTFIELD = 1;
/**
* Note: prefix is "PTYPE" because TYPE_ is used in ObjectModel (definition).
*/
const PTYPE_SIMPLE = 0;
const PTYPE_PACK = 1;
const PTYPE_VIRTUAL = 2;
/**
* @param int|null $id_product Product identifier
* @param bool $full Load with price, tax rate, manufacturer name, supplier name, tags, stocks...
* @param int|null $id_lang Language identifier
* @param int|null $id_shop Shop identifier
* @param Context|null $context Context to use for retrieve cart
*/
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
parent::__construct($id_product, $id_lang, $id_shop);
$unitPriceRatio = new DecimalNumber((string) ($this->unit_price_ratio ?? 0));
$price = new DecimalNumber((string) ($this->price ?? 0));
if ($unitPriceRatio->isGreaterThanZero()) {
$this->unit_price = (float) (string) $price->dividedBy($unitPriceRatio);
}
if ($full && $this->id) {
if (!$context) {
$context = Context::getContext();
}
$this->isFullyLoaded = $full;
$this->tax_name = 'deprecated'; // The applicable tax may be BOTH the product one AND the state one (moreover this variable is some deadcode)
$this->manufacturer_name = Manufacturer::getNameById((int) $this->id_manufacturer);
$this->supplier_name = Supplier::getNameById((int) $this->id_supplier);
$address = null;
if (is_object($context->cart) && $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} != null) {
$address = $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
}
$this->tax_rate = $this->getTaxesRate(new Address($address));
$this->new = $this->isNew();
// Keep base price
$this->base_price = $this->price;
$this->price = Product::getPriceStatic((int) $this->id, false, null, 6, null, false, true, 1, false, null, null, null, $this->specificPrice);
$this->unit_price = ($this->unit_price_ratio != 0 ? $this->price / $this->unit_price_ratio : 0);
$this->tags = Tag::getProductTags((int) $this->id);
$this->loadStockData();
}
if ($this->id_category_default) {
$this->category = Category::getLinkRewrite((int) $this->id_category_default, (int) $id_lang);
}
}
/**
* @see ObjectModel::getFieldsShop()
*
* @return array
*/
public function getFieldsShop()
{
$fields = parent::getFieldsShop();
if (null === $this->update_fields || (!empty($this->update_fields['price']) && !empty($this->update_fields['unit_price']))) {
if ($this->unit_price !== null) {
$fields['unit_price_ratio'] = (float) $this->unit_price > 0 ? $this->price / $this->unit_price : 0;
}
}
$fields['unity'] = pSQL($this->unity);
return $fields;
}
/**
* {@inheritdoc}
*/
public function add($autodate = true, $null_values = false)
{
if ($this->is_virtual) {
$this->product_type = ProductType::TYPE_VIRTUAL;
}
if (!parent::add($autodate, $null_values)) {
return false;
}
$id_shop_list = Shop::getContextListShopID();
if ($this->getType() == Product::PTYPE_VIRTUAL) {
foreach ($id_shop_list as $value) {
StockAvailable::setProductOutOfStock((int) $this->id, OutOfStockType::OUT_OF_STOCK_AVAILABLE, $value);
}
if ($this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
}
} else {
foreach ($id_shop_list as $value) {
StockAvailable::setProductOutOfStock((int) $this->id, OutOfStockType::OUT_OF_STOCK_DEFAULT, $value);
}
}
$this->setGroupReduction();
Hook::exec('actionProductSave', ['id_product' => (int) $this->id, 'product' => $this]);
return true;
}
/**
* {@inheritdoc}
*/
public function update($null_values = false)
{
if ($this->is_virtual) {
$this->product_type = ProductType::TYPE_VIRTUAL;
}
$return = parent::update($null_values);
$this->setGroupReduction();
// Sync stock Reference, EAN13, MPN and UPC
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) {
Db::getInstance()->update('stock', [
'reference' => pSQL($this->reference),
'ean13' => pSQL($this->ean13),
'isbn' => pSQL($this->isbn),
'upc' => pSQL($this->upc),
'mpn' => pSQL($this->mpn),
], 'id_product = ' . (int) $this->id . ' AND id_product_attribute = 0');
}
Hook::exec('actionProductSave', ['id_product' => (int) $this->id, 'product' => $this]);
Hook::exec('actionProductUpdate', ['id_product' => (int) $this->id, 'product' => $this]);
if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
}
return $return;
}
/**
* Init computation of price display method (i.e. price should be including tax or not) for a customer.
* If customer Id passed as null then this compute price display method with according of current group.
* Otherwise a price display method will compute with according of a customer address (i.e. country).
*
* @see Group::getPriceDisplayMethod()
*
* @param int|null $id_customer Customer identifier
*/
public static function initPricesComputation($id_customer = null)
{
if ((int) $id_customer > 0) {
$customer = new Customer((int) $id_customer);
if (!Validate::isLoadedObject($customer)) {
die(Tools::displayError());
}
self::$_taxCalculationMethod = Group::getPriceDisplayMethod((int) $customer->id_default_group);
$cur_cart = Context::getContext()->cart;
$id_address = 0;
if (Validate::isLoadedObject($cur_cart)) {
$id_address = (int) $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
}
$address_infos = Address::getCountryAndState($id_address);
if (self::$_taxCalculationMethod != PS_TAX_EXC
&& !empty($address_infos['vat_number'])
&& $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY')
&& Configuration::get('VATNUMBER_MANAGEMENT')) {
self::$_taxCalculationMethod = PS_TAX_EXC;
}
} else {
self::$_taxCalculationMethod = Group::getPriceDisplayMethod(Group::getCurrent()->id);
}
}
/**
* Returns price display method for a customer (i.e. price should be including tax or not).
*
* @see initPricesComputation()
*
* @param int|null $id_customer Customer identifier
*
* @return int Returns 0 (PS_TAX_INC) if tax should be included, otherwise 1 (PS_TAX_EXC) - tax should be excluded
*/
public static function getTaxCalculationMethod($id_customer = null)
{
if (self::$_taxCalculationMethod === null || $id_customer !== null) {
Product::initPricesComputation($id_customer);
}
return (int) self::$_taxCalculationMethod;
}
/**
* Move a product inside its category.
*
* @param bool $way Up (1) or Down (0)
* @param int $position
*
* @return bool Update result
*/
public function updatePosition($way, $position)
{
if (!$res = Db::getInstance()->executeS('
SELECT cp.`id_product`, cp.`position`, cp.`id_category`
FROM `' . _DB_PREFIX_ . 'category_product` cp
WHERE cp.`id_category` = ' . (int) Tools::getValue('id_category', 1) . '
ORDER BY cp.`position` ASC')
) {
return false;
}
foreach ($res as $product) {
if ((int) $product['id_product'] == (int) $this->id) {
$moved_product = $product;
}
}
if (!isset($moved_product) || !isset($position)) {
return false;
}
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$result = (
Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'category_product` cp
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (p.`id_product` = cp.`id_product`)
' . Shop::addSqlAssociation('product', 'p') . '
SET cp.`position`= `position` ' . ($way ? '- 1' : '+ 1') . ',
p.`date_upd` = "' . date('Y-m-d H:i:s') . '", product_shop.`date_upd` = "' . date('Y-m-d H:i:s') . '"
WHERE cp.`position`
' . ($way
? '> ' . (int) $moved_product['position'] . ' AND `position` <= ' . (int) $position
: '< ' . (int) $moved_product['position'] . ' AND `position` >= ' . (int) $position) . '
AND `id_category`=' . (int) $moved_product['id_category'])
&& Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'category_product` cp
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (p.`id_product` = cp.`id_product`)
' . Shop::addSqlAssociation('product', 'p') . '
SET cp.`position` = ' . (int) $position . ',
p.`date_upd` = "' . date('Y-m-d H:i:s') . '", product_shop.`date_upd` = "' . date('Y-m-d H:i:s') . '"
WHERE cp.`id_product` = ' . (int) $moved_product['id_product'] . '
AND cp.`id_category`=' . (int) $moved_product['id_category'])
);
Hook::exec('actionProductUpdate', ['id_product' => (int) $this->id, 'product' => $this]);
return $result;
}
/**
* Reorder product position in category $id_category.
* Call it after deleting a product from a category.
*
* @param int $id_category Category identifier
* @param int $position
*
* @return bool
*/
public static function cleanPositions($id_category, $position = 0)
{
$return = true;
if (!(int) $position) {
$result = Db::getInstance()->executeS('
SELECT `id_product`
FROM `' . _DB_PREFIX_ . 'category_product`
WHERE `id_category` = ' . (int) $id_category . '
ORDER BY `position`
');
$total = count($result);
for ($i = 0; $i < $total; ++$i) {
$return &= Db::getInstance()->update(
'category_product',
['position' => $i],
'`id_category` = ' . (int) $id_category . ' AND `id_product` = ' . (int) $result[$i]['id_product']
);
$return &= Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'product` p' . Shop::addSqlAssociation('product', 'p') . '
SET p.`date_upd` = "' . date('Y-m-d H:i:s') . '", product_shop.`date_upd` = "' . date('Y-m-d H:i:s') . '"
WHERE p.`id_product` = ' . (int) $result[$i]['id_product']
);
}
} else {
$result = Db::getInstance()->executeS('
SELECT `id_product`
FROM `' . _DB_PREFIX_ . 'category_product`
WHERE `id_category` = ' . (int) $id_category . ' AND `position` > ' . (int) $position . '
ORDER BY `position`
');
$total = count($result);
$return &= Db::getInstance()->update(
'category_product',
['position' => ['type' => 'sql', 'value' => '`position`-1']],
'`id_category` = ' . (int) $id_category . ' AND `position` > ' . (int) $position
);
for ($i = 0; $i < $total; ++$i) {
$return &= Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'product` p' . Shop::addSqlAssociation('product', 'p') . '
SET p.`date_upd` = "' . date('Y-m-d H:i:s') . '", product_shop.`date_upd` = "' . date('Y-m-d H:i:s') . '"
WHERE p.`id_product` = ' . (int) $result[$i]['id_product']
);
}