-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshoppingfeed.php
More file actions
1681 lines (1504 loc) · 62.4 KB
/
Copy pathshoppingfeed.php
File metadata and controls
1681 lines (1504 loc) · 62.4 KB
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 2019 Shopping Feed
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 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/AFL-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 tech@202-ecommerce.com so we can send you a copy immediately.
*
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright Since 2019 Shopping Feed
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License (AFL 3.0)
*/
use ShoppingfeedAddon\OrderInvoiceSync\Hub;
use ShoppingfeedAddon\Services\SfTools;
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'shoppingfeed/vendor/autoload.php';
// Set this as comment so Classlib will import the files; but don't uncomment !
// Installation will fail on PS 1.6 if "use" statements are in the main module file
// use ShoppingfeedClasslib\Module;
// use ShoppingfeedClasslib\Actions\ActionsHandler;
// use ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerHandler;
// use ShoppingfeedClasslib\Registry;
// use ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerExtension;
// use ShoppingfeedClasslib\Extensions\ProcessMonitor\ProcessMonitorExtension;
// use ShoppingfeedAddon\Hook\HookDispatcher;
// use ShoppingfeedAddon\ProductFilter\FilterFactory;
// use ShoppingfeedAddon\Services\OrderTracker;
// use ShoppingfeedAddon\Services\SfTools;
/**
* The base module class
*/
class Shoppingfeed extends ShoppingfeedClasslib\Module
{
/**
* This module requires at least PHP version
*
* @var string
*/
public $php_version_required = '5.6';
public const AUTH_TOKEN = 'SHOPPINGFEED_AUTH_TOKEN';
public const STOCK_SYNC_ENABLED = 'SHOPPINGFEED_STOCK_SYNC_ENABLED';
public const PRICE_SYNC_ENABLED = 'SHOPPINGFEED_PRICE_SYNC_ENABLED';
public const ORDER_SYNC_ENABLED = 'SHOPPINGFEED_ORDER_SYNC_ENABLED';
public const STOCK_SYNC_MAX_PRODUCTS = 'SHOPPINGFEED_STOCK_SYNC_MAX_PRODUCTS';
public const REAL_TIME_SYNCHRONIZATION = 'SHOPPINGFEED_REAL_TIME_SYNCHRONIZATION';
public const LAST_CRON_TIME_SYNCHRONIZATION = 'SHOPPINGFEED_LAST_CRON_TIME_SYNCHRONIZATION';
public const ORDER_STATUS_TIME_SHIFT = 'SHOPPINGFEED_ORDER_STATUS_TIME_SHIFT';
public const ORDER_STATUS_MAX_ORDERS = 'SHOPPINGFEED_ORDER_STATUS_MAX_ORDERS';
public const SHIPPED_ORDERS = 'SHOPPINGFEED_SHIPPED_ORDERS';
public const CANCELLED_ORDERS = 'SHOPPINGFEED_CANCELLED_ORDERS';
public const REFUNDED_ORDERS = 'SHOPPINGFEED_REFUNDED_ORDERS';
public const DELIVERED_ORDERS = 'SHOPPINGFEED_DELIVERED_ORDERS';
public const ORDER_IMPORT_ENABLED = 'SHOPPINGFEED_ORDER_IMPORT_ENABLED';
public const ORDER_IMPORT_TEST = 'SHOPPINGFEED_ORDER_IMPORT_TEST';
public const ORDER_IMPORT_SHIPPED = 'SHOPPINGFEED_ORDER_IMPORT_SHIPPED';
public const ORDER_IMPORT_SHIPPED_MARKETPLACE = 'SHOPPINGFEED_ORDER_IMPORT_SHIPPED_MARKETPLACE';
public const ORDER_IMPORT_SPECIFIC_RULES_CONFIGURATION = 'SHOPPINGFEED_ORDER_IMPORT_SPECIFIC_RULES_CONFIGURATION';
public const ORDER_DEFAULT_CARRIER_REFERENCE = 'SHOPPINGFEED_ORDER_DEFAULT_CARRIER_REFERENCE';
public const PRODUCT_FEED_CARRIER_REFERENCE = 'SHOPPINGFEED_PRODUCT_FEED_CARRIER_REFERENCE';
public const PRODUCT_FEED_SYNC_PACK = 'SHOPPINGFEED_PRODUCT_FEED_SYNC_PACK';
public const PRODUCT_FEED_IMAGE_FORMAT = 'SHOPPINGFEED_PRODUCT_FEED_IMAGE_FORMAT';
public const PRODUCT_FEED_CATEGORY_DISPLAY = 'SHOPPINGFEED_PRODUCT_FEED_CATEGORY_DISPLAY';
public const PRODUCT_FEED_CUSTOM_FIELDS = 'SHOPPINGFEED_PRODUCT_FEED_CUSTOM_FIELDS';
public const PRODUCT_FEED_REFERENCE_FORMAT = 'SHOPPINGFEED_PRODUCT_FEED_REFERENCE_FORMAT';
public const PRODUCT_FEED_RULE_FILTERS = 'SHOPPINGFEED_PRODUCT_FEED_RULE_FILTERS';
public const PRODUCT_VISIBILTY_NOWHERE = 'SHOPPINGFEED_PRODUCT_VISIBILTY_NOWHERE';
public const PRODUCT_SYNC_BY_DATE_UPD = 'SHOPPINGFEED_PRODUCT_SYNC_BY_DATE_UPD';
public const PRODUCT_FEED_TIME_FULL_UPDATE = 'SHOPPINGFEED_PRODUCT_FEED_TIME_FULL_UPDATE';
public const PRODUCT_FEED_INTERVAL_CRON = 'SHOPPINGFEED_PRODUCT_FEED_INTERVAL_CRON';
public const ORDER_IMPORT_PERMANENT_SINCE_DATE = 'SHOPPINGFEED_ORDER_IMPORT_PERMANENT_SINCE_DATE';
public const ORDER_SHIPPED_IMPORT_PERMANENT_SINCE_DATE = 'SHOPPINGFEED_ORDER_SHIPPED_IMPORT_PERMANENT_SINCE_DATE';
public const ORDER_SHIPPED_BY_MARKETPLACE_IMPORT_PERMANENT_SINCE_DATE = 'SHOPPINGFEED_ORDER_SHIPPED_BY_MARKETPLACE_IMPORT_PERMANENT_SINCE_DATE';
public const IMPORT_ORDER_STATE = 'SHOPPINGFEED_FIRST_STATE_AFTER_IMPORT';
public const CDISCOUNT_FEE_PRODUCT = 'SHOPPINGFEED_CDISCOUNT_FEE_PRODUCT';
public const NEED_UPDATE_HOOK = 'SHOPPINGFEED_IS_NEED_UPDATE_HOOK';
public const ORDER_TRACKING = 'SHOPPINGFEED_ORDER_TRACKING';
public const ALLOW_PARTIAL_REFUND = 'SHOPPINGFEED_ALLOW_PARTIAL_REFUND';
public const COMPRESS_PRODUCTS_FEED = 'SHOPPINGFEED_COMPRESS_PRODUCTS_FEED';
public const SEND_NOTIFICATION = 'SHOPPINGFEED_SEND_NOTIFICATION';
public const PRODUCT_FEED_EXPORT_HIERARCHY = 'SHOPPINGFEED_PRODUCT_FEED_EXPORT_HIERARCHY';
public const SYNC_PRODUCT_ATTACHMENT_TITLE = 'SHOPPINGFEED_SYNC_PRODUCT_ATTACHMENT_TITLE';
public const STATUS_SEND_INVOICE = 'SHOPPINGFEED_STATUS_SEND_INVOICE';
public const ORDER_OPERATION_ACCEPT = 'accept';
public const ORDER_OPERATION_CANCEL = 'cancel';
public const ORDER_OPERATION_REFUSE = 'refuse';
public const ORDER_OPERATION_SHIP = 'ship';
public const ORDER_OPERATION_REFUND = 'refund';
public const ORDER_OPERATION_ACKNOWLEDGE = 'acknowledge';
public const ORDER_OPERATION_UNACKNOWLEDGE = 'unacknowledge';
public const ORDER_OPERATION_UPLOAD_DOCUMENTS = 'upload-documents';
public const ORDER_OPERATION_DELIVER = 'deliver';
public const ORDER_INVOICE_SYNC_MARKETPLACES = 'SHOPPINGFEED_ORDER_INVOICE_SYNC_MARKETPLACES';
public const IMPORT_MARKETPLACE_DISCOUNT = 'SHOPPINGFEED_IMPORT_MARKETPLACE_DISCOUNT';
public $extensions = [
ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerExtension::class,
ShoppingfeedClasslib\Extensions\ProcessMonitor\ProcessMonitorExtension::class,
ShoppingfeedClasslib\Extensions\Diagnostic\DiagnosticExtension::class,
];
/**
* List of objectModel used in this Module
*
* @var array
*/
public $objectModels = [
ShoppingfeedTaskOrder::class,
ShoppingfeedProduct::class,
ShoppingfeedOrder::class,
ShoppingfeedCarrier::class,
ShoppingfeedPreloading::class,
ShoppingfeedToken::class,
];
/**
* List of cron tasks indexed by controller name
* Title value must be an array indexed by iso language (en is required)
* Frequency value can be hourly, daily, weekly, monthly
*
* @var array
*/
public $cronTasks = [
'syncProduct' => [
'name' => 'shoppingfeed:syncProduct',
'title' => [
'en' => 'Synchronize products on Shopping Feed',
'fr' => 'Synchronisation des produits sur Shopping Feed',
],
'frequency' => '5min',
],
'syncOrder' => [
'name' => 'shoppingfeed:syncOrder',
'title' => [
'en' => 'Synchronize orders on Shopping Feed',
'fr' => 'Synchronisation des commandes sur Shopping Feed',
],
'frequency' => '5min',
],
'syncAll' => [
'name' => 'shoppingfeed:syncAll',
'title' => [
'en' => 'Sync shoppingfeed products and orders',
'fr' => 'Sync produits et commandes shoppingfeed',
],
'frequency' => '5min',
],
];
/** @var array
*/
public $moduleAdminControllers = [
[
'name' => [
'en' => 'Shopping Feed',
'fr' => 'Shopping Feed',
],
'class_name' => 'shoppingfeed',
'parent_class_name' => 'SELL',
'icon' => 'store_mall_directory',
'visible' => true,
],
[
'name' => [
'en' => 'Marketplaces Summary',
'fr' => 'Commandes Marketplaces',
],
'class_name' => 'AdminShoppingfeedOrders',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Shopping Feed',
'fr' => 'Shopping Feed',
],
'class_name' => 'AdminShoppingfeedOrderImport',
'parent_class_name' => 'shoppingfeed',
'visible' => false,
],
[
'name' => [
'en' => 'Settings',
'fr' => 'Paramètres',
],
'class_name' => 'AdminShoppingfeedSettings',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Account settings',
'fr' => 'Paramètres du compte',
],
'class_name' => 'AdminShoppingfeedAccountSettings',
'parent_class_name' => 'AdminShoppingfeedSettings',
'visible' => true,
],
[
'name' => [
'en' => 'Products feed',
'fr' => 'Flux des produits',
],
'class_name' => 'AdminShoppingfeedGeneralSettings',
'parent_class_name' => 'AdminShoppingfeedSettings',
'visible' => true,
],
[
'name' => [
'en' => 'Orders feeds',
'fr' => 'Flux des commandes',
],
'class_name' => 'AdminShoppingfeedOrderImportRules',
'parent_class_name' => 'AdminShoppingfeedSettings',
'visible' => true,
],
[
'name' => [
'en' => 'Logs & crons',
'fr' => 'Logs & crons',
],
'class_name' => 'AdminShoppingfeedProcess',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Logs',
'fr' => 'Logs',
],
'class_name' => 'AdminShoppingfeedProcessLogger',
'parent_class_name' => 'AdminShoppingfeedProcess',
'visible' => true,
],
[
'name' => [
'en' => 'Cron Tasks',
'fr' => 'Tâches cron',
],
'class_name' => 'AdminShoppingfeedProcessMonitor',
'parent_class_name' => 'AdminShoppingfeedProcess',
'visible' => true,
],
[
'name' => [
'en' => 'Diagnostics',
'fr' => 'Dépannage',
],
'class_name' => 'AdminShoppingfeedDiagnostic',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
];
/**
* List of ModuleFrontController used in this Module
* Module::install() register it, after that you can edit it in BO (for rewrite if needed)
*
* @var array
*/
public $controllers = [
'syncProduct',
'syncOrder',
];
/**
* List of hooks used in this Module
*
* @var array
*/
public $hooks = [
'actionUpdateQuantity',
'actionObjectProductUpdateBefore',
'actionObjectCombinationUpdateBefore',
'actionObjectProductUpdateAfter',
'actionObjectCombinationUpdateAfter',
'actionValidateOrder',
'actionOrderStatusPostUpdate',
'actionOrderSlipAdd',
'actionShoppingfeedOrderImportRegisterSpecificRules',
'actionObjectProductDeleteBefore',
'ActionObjectCategoryUpdateAfter',
'actionObjectSpecificPriceAddAfter',
'actionObjectSpecificPriceUpdateAfter',
'actionObjectSpecificPriceDeleteAfter',
'actionDeleteProductAttribute',
'actionAdminSpecificPriceRuleControllerDeleteBefore',
'displayPDFInvoice',
'actionEmailSendBefore',
];
/**
* Used to avoid spam or unauthorized execution of cron controller
*
* @var string Unique token depend on _COOKIE_KEY_ which is unique to this website
*
* @see SfTools::hash()
*/
public $secure_key;
/** @var SfTools */
public $tools;
/**
* creates an instance of the module
* Shoppingfeed constructor.
*/
public function __construct()
{
$this->name = 'shoppingfeed';
$this->version = '@version@';
$this->author = '202 ecommerce';
$this->tab = 'market_place';
$this->ps_versions_compliancy = ['min' => '1.7.6', 'max' => '9.99.99'];
$this->need_instance = 0;
$this->bootstrap = true;
$this->module_key = '7251acf8971b8f1de58ce48f01f86a9d';
$this->tools = new SfTools();
parent::__construct();
$this->displayName = $this->l('Shoppingfeed Prestashop Plugin (Feed&Order)');
$this->description = $this->l('Improve your Shopping Feed module\'s Marketplaces stocks synchronization speed.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
$this->secure_key = $this->tools->hash($this->name);
if (version_compare(_PS_VERSION_, '1.7', '<')) {
$this->moduleAdminControllers = [
[
'name' => [
'en' => 'Shopping Feed',
'fr' => 'Shopping Feed',
],
'class_name' => 'shoppingfeed',
'icon' => 'AdminParentOrders',
'visible' => true,
],
[
'name' => [
'en' => 'Marketplaces Summary',
'fr' => 'Commandes Marketplaces',
],
'class_name' => 'AdminShoppingfeedOrders',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Shopping Feed',
'fr' => 'Shopping Feed',
],
'class_name' => 'AdminShoppingfeedOrderImport',
'parent_class_name' => 'shoppingfeed',
'visible' => false,
],
[
'name' => [
'en' => 'Account settings',
'fr' => 'Paramètres du compte',
],
'class_name' => 'AdminShoppingfeedAccountSettings',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Products feed',
'fr' => 'Flux des produits',
],
'class_name' => 'AdminShoppingfeedGeneralSettings',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Orders feeds',
'fr' => 'Flux des commandes',
],
'class_name' => 'AdminShoppingfeedOrderImportRules',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Logs',
'fr' => 'Logs',
],
'class_name' => 'AdminShoppingfeedProcessLogger',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Cron Tasks',
'fr' => 'Tâches cron',
],
'class_name' => 'AdminShoppingfeedProcessMonitor',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
[
'name' => [
'en' => 'Diagnostics',
'fr' => 'Dépannage',
],
'class_name' => 'AdminShoppingfeedDiagnostic',
'parent_class_name' => 'shoppingfeed',
'visible' => true,
],
];
}
// There is a risk having a class not found exception because of cache of autoload.php
try {
$this->hookDispatcher = new ShoppingfeedAddon\Hook\HookDispatcher($this);
$this->hooks = array_merge($this->hooks, $this->hookDispatcher->getAvailableHooks());
} catch (Exception $e) { // for php version < 7.0
} catch (Throwable $e) {
}
if ((int) Configuration::getGlobalValue(self::NEED_UPDATE_HOOK) === 1) {
Configuration::updateGlobalValue(self::NEED_UPDATE_HOOK, (int) $this->updateHooks());
}
}
/**
* Installs the module; see the parent ShoppingfeedModule class from classlib
*
* @return bool
*/
public function install()
{
$res = parent::install();
$res &= $this->addDateIndexToLogs();
$res &= $this->addIndexToPreloadingTable();
$this->setConfigurationDefault(self::STOCK_SYNC_ENABLED, true);
$this->setConfigurationDefault(self::PRICE_SYNC_ENABLED, true);
$this->setConfigurationDefault(self::ORDER_SYNC_ENABLED, true);
$this->setConfigurationDefault(self::STOCK_SYNC_MAX_PRODUCTS, 100);
$this->setConfigurationDefault(self::REAL_TIME_SYNCHRONIZATION, false);
$this->setConfigurationDefault(self::ORDER_STATUS_TIME_SHIFT, 5);
$this->setConfigurationDefault(self::ORDER_STATUS_MAX_ORDERS, 100);
$this->setConfigurationDefault(self::SHIPPED_ORDERS, json_encode([]));
$this->setConfigurationDefault(self::CANCELLED_ORDERS, json_encode([]));
$this->setConfigurationDefault(self::REFUNDED_ORDERS, json_encode([]));
$this->setConfigurationDefault(self::DELIVERED_ORDERS, json_encode([]));
$this->setConfigurationDefault(self::ORDER_IMPORT_ENABLED, true);
$this->setConfigurationDefault(self::ORDER_IMPORT_SHIPPED, false);
$this->setConfigurationDefault(self::ORDER_IMPORT_SPECIFIC_RULES_CONFIGURATION, json_encode([]));
$this->setConfigurationDefault(self::ORDER_IMPORT_SHIPPED_MARKETPLACE, 0);
$this->setConfigurationDefault(self::PRODUCT_FEED_CARRIER_REFERENCE, Configuration::getGlobalValue('PS_CARRIER_DEFAULT'));
$this->setConfigurationDefault(self::ORDER_DEFAULT_CARRIER_REFERENCE, Configuration::getGlobalValue('PS_CARRIER_DEFAULT'));
$this->setConfigurationDefault(self::COMPRESS_PRODUCTS_FEED, 1);
$this->setConfigurationDefault(self::SEND_NOTIFICATION, 0);
if (method_exists(ImageType::class, 'getFormatedName')) {
$this->setConfigurationDefault(self::PRODUCT_FEED_IMAGE_FORMAT, call_user_func([ImageType::class, 'getFormatedName'], 'large'));
} else {
$this->setConfigurationDefault(self::PRODUCT_FEED_IMAGE_FORMAT, ImageType::getFormattedName('large'));
}
$this->saveToken();
return (bool) $res;
}
private function saveToken()
{
$sfToken = new ShoppingfeedToken();
$shops = Shop::getShops();
foreach ($shops as $shop) {
$tokenConfig = Configuration::get('SHOPPING_FLUX_TOKEN', null, null, $shop['id_shop']);
if ($tokenConfig == false) {
continue;
}
$tokenTable = $sfToken->findByToken($tokenConfig);
if ($tokenTable !== false) {
continue;
}
try {
$api = ShoppingfeedApi::getInstanceByToken(null, $tokenConfig);
} catch (ShoppingfeedPrefix\GuzzleHttp\Exception\ClientException $e) {
continue;
}
if (empty($api->getMainStore())) {
continue;
}
$sfToken->addToken(
$shop['id_shop'],
Configuration::get('PS_LANG_DEFAULT', null, null, $shop['id_shop']),
Configuration::get('PS_CURRENCY_DEFAULT', null, null, $shop['id_shop']),
$tokenConfig,
$api->getMainStore()->getId(),
$api->getMainStore()->getName()
);
}
}
public function uninstall()
{
return parent::uninstall();
}
/**
* Desactivate current module. Hide module admin Tab.
*
* @param bool $force_all If true, disable module for all shop
*/
public function disable($force_all = false)
{
if (parent::disable($force_all) === false && version_compare(_PS_VERSION_, '1.7', '>=')) {
// On pS1.6, Module::disable() always returns false
return false;
}
$tab = Tab::getInstanceFromClassName('shoppingfeed');
if ($tab->id == null) {
return true;
}
$tab->active = false;
$tab->save();
return true;
}
/**
* Activate current module. Active module admin Tab.
*
* @param bool $force_all If true, enable module for all shop
*/
public function enable($force_all = false)
{
if (parent::enable($force_all) === false) {
return false;
}
$tab = Tab::getInstanceFromClassName('shoppingfeed');
if ($tab->id == null) {
return true;
}
$tab->active = true;
$tab->save();
return true;
}
public function setConfigurationDefault($key, $defaultValue)
{
if (!Configuration::hasKey($key)) {
Configuration::updateGlobalValue($key, $defaultValue);
}
}
/**
* Breaking changes (e.g. deprecation) notice should be set here; get the
* controller with $this->context->controller and set the messages
*/
public function setBreakingChangesNotices()
{
}
/**
* @return bool
*/
public static function isOrderSyncAvailable($id_shop = null)
{
$shoppingfluxexport = Module::getInstanceByName('shoppingfluxexport');
// Is the old module installed ?
if (Validate::isLoadedObject($shoppingfluxexport) && $shoppingfluxexport->active) {
// Is order "shipped" status sync disabled in the old module ?
if (false === empty(Configuration::get('SHOPPING_FLUX_STATUS_SHIPPED', null, null, $id_shop))) {
return false;
}
// Is order "canceled" status sync disabled in the old module ?
if (false === empty(Configuration::get('SHOPPING_FLUX_STATUS_CANCELED', null, null, $id_shop))) {
return false;
}
}
return true;
}
public static function isOrderEligibleToSync(Order $order)
{
$orderCreationDate = DateTime::createFromFormat('Y-m-d H:i:s', $order->date_add);
if ($orderCreationDate && (time() - $orderCreationDate->getTimestamp() > 60 * 60 * 24 * 90)) {
return false;
}
return true;
}
/**
* Checks if order import can be activated
*
* @return bool
*/
public static function isOrderImportAvailable($id_shop = null)
{
$shoppingfluxexport = Module::getInstanceByName('shoppingfluxexport');
// Is the old module installed ?
if (Validate::isLoadedObject($shoppingfluxexport) && $shoppingfluxexport->active) {
if (Configuration::get('SHOPPING_FLUX_ORDERS', null, null, $id_shop) != '') {
return false;
}
}
return true;
}
public static function isCatalogModeEnabled()
{
return Configuration::isCatalogMode();
}
/**
* Redirects the user to our AdminController for configuration
*
* @throws PrestaShopException
*/
public function getContent()
{
Tools::redirectAdmin(
Context::getContext()->link->getAdminLink('AdminShoppingfeedAccountSettings')
);
}
/**
* Returns the product's Shopping Feed reference. The developer can skip
* products to sync by overriding this method and have it return false.
*
* @param ShoppingfeedProduct $sfProduct
* @param array $arguments Should you want to pass more arguments to this
* function, you can find them in this array
*
* @return string
*/
public function mapReference(ShoppingfeedProduct $sfProduct, ...$arguments)
{
$reference = $sfProduct->getShoppingfeedReference();
Hook::exec(
'ShoppingfeedMapProductReference', // hook_name
[
'ShoppingFeedProduct' => &$sfProduct,
'reference' => &$reference,
] // hook_args
);
return $reference;
}
/**
* Returns the Prestashop product matching the Shopping Feed reference. The
* developer can skip specific products during order import by overriding
* this method and have it return false.
*
* @param string $sfProductReference The product's reference in Shopping Feed's system
* @param int $id_shop
* @param array $arguments Should you want to pass more arguments to this
* function, you can find them in this array
*
* @return Product
*/
public function mapPrestashopProduct($sfProductReference, $id_shop, ...$arguments)
{
$sfProduct = new ShoppingfeedProduct();
$sfProductReference = $sfProduct->getReverseShoppingfeedReference($sfProductReference, $id_shop);
Hook::exec(
'ShoppingfeedReverseProductReference', // hook_name
[
'sfProductReference' => &$sfProductReference,
'id_shop' => $id_shop,
] // hook_args
);
$explodedReference = explode('_', $sfProductReference);
$id_product = isset($explodedReference[0]) ? (int) $explodedReference[0] : null;
if ($this->tools->isInt($id_product)) {
$product = new Product($id_product, true, null, $id_shop);
} else {
$product = new Product();
}
if (isset($explodedReference[1]) && $this->tools->isInt($explodedReference[1])) {
/* @phpstan-ignore-next-line */
$product->id_product_attribute = $explodedReference[1];
} else {
/* @phpstan-ignore-next-line */
$product->id_product_attribute = null;
}
Hook::exec(
'ShoppingfeedMapProduct', // hook_name
[
'sfProductReference' => &$sfProductReference,
'product' => &$product,
'id_shop' => $id_shop,
] // hook_args
);
return $product;
}
/**
* Returns the product's price sent to the Shopping Feed API. The developer
* can skip products to sync by overriding this method and have it return
* false. Note that the comparison with the return value is strict to allow
* "0" as a valid price.
*
* @param ShoppingfeedProduct $sfProduct
* @param int $id_shop
* @param array $arguments Should you want to pass more arguments to this
* function, you can find them in this array
*
* @return float
*/
public function mapProductPrice(ShoppingfeedProduct $sfProduct, $id_shop, $arguments = [])
{
$specific_price_output = null;
Product::flushPriceCache();
// Tax depends on a country. We use country configured as default one for shop.
$id_country = (int) Configuration::get('PS_COUNTRY_DEFAULT', null, null, $id_shop);
$id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT', null, null, $id_shop);
$id_group = (int) Group::getCurrent()->id;
$price = Product::priceCalculation(
$id_shop,
$sfProduct->id_product,
$sfProduct->id_product_attribute ? // id_product_attribute
$sfProduct->id_product_attribute : null,
$id_country,
0,// id_state
'',// postcode
$id_currency,// id_currency
$id_group,// id_group
1,// quantity
true,// use_tax
2,// decimals
false,// only_reduc
is_array($arguments) && array_key_exists('price_with_reduction', $arguments) && $arguments['price_with_reduction'] === true, // usereduc
true,// with_ecotax
$specific_price_output,// specific_price_output
true,// use_group_reduction
0,// id_customer
true,// use_customer_price
0,// id_cart
0,// real_quantity
0// id_customization
);
Hook::exec(
'ShoppingfeedMapProductPrice', // hook_name
[
'ShoppingFeedProduct' => &$sfProduct,
'price' => &$price,
'arguments' => $arguments,
] // hook_args
);
return $price;
}
/**
* Returns the number of product in the feed The developer
* can skip products to sync so it's necessary to ajust the number of
* products in order to manage indexation.
*
* @return int
*/
public function countProductsOnFeed($id_shop = null)
{
$sql = $this->sqlProductsOnFeed($id_shop);
$sql->select('COUNT(distinct p.`id_product`)');
$countProductsOnFeed = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
return (int) $countProductsOnFeed;
}
/**
* Returns the
*
* @return DbQuery
*/
public function sqlProductsOnFeed($id_shop = null)
{
$cdiscountFeeProductId = $this->initCdiscountFeeProduct()->getIdProduct();
$sql = new DbQuery();
$sql->from(Product::$definition['table'] . '_shop', 'ps')
->leftJoin(Product::$definition['table'], 'p', 'p.id_product = ps.id_product')
->where('ps.active = 1')
->where('ps.available_for_order = 1');
if ($cdiscountFeeProductId !== null) {
$sql->where('p.id_product <> ' . (int) $cdiscountFeeProductId);
}
if ($id_shop === null) {
$id_shop = (int) Configuration::get('PS_SHOP_DEFAULT');
}
$sql->where('ps.id_shop = ' . (int) $id_shop);
$product_feed_rule_filters = Configuration::getGlobalValue(Shoppingfeed::PRODUCT_FEED_RULE_FILTERS);
$product_visibility_nowhere = (bool) Configuration::getGlobalValue(Shoppingfeed::PRODUCT_VISIBILTY_NOWHERE);
$product_filters = json_decode($product_feed_rule_filters, true);
$sqlFilter = [];
if (is_array($product_filters)) {
foreach ($product_filters as $groupFilters) {
$groupFilterCollection = [];
foreach ($groupFilters as $filterMap) {
if (false === is_array($filterMap)) {
continue;
}
$type = key($filterMap);
$filter = $this->getFilterFactory()->getFilter($type, $filterMap[$type]);
$groupFilterCollection[] = $filter->getSqlChunk();
}
if (false === empty($groupFilterCollection)) {
$sqlFilter[] = implode(' AND ', $groupFilterCollection);
}
}
$sqlFilter = array_map(
function ($condition) {
return '(' . $condition . ')';
},
$sqlFilter
);
}
if (count($sqlFilter) > 0) {
$sql->where(implode(' OR ', $sqlFilter));
}
if ((bool) Configuration::getGlobalValue(Shoppingfeed::PRODUCT_FEED_SYNC_PACK) !== true) {
$sql->where('p.cache_is_pack = 0');
}
if ($product_visibility_nowhere === false) {
$sql->where("ps.visibility != 'none'");
}
Hook::exec('ShoppingfeedSqlProductsOnFeed',
[
'id_shop' => $id_shop,
'sql' => &$sql,
]
);
return $sql;
}
/****************************** Stock hook *******************************/
/**
* Saves a product for stock synchronization, or synchronizes it directly
* using the Actions handler
*
* @param array $params The hook parameters
*
* @throws Exception
*/
public function hookActionUpdateQuantity($params)
{
$this->updateShoppingFeedPreloading([$params['id_product']], ShoppingfeedPreloading::ACTION_SYNC_STOCK);
if (!Configuration::get(Shoppingfeed::STOCK_SYNC_ENABLED)) {
return;
}
$id_product = $params['id_product'];
$id_product_attribute = $params['id_product_attribute'];
try {
$handler = new ShoppingfeedClasslib\Actions\ActionsHandler();
$handler
->setConveyor([
'id_product' => $id_product,
'id_product_attribute' => $id_product_attribute,
'product_action' => ShoppingfeedProduct::ACTION_SYNC_STOCK,
])
->addActions('saveProduct')
->process('shoppingfeedProductSyncStock');
} catch (Exception $e) {
ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerHandler::logInfo(
sprintf(
$this->l('Product %s not registered for synchronization: %s', 'ShoppingfeedProductSyncActions'),
$id_product . ($id_product_attribute ? '_' . $id_product_attribute : ''),
$e->getMessage() . ' ' . $e->getFile() . ':' . $e->getLine()
),
'Product',
$id_product
);
}
ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerHandler::closeLogger();
}
/****************************** Prices hooks ******************************/
/* We'll have to check for products updates and combinations updates.
* For each object, we'll use the "UpdateBefore" hooks.
* We won't check the "Add" and "Delete" hooks, since the export module
* should export the first prices and the deletion from the catalog.
* If we're using realtime sync, the SF call will be done in the
* "UpdateAfter" hooks, otherwise we'll send non-updated values.
*/
/**
* Compares an updated product's price with its old price. If the new price
* is different, saves the product for price synchronization.
*
* @param array $params The hook parameters
*/
public function hookActionObjectProductUpdateBefore($params)
{
if (!Configuration::get(Shoppingfeed::PRICE_SYNC_ENABLED)) {
return;
}
$product = $params['object'];
if (!Validate::isLoadedObject($product)) {
return;
}
// Retrieve previous values in DB
// If all goes well, they should already be cached...
$old_product = new Product($product->id);
if ((float) $old_product->price == (float) $product->price) {
return;
}
try {
$handler = new ShoppingfeedClasslib\Actions\ActionsHandler();
$handler
->setConveyor([
'id_product' => $product->id,
'product_action' => ShoppingfeedProduct::ACTION_SYNC_PRICE,
])
->addActions('saveProduct')
->process('shoppingfeedProductSyncPrice');
} catch (Exception $e) {
ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerHandler::logInfo(
sprintf(
$this->l('Product %s not registered for synchronization: %s', 'ShoppingfeedProductSyncActions'),
$product->id,
$e->getMessage() . ' ' . $e->getFile() . ':' . $e->getLine()
),
'Product',
$product->id
);
}
if (!ShoppingfeedClasslib\Registry::isRegistered('updated_product_prices_ids')) {
ShoppingfeedClasslib\Registry::set('updated_product_prices_ids', []);
}
$updatedProductPricesIds = ShoppingfeedClasslib\Registry::get('updated_product_prices_ids');
$updatedProductPricesIds[] = $product->id;
ShoppingfeedClasslib\Registry::set('updated_product_prices_ids', $updatedProductPricesIds);
ShoppingfeedClasslib\Extensions\ProcessLogger\ProcessLoggerHandler::closeLogger();
// Combinations hook are not called when saving the product on 1.6
if (version_compare(_PS_VERSION_, '1.7', '<')) {
$attributes = $product->getAttributesResume(Context::getContext()->language->id);
if (is_array($attributes) && false == empty($attributes)) {
foreach ($attributes as $attribute) {
$this->hookActionObjectCombinationUpdateBefore([