forked from thinkshout/mailchimp_ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailchimp_ecommerce.module
1262 lines (1139 loc) · 39.3 KB
/
mailchimp_ecommerce.module
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
/**
* @file
* MailChimp eCommerce core functionality.
*/
/**
* Implements hook_menu().
*/
function mailchimp_ecommerce_menu() {
$items = array();
$items['admin/config/services/mailchimp/ecommerce'] = array(
'title' => 'eCommerce',
'description' => 'Configure MailChimp eCommerce.',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailchimp_ecommerce_admin_settings'),
'access callback' => 'mailchimp_apikey_ready_access',
'access arguments' => array('administer mailchimp'),
'file' => 'includes/mailchimp_ecommerce.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items['admin/config/services/mailchimp/ecommerce/sync'] = array(
'title' => 'eCommerce data sync',
'description' => 'Sync eCommerce data to MailChimp.',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailchimp_ecommerce_admin_sync'),
'access callback' => 'mailchimp_apikey_ready_access',
'access arguments' => array('administer mailchimp'),
'file' => 'includes/mailchimp_ecommerce.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 21,
);
$items['admin/config/services/mailchimp/ecommerce/sync-orders'] = array(
'title' => 'eCommerce historical orders sync',
'description' => 'Sync past eCommerce order data to MailChimp.',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailchimp_ecommerce_admin_sync_orders'),
'access callback' => 'mailchimp_apikey_ready_access',
'access arguments' => array('administer mailchimp'),
'file' => 'includes/mailchimp_ecommerce.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 21,
);
return $items;
}
function mailchimp_ecommerce_use_queue() {
return variable_get('mailchimp_ecommerce_use_queue', FALSE);
}
/**
* Implements hook_queue_info().
*/
function mailchimp_ecommerce_cron_queue_info() {
$queues['mailchimp_ecommerce_ops'] = array(
'worker callback' => 'mailchimp_ecommerce_queue_process',
'time' => 60,
);
return $queues;
}
/**
* Callback to create a queue item.
*
* @param array $item
* Properties to send to the Queue API.
* - 'op' (string) The operation to be performed by the MailChimp API. This maps
* to the actual MailChimp API methods.
* - 'cart_id' (string) The ID of the current cart.
* - 'store_id' (string) The Store ID.
* - 'customer' (array) A fully loaded MailChimp $customer.
* - 'cart' For cart operations, this is the $order from Drupal.
* - 'order' For order operations, this is the $order from Drupal.
*/
function mailchimp_ecommerce_create_queue_item($item) {
$queue = DrupalQueue::get('mailchimp_ecommerce_ops');
$queue->createItem($item);
}
/**
* Worker function to process a MailChimp eCommerce queue item.
*
* @param DrupalQueueItem $item
* A Drupal Queue object.
*/
function mailchimp_ecommerce_queue_process($item) {
$list_id = mailchimp_ecommerce_get_list_id();
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
if (isset($item->customer)) {
$customer = $item->customer;
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
return;
}
if (!mailchimp_ecommerce_validate_customer($customer) && !$customer['email_address']) {
return;
}
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
}
switch ($item->op) {
case 'addCart':
case 'updateCart':
$mc_ecommerce->{$item->op}($item->store_id, $item->cart_id, $customer, $item->cart);
break;
case 'deleteCart':
$mc_ecommerce->{$item->op}($item->store_id, $item->cart_id);
break;
case 'addCartLine':
case 'updateCartLine':
$mc_ecommerce->{$item->op}($item->store_id, $item->cart_id, $item->line_id, $item->product);
break;
case 'deleteCartLine':
$mc_ecommerce->{$item->op}($item->store_id, $item->cart_id, $item->line_id);
break;
case 'addOrder':
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
// Increment customer totals
$remote_customer = mailchimp_ecommerce_get_customer($customer['id']);
$customer['orders_count'] = $remote_customer->orders_count + 1;
$customer['total_spent'] = $remote_customer->total_spent + $item->order['order_total'];
$mc_ecommerce->{$item->op}($item->store_id, $item->order_id, $customer, $item->order);
break;
case 'updateOrder':
$mc_ecommerce->{$item->op}($item->store_id, $item->order_id, $item->order);
break;
case 'deleteOrder':
$mc_ecommerce->{$item->op}($item->store_id, $item->order_id, $item->order);
// Decrement customer totals.
$remote_customer = mailchimp_ecommerce_get_customer($customer['id']);
$customer['orders_count'] = $remote_customer->orders_count - 1;
$customer['total_spent'] = $remote_customer->total_spent - $item->order['order_total'];
break;
}
}
/**
* Implements hook_page_build().
*/
function mailchimp_ecommerce_page_build(&$page) {
$campaign_id = isset($_GET['mc_cid']) ? check_plain($_GET['mc_cid']) : '';
if ($campaign_id) {
$_SESSION['mc_cid'] = $campaign_id;
}
}
/**
* Return information about the store from the supplied id.
*
* @param string $store_id
* The ID of the store.
*
* @return object
* MailChimp store object.
*/
function mailchimp_ecommerce_get_store($store_id) {
$store = NULL;
try {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$store = $mc_ecommerce->getStore($store_id);
}
catch (Exception $e) {
if ($e->getCode() == 404) {
// Store doesn't exist; no need to log an error.
}
else {
mailchimp_ecommerce_log_error_message('Unable to get store: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
return $store;
}
/**
* Add a new store to Mailchimp.
*
* @param string $store_id
* The ID of the store.
* @param array $store
* Associative array of store information.
* - list_id (string) The id for the list associated with the store.
* - name (string) The name of the store.
* - currency_code (string) The three-letter ISO 4217 code for the currency
* that the store accepts.
* @param string $platform
* The eCommerce platform being used to create this store.
* This module's submodules use 'Drupal Ubercart' and 'Drupal Commerce'.
*/
function mailchimp_ecommerce_add_store($store_id, $store, $platform) {
try {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$parameters = [
'platform' => $platform,
];
$mc_store = $mc_ecommerce->addStore($store_id, $store, $parameters);
module_invoke_all('mailchimp_ecommerce_add_store', $mc_store);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add a new store: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Update a store name or currency code.
*
* @param string $store_id
* The ID of the store.
* @param string $name
* The name of the store.
* @param string $currency_code
* The three-letter ISO 4217 code.
* @param string $platform
* The eCommerce platform being used to create this store.
* This module's submodules use 'Drupal Ubercart' and 'Drupal Commerce'.
*/
function mailchimp_ecommerce_update_store($store_id, $name, $currency_code, $platform) {
try {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$parameters = [
'platform' => $platform,
];
$mc_ecommerce->updateStore($store_id, $name, $currency_code, $parameters);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update a store: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Add a new customer to Mailchimp.
*
* @param array $customer
* Array of customer fields.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/customers/#create-post_ecommerce_stores_store_id_customers
*/
function mailchimp_ecommerce_add_customer($customer) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
$list_id = mailchimp_ecommerce_get_list_id();
if (empty($store_id)) {
throw new Exception('Cannot add a customer without a store ID.');
}
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$opt_in_status = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
$customer['opt_in_status'] = $opt_in_status;
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->addCustomer($store_id, $customer);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add a customer: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Read a customer from Mailchimp.
*
* @param string $customer_id
* Unique id of customer.
*
* @return object
* MailChimp customer object.
*/
function mailchimp_ecommerce_get_customer($customer_id) {
$customer = NULL;
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot get a customer without a store ID.');
}
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$customer = $mc_ecommerce->getCustomer($store_id, $customer_id);
}
catch (Exception $e) {
if ($e->getCode() == 404) {
// Customer doesn't exist in the store; no need to log an error.
}
else {
mailchimp_ecommerce_log_error_message('Unable to get a customer: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
return $customer;
}
/**
* Update a customer record in Mailchimp.
*
* @param array $customer
* Array of customer fields.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/customers/#create-post_ecommerce_stores_store_id_customers
*/
function mailchimp_ecommerce_update_customer($customer) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
$list_id = mailchimp_ecommerce_get_list_id();
if (empty($store_id)) {
throw new Exception('Cannot update a customer without a store ID.');
}
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
$mc_ecommerce->updateCustomer($store_id, $customer);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update a customer: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Delete a customer from Mailchimp.
*
* @param string $customer_id
* Unique id of customer.
*/
function mailchimp_ecommerce_delete_customer($customer_id) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot delete a customer without a store ID.');
}
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->deleteCustomer($store_id, $customer_id);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to delete a customer: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Adds a new cart to the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
* @param array $customer
* Associative array of customer information.
* - id (string): A unique identifier for the customer.
* @param array $cart
* Associative array of cart information.
* - currency_code (string): The three-letter ISO 4217 currency code.
* - order_total (float): The total for the order.
* - lines (array): An array of the order's line items.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/carts/#create-post_ecommerce_stores_store_id_carts
*/
function mailchimp_ecommerce_add_cart($cart_id, array $customer, array $cart) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot add a cart without a store ID.');
}
// Do nothing with no email.
if (!$customer['email_address']) {
return;
}
if (!mailchimp_ecommerce_validate_customer($customer)) {
// A user not existing in the store's MailChimp list is not an error, so
// don't throw an exception.
return;
}
$campaign_id = mailchimp_ecommerce_get_campaign_id();
if (!empty($campaign_id)) {
$cart['campaign_id'] = $campaign_id;
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'addCart',
'cart_id' => $cart_id,
'store_id' => $store_id,
'customer' => $customer,
'cart' => $cart,
]);
}
else {
$list_id = mailchimp_ecommerce_get_list_id();
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
$mc_ecommerce->addCart($store_id, $cart_id, $customer, $cart);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add a cart: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Updates an existing cart in the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
* @param array $customer
* Associative array of customer information.
* - id (string): A unique identifier for the customer.
* @param array $cart
* Associative array of cart information.
* - currency_code (string): The three-letter ISO 4217 currency code.
* - order_total (float): The total for the order.
* - lines (array): An array of the order's line items.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/carts/#edit-patch_ecommerce_stores_store_id_carts_cart_id
*/
function mailchimp_ecommerce_update_cart($cart_id, array $customer, array $cart) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot update a cart without a store ID.');
}
// Do nothing with no email.
if (!$customer['email_address']) {
return;
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'updateCart',
'cart_id' => $cart_id,
'store_id' => $store_id,
'customer' => $customer,
'cart' => $cart,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$list_id = mailchimp_ecommerce_get_list_id();
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
$parameters = [
'customer' => (object) $customer,
];
$parameters += $cart;
$mc_ecommerce->updateCart($store_id, $cart_id, $parameters);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update a cart: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Deletes a cart in the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
*/
function mailchimp_ecommerce_delete_cart($cart_id) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot delete a cart without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'deleteCart',
'cart_id' => $cart_id,
'store_id' => $store_id,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->deleteCart($store_id, $cart_id);
}
}
catch (Exception $e) {
if ($e->getCode() == 404) {
// Cart doesn't exist; no need to log an error.
}
else {
mailchimp_ecommerce_log_error_message('Unable to delete a cart: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
}
/**
* Adds a line to a cart in the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
* @param string $line_id
* A unique identifier for the order line item.
* @param array $product
* Associative array of product information.
* - product_id (string) The unique identifier for the product.
* - product_variant_id (string) The unique identifier for the variant.
* - quantity (int) The quantity of a cart line item.
* - price (float) The price of a cart line item.
*/
function mailchimp_ecommerce_add_cart_line($cart_id, $line_id, $product) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot add a cart line without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'addCartLine',
'cart_id' => $cart_id,
'store_id' => $store_id,
'line_id' => $line_id,
'product' => $product,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->addCartLine($store_id, $cart_id, $line_id, $product);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add a cart line: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Updates an existing line in a cart in the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
* @param string $line_id
* A unique identifier for the order line item.
* @param array $product
* Associative array of product information.
* - product_id (string) The unique identifier for the product.
* - product_variant_id (string) The unique identifier for the variant.
* - quantity (int) The quantity of a cart line item.
* - price (float) The price of a cart line item.
*/
function mailchimp_ecommerce_update_cart_line($cart_id, $line_id, $product) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot update a cart line without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'updateCartLine',
'cart_id' => $cart_id,
'store_id' => $store_id,
'line_id' => $line_id,
'product' => $product,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->updateCartLine($store_id, $cart_id, $line_id, $product);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update a cart line: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Deletes a line in a cart in the current MailChimp store.
*
* @param string $cart_id
* The cart ID.
* @param string $line_id
* A unique identifier for the order line item.
*/
function mailchimp_ecommerce_delete_cart_line($cart_id, $line_id) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot delete a cart line without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'deleteCartLine',
'cart_id' => $cart_id,
'store_id' => $store_id,
'line_id' => $line_id,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->deleteCartLine($store_id, $cart_id, $line_id);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to delete a cart line: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Gets an order from the current MailChimp store.
*
* @param string $order_id
* The order ID.
*
* @return object
* The order.
*/
function mailchimp_ecommerce_get_order($order_id, $log_404 = TRUE) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot get an order without a store ID.');
}
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$order = $mc_ecommerce->getOrder($store_id, $order_id);
return $order;
}
catch (Exception $e) {
if ($e->getCode() == 404 && !$log_404) {
// Order doesn't exist in the store; no need to log an error.
}
else {
mailchimp_ecommerce_log_error_message('Unable to get order: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
return NULL;
}
/**
* Adds a new order to the current MailChimp store.
*
* @param string $order_id
* The order ID.
* @param array $customer
* Associative array of customer information.
* - id (string): A unique identifier for the customer.
* @param array $order
* Associative array of order information.
* - currency_code (string): The three-letter ISO 4217 currency code.
* - order_total (float): The total for the order.
* - lines (array): An array of the order's line items.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/orders/#create-post_ecommerce_stores_store_id_orders
*/
function mailchimp_ecommerce_add_order($order_id, array $customer, array $order) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot add an order without a store ID.');
}
if (!mailchimp_ecommerce_validate_customer($customer)) {
// A user not existing in the store's MailChimp list is not an error, so
// don't throw an exception.
return;
}
$campaign_id = mailchimp_ecommerce_get_campaign_id();
if (!empty($campaign_id)) {
$order['campaign_id'] = $campaign_id;
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'addOrder',
'store_id' => $store_id,
'order_id' => $order_id,
'customer' => $customer,
'order' => $order,
]);
}
else {
$list_id = mailchimp_ecommerce_get_list_id();
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
// Increment customer totals
$remote_customer = mailchimp_ecommerce_get_customer($customer['id']);
$customer['orders_count'] = $remote_customer->orders_count + 1;
$customer['total_spent'] = $remote_customer->total_spent + $order['order_total'];
$mc_ecommerce->addOrder($store_id, $order_id, $customer, $order);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add an order: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Updates an existing order in the current MailChimp store.
*
* @param string $order_id
* The order ID.
* @param array $order
* Associative array of order information.
* - currency_code (string): The three-letter ISO 4217 currency code.
* - order_total (float): The total for the order.
* - lines (array): An array of the order's line items.
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/ecommerce/stores/orders/#edit-patch_ecommerce_stores_store_id_orders_order_id
*/
function mailchimp_ecommerce_update_order($order_id, array $order) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot update an order without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'updateOrder',
'order_id' => $order_id,
'store_id' => $store_id,
'order' => $order,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->updateOrder($store_id, $order_id, $order);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update an order: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Deletes an Order from the current MailChimp store.
*
* @param string $order_id
* The Order ID.
*/
function mailchimp_ecommerce_delete_order($order_id) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot delete an order without a store ID.');
}
if (mailchimp_ecommerce_use_queue()) {
mailchimp_ecommerce_create_queue_item([
'op' => 'deleteOrder',
'order_id' => $order_id,
'store_id' => $store_id,
]);
}
else {
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->deleteOrder($store_id, $order_id);
// Pull member information to get member status.
$memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
$customer['opt_in_status'] = (isset($memberinfo->status) && ($memberinfo->status == 'subscribed')) ? TRUE : FALSE;
// Decrement customer totals.
$remote_customer = mailchimp_ecommerce_get_customer($customer['id']);
$customer['orders_count'] = $remote_customer->orders_count - 1;
$customer['total_spent'] = $remote_customer->total_spent - $item->order['order_total'];
}
}
catch (Exception $e) {
if ($e->getCode() == 404) {
// Order doesn't exist; no need to log an error.
}
else {
mailchimp_ecommerce_log_error_message('Unable to delete an order: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
}
/**
* Adds a product to MailChimp.
*
* Adds a product variant if a product with the given ID exists.
*
* In MailChimp, each product requires at least one product variant. This
* function will create a single product variant when creating new products.
*
* A product variant is contained within a product and can be used to
* represent shirt size, color, etc.
*
* @param string $product_id
* Unique ID of the product.
* @param string $product_variant_id
* ID of the product variant.
* May be identical to $product_id for single products.
* @param string $title
* The product title.
* @param string $description
* The product description.
* @param string $type
* The product type.
* @param string $sku
* The product SKU.
* @param string $url
* The product URL.
* @param float $price
* The product price.
* @param string $image_url
* A URL to a representative product Image.
* @param int $stock
* If enabled, the current active inventory.
*/
function mailchimp_ecommerce_add_product($product_id, $product_variant_id, $title, $description, $type, $sku, $url, $price, $image_url = '', $stock = 1) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot add a product without a store ID.');
}
// TRUE when a new product is created, false if a product variant is added.
$new_product = FALSE;
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
// Create MailChimp product from product type.
try {
$mc_ecommerce->getProduct($store_id, $product_id);
}
catch (Exception $e) {
if ($e->getCode() == 404) {
// TODO check if product has adjustments with additional SKUs defined. Add these as variants instead of default
// No existing product; create new product with default variant.
$variant = [
'id' => $product_variant_id,
'title' => $title,
'sku' => $sku,
'url' => $url,
'price' => $price,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
];
$mc_ecommerce->addProduct($store_id, $product_id, $title, $url, [$variant], [
'description' => $description,
'type' => $type,
]);
$new_product = TRUE;
}
else {
// An actual error occurred; pass on the exception.
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
if (!$new_product) {
// Add a variant to an existing product.
$mc_ecommerce->addProductVariant($store_id, $product_id, [
'id' => $product_variant_id,
'title' => $title,
'sku' => $sku,
'price' => $price,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
]);
}
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to add a product: ' . $e->getMessage());
mailchimp_ecommerce_show_error($e->getMessage());
}
}
/**
* Updates an existing product in MailChimp.
*
* MailChimp only allows for product variants to be updated. The parent
* product cannot be changed once created. This function will update the
* variant associated with the given product ID and SKU.
*
* @param string $product_id
* Unique ID of the product.
* @param string $product_variant_id
* ID of the product variant.
* May be identical to $product_id for single products.
* @param string $title
* The product title.
* @param string $sku
* The product SKU.
* @param float $price
* The product price.
* @param string $image_url
* A URL to a representative product Image.
* @param int $stock
* If enabled, the current active inventory.
*/
function mailchimp_ecommerce_update_product($product_id, $product_variant_id, $title, $sku, $url, $price, $image_url = '', $stock = 1) {
try {
$store_id = mailchimp_ecommerce_get_store_id();
if (empty($store_id)) {
throw new Exception('Cannot update a product without a store ID.');
}
/* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
$mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
$mc_ecommerce->updateProductVariant($store_id, $product_id, $product_variant_id, [
'title' => $title,
'sku' => $sku,
'url' => $url,
'price' => $price,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
]);
}
catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update product: ' . $e->getMessage());
drupal_set_message($e->getMessage(), 'error');
}
}
/**
* Deletes a product in MailChimp.
*
* @param string $product_id
* Unique ID of the product.
*/
function mailchimp_ecommerce_delete_product($product_id) {
try {