fgc-modify-active-campaign.php
33.6 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
<?php
/**
* Plugin Name: Fgc Modify Active Campaign
* Plugin URI: http://fgc.vn
* Description: Fgc Modify Active Campaign
* Author: Phuong An.
* Author URI: http://fgc.vn
* Version: 1.0
*
* Copyright 2016 Prospress, Inc. (email : freedoms@prospress.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions
* @author Phuong An
* @since 1.0
*/
require_once(WP_CONTENT_DIR . "/plugins/pl-sync-ac/activecampaign/includes/ActiveCampaign.class.php");
if (!class_exists('FgcRequest')) {
require( 'request.class.php' );
}
class FGC_ActiveCampaign {
/**
*
* @return \ActiveCampaign
*/
public static function ac() {
$setting_ac = get_option("settings_activecampaign");
return new ActiveCampaign($setting_ac["api_url"], $setting_ac["api_key"]);
}
/**
* Sync to activecampaign by User ID
* @param type $user_id
* @param type $contact
*/
public static function sync_contact_by_user_id($user_id, $contact) {
$user = get_userdata($user_id);
$contact["email"] = $user->user_login;
$res = self::ac()->api("contact/sync", $contact);
}
/**
* Sync to activecampaign by Email
* @param type $email
* @param type $contact
*/
public static function sync_contact_by_email($email, $contact) {
if (count($contact) > 0) {
$contact["email"] = $email;
$res = self::ac()->api("contact/sync", $contact);
}
}
/**
* Get contact by email
*
* @param type $email
* @return type $contact
*/
public static function get_contact_by_email($email) {
return self::ac()->api("contact/view?email=$email");
}
/**
* Get contact by user ID
*
* @param type $email
* @return type $contact
*/
public static function get_contact_by_user_id($user_id) {
$user = get_userdata($user_id);
return self::get_contact_by_email($user->user_login);
}
/**
* Get field by user ID
*
* @param type $email
* @return type $contact
*/
public static function get_value_of_field_by_user_id($user_id, $field_name) {
$contact = self::get_contact_by_user_id($user_id);
foreach ($contact->fields as $field) {
if ($field->tag == '%' . $field_name . '%') {
return $field->val;
}
}
}
/**
* Get field by email
*
* @param type $email
* @return type $contact
*/
public static function get_value_of_field_by_email($email, $field_name) {
$contact = self::get_contact_by_email($email);
foreach ($contact->fields as $field) {
if ($field->tag == '%' . $field_name . '%') {
return $field->val;
}
}
}
/**
* Remove Tag by User ID
* @param type $tag
* @param type $user_id
*/
public static function remove_tag_by_user_id($tag, $user_id) {
$user = get_userdata($user_id);
$contact["email"] = $user->user_login;
$contact["tags"] = $tag;
$res = self::ac()->api("contact/tag/remove", $contact);
}
/**
* Remove Tags by User ID
* @param type $tag
* @param type $user_id
*/
public static function remove_tags_by_user_id($tags, $user_id) {
$user = get_userdata($user_id);
$contact["email"] = $user->user_login;
$contact["tags"] = $tags;
$res = self::ac()->api("contact/tag/remove", $contact);
}
/**
* Add Tag by User ID
* @param type $tag
* @param type $user_id
*/
public static function add_tag_by_user_id($tag, $user_id) {
$user = get_userdata($user_id);
$contact["email"] = $user->user_login;
$contact["tags"] = $tag;
$res = self::ac()->api("contact/tag/add", $contact);
}
/**
* Add Tag by Email
* @param type $tag
* @param type $email
*/
public static function add_tag_by_email($tag, $email) {
$contact["email"] = $email;
$contact["tags"] = $tag;
$res = self::ac()->api("contact/tag/add", $contact);
}
/**
* Get Tags by Email
* @param type $email
* @return type
*/
public static function get_tags_by_email($email) {
return self::get_contact_by_email($email)->tags;
}
/**
* Check Tag is exist by Email
* @param type $tag
* @param type $email
* @return type
*/
public static function check_tag_is_exist_by_email($tag, $email) {
return in_array($tag, self::get_tags_by_email($email));
}
/**
* Check Subscription is active by User ID
* @param type $user_id
* @return boolean
*/
public static function check_active_subscription_by_user_id($user_id) {
if ($user_id) {
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `is_take_month_off` = 1 AND `user_id` = $user_id ";
$results = $wpdb->get_results($query);
if (empty($results)) {
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id);
if (!empty($subscriptions) && count($subscriptions) > 0) {
foreach ($subscriptions as $subscription) {
if ($subscription['status'] == 'active') {
return true;
}
}
}
} else {
return true;
}
}
return false;
}
/**
* Get status and current drink of subcription.
* @global type $wpdb
* @param type $user_id
* @return type
*/
public static function get_info_of_active_or_onhold_subscription($user_id) {
$return = array('has_subscription' => false, 'status' => '', 'current_drink' => 0, 'time_reactive' => 0, 'variation_id' => 0);
if ($user_id) {
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `is_take_month_off` = 1 AND `user_id` = $user_id ";
$results = $wpdb->get_results($query);
if (empty($results)) {
$status_check = 'active';
} else {
$time_reactive = $results[0]->date_active;
$status_check = 'on-hold';
}
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id);
if (!empty($subscriptions) && count($subscriptions) > 0) {
foreach ($subscriptions as $subscription) {
if ($subscription['status'] == $status_check) {
$current_drink = (int) get_post_meta($subscription['variation_id'], 'attribute_drinks')[0];
$return = array('has_subscription' => true, 'status' => $status_check, 'current_drink' => $current_drink, 'time_reactive' => $time_reactive, 'variation_id' => $subscription['variation_id']);
break;
}
}
}
}
return $return;
}
/**
* Edit email after customer change their email.
*
* @param type $old_email
* @param type $new_email
* @return boolean
*/
public static function edit_email_by_contact_id($old_email, $new_email) {
if ($old_email && $new_email) {
$contact = self::get_contact_by_email($old_email);
$contact_id = $contact->id;
$contact->email = $new_email;
$res = self::ac()->api("contact/edit", (array) $contact);
}
return false;
}
/**
* Add lead sources by email
*
* @param type $email
* @param type $lead_source_value
*/
public static function fgc_add_lead_sources($email, $lead_source_value) {
if (self::get_value_of_field_by_email($email, 'LEAD_SOURCE') == '') {
$contact["field[%LEAD_SOURCE%,0]"] = $lead_source_value;
self::sync_contact_by_email($email, $contact);
}
}
}
add_action('user_register', 'fgc_set_cookie_after_register', 10, 1);
/**
* sync to activecampaign after registration account
* author: Ho Ngoc Hang
* @param type $user_id
*/
function fgc_set_cookie_after_register($user_id) {
setcookie("UNIQUE_REFERRAL_URL", '1', time() + 3600, '/');
setcookie("isNewUser", '1', time() + 3600, '/');
}
add_action('wp_loaded', 'fgc_update_active_campaign_tag_after_cancelled', 99);
function fgc_update_active_campaign_tag_after_cancelled() {
if (isset($_GET['cancelled_subscription']) && isset($_GET['subscription_id']) && isset($_GET['_wpnonce'])) {
$user_id = get_current_user_id();
FGC_ActiveCampaign::remove_tag_by_user_id('PURCHASE: Subscription', $user_id);
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: Cancellation', $user_id);
// Cancellation Reason
if ($_POST['cancel_other'] !== '') {
$cancellation_reason = $_POST['cancel_other'];
} else {
$cancellation_reason = $_POST['cancel_reason'];
}
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, array('field[%REASON_CANCELLATION%,0]' => $cancellation_reason));
}
}
add_action('woocommerce_checkout_update_order_meta', 'fgc_remove_event_cancelled_tag');
function fgc_remove_event_cancelled_tag($order_id) {
$order = wc_get_order($order_id);
$user_id = get_current_user_id();
foreach ($order->get_items() as $key => $value) {
$_product = $order->get_product_from_item($value);
if ($_product->product_type == 'subscription_variation') {
$customer_email = $order->billing_email;
FGC_ActiveCampaign::remove_tag_by_user_id('SUBSCRIPTION: Cancellation', $user_id);
}
}
// FGC_ActiveCampaign::remove_tags_by_user_id("CHECKOUT: Abandon Zero Completed,CHECKOUT: Abandoned Partially Completed,CHECKOUT: Abandoned Fully Completed", $user_id);
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandon Zero Completed', $user_id);
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandoned Partially Completed', $user_id);
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandoned Fully Completed', $user_id);
}
add_action('init', 'fgc_add_checkout_abandon_tag', 99);
function fgc_add_checkout_abandon_tag() {
$request_uri = $_SERVER["REQUEST_URI"];
$user_id = get_current_user_id();
if (strpos($request_uri, '/checkout') !== false && strpos($request_uri, 'order-received') == false && !FGC_ActiveCampaign::check_active_subscription_by_user_id($user_id)) {
if (is_admin() && defined('DOING_AJAX') && DOING_AJAX) {
} else {
setcookie("firstLoadCheckoutPage", '1', time() + 3600, '/');
}
} else {
if (isset($_REQUEST['action'])) {
if ($_REQUEST['action'] == 'eventAbandonPartiallyCompleted') {
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandon Zero Completed', $user_id);
FGC_ActiveCampaign::add_tag_by_user_id('CHECKOUT: Abandoned Partially Completed', $user_id);
exit();
}
if ($_REQUEST['action'] == 'eventAbandonFullyCompleted') {
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandon Zero Completed', $user_id);
FGC_ActiveCampaign::remove_tag_by_user_id('CHECKOUT: Abandoned Partially Completed', $user_id);
FGC_ActiveCampaign::add_tag_by_user_id('CHECKOUT: Abandoned Fully Completed', $user_id);
exit();
}
}
}
}
add_action('init', 'fgc_success_message_free_sample_form', 99);
function fgc_success_message_free_sample_form() {
if (isset($_REQUEST['action']) && isset($_REQUEST['email'])) {
if ($_REQUEST['action'] == 'getSuccessMessageFreeSampleForm' && $_REQUEST['email'] != '') {
$_return = array('message' => '', 'emptyTag' => 'true');
if (FGC_ActiveCampaign::check_tag_is_exist_by_email('EVENT: Free Sample Requested', $_REQUEST['email'])) {
setcookie("message_free_sample_ac", 'Thanks. We already have your details. Your free sample will be on its way shortly.', time() + 3600, '/');
exit();
} else {
if (FGC_ActiveCampaign::check_tag_is_exist_by_email('EVENT: Free Sample Sent', $_REQUEST['email'])) {
setcookie("message_free_sample_ac", "Thanks for your submission. Our records show that we have already sent a free sample. Unfortunately we can only send one per person. If you didn't receive your sample, please email steve@reize.com.au", time() + 3600, '/');
exit();
} else {
FGC_ActiveCampaign::add_tag_by_email('EVENT: Free Sample Requested', $_REQUEST['email']);
FGC_ActiveCampaign::fgc_add_lead_sources($_REQUEST['email'], 'Website Free Sample Form');
setcookie("message_free_sample_ac", "Thanks. Your free sample will be on its way shortly.", time() + 3600, '/');
exit();
}
}
}
}
}
//add_action('woocommerce_payment_complete', 'fgc_add_tag_is_upgrade_or_downgrade', 10);
function fgc_add_tag_is_upgrade_or_downgrade($order_id, $user_id) {
$subscription_id = get_post_meta($order_id, '_subscription_switch', true);
if ($subscription_id != '') {
global $wpdb;
if ($user_id == 0) {
$user_id = get_current_user_id();
}
$orderItemsTable = $wpdb->prefix . 'woocommerce_order_items';
$query = "SELECT * FROM `$orderItemsTable` WHERE `order_item_type` = 'line_item_switched' AND `order_id` = $subscription_id ORDER BY `order_item_id` DESC ";
$result = $wpdb->get_results($query);
if (!empty($result)) {
$oldItemID = $result[0]->order_item_id;
if ($oldItemID != '') {
$query = "SELECT * FROM `$orderItemsTable` WHERE `order_item_type` = 'line_item' AND `order_id` = $subscription_id ORDER BY `order_item_id` DESC ";
$result = $wpdb->get_results($query);
if (!empty($result)) {
$newItemID = $result[0]->order_item_id;
}
if ($newItemID != '') {
$oldDrink = (int) wc_get_order_item_meta($oldItemID, 'drinks');
$newDrink = (int) wc_get_order_item_meta($newItemID, 'drinks');
if ($oldDrink > $newDrink) {
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: Downgrade', $user_id);
} else if ($oldDrink < $newDrink) {
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: Upgrade', $user_id);
}
}
}
}
}
}
add_action('woocommerce_payment_complete', 'fgc_add_session_order_id', 10);
function fgc_add_session_order_id($order_id) {
if (!session_id()) {
session_start();
}
$_SESSION['orderId'] = $order_id;
$user_id = get_current_user_id();
// FgcRequest::request(get_bloginfo('url') . '/?action=checkAfterPaymentComplete&orderid=' . $order_id . '&userid=' . $user_id);
fgc_add_tag_is_upgrade_or_downgrade($order_id, $user_id);
fgc_update_lifetime_fields($order_id);
fgc_update_affiliates_fields($order_id);
}
add_action('woocommerce_order_status_completed', 'fgc_update_recent_values');
function fgc_update_recent_values($order_id) {
$order = new WC_Order($order_id);
$user_id = $order->user_id;
$contact["field[%MOST_RECENT_ORDER_DATE%,0]"] = date('j/n/y');
$contact["field[%MOST_RECENT_ORDER_NUMBER%,0]"] = $order_id;
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
}
function fgc_update_lifetime_fields($order_id) {
// $order_id
// $_SESSION['orderId'] = $order_id;
$order = new WC_Order($order_id);
$user_id = $order->user_id;
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys(wc_get_order_statuses()),
));
$subscription_purchase_count = 0;
$one_time_purchase_count = 0;
$drinks = 0;
$_order_total = 0;
foreach ($customer_orders as $customer_order) {
$_order = new WC_Order($customer_order->ID);
if ($_order->post->post_parent > 0) {
continue;
}
if (get_post_meta($_order->id, '_payment_method', true) == '') {
continue;
}
foreach ($_order->get_items() as $item) {
$drink = 0;
if ($item['variation_id'] > 0) {
$drink = (int) $item['item_meta']['drinks'][0];
$subscription_purchase_count++;
} else {
$drink = (int) str_replace(' drinks', '', $item['name']);
$one_time_purchase_count++;
}
$drinks = $drinks + $drink;
}
$_order_total = $_order_total + (float) get_post_meta($_order->id, '_order_total', true);
}
$contact["field[%TOTAL_ORDER_COUNT%,0]"] = $subscription_purchase_count + $one_time_purchase_count;
$contact["field[%ONE_TIME_PURCHASE_COUNT%,0]"] = $one_time_purchase_count;
$contact["field[%SUBSCRIPTION_PURCHASE_COUNT%,0]"] = $subscription_purchase_count;
$contact["field[%LIFETIME_SACHETS_BOUGHT%,0]"] = $drinks;
$contact["field[%LIFETIME_DOLLARS_SPENT%,0]"] = $_order_total;
add_user_meta($user_id, '_LIFETIME_DOLLARS_SPENT', $_order_total);
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
}
add_action('init', 'fgc_update_all_lifetime_fields', 10);
/**
* Run after deployment
*/
function fgc_update_all_lifetime_fields() {
if (isset($_REQUEST['action'])) {
if ($_REQUEST['action'] == 'updateLifetimeFields') {
echo '<pre>';
$number = (int) $_REQUEST['number'];
if ($number < 1) {
echo 'error number';
}
$users = get_users();
$i = 0;
var_dump(count($users));
foreach ($users as $user) {
$i++;
if ((($number - 1) * 20) > $i) {
continue;
}
if (($number * 20) < $i) {
break;
}
$user_id = $user->ID;
// $user_id = $order->user_id;
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys(wc_get_order_statuses()),
));
if (count($customer_orders) == 0) {
continue;
}
$subscription_purchase_count = 0;
$one_time_purchase_count = 0;
$drinks = 0;
$_order_total = 0;
// var_dump($customer_orders);
foreach ($customer_orders as $customer_order) {
$_order = new WC_Order($customer_order->ID);
if ($_order->post->post_parent > 0) {
continue;
}
if (get_post_meta($_order->id, '_payment_method', true) == '') {
continue;
}
foreach ($_order->get_items() as $item) {
$drink = 0;
if ($item['variation_id'] > 0) {
$drink = (int) $item['item_meta']['drinks'][0];
$subscription_purchase_count++;
} else {
$drink = (int) str_replace(' drinks', '', $item['name']);
$one_time_purchase_count++;
}
$drinks = $drinks + $drink;
}
$_order_total = $_order_total + (float) get_post_meta($_order->id, '_order_total', true);
}
$contact["field[%TOTAL_ORDER_COUNT%,0]"] = $subscription_purchase_count + $one_time_purchase_count;
$contact["field[%ONE_TIME_PURCHASE_COUNT%,0]"] = $one_time_purchase_count;
$contact["field[%SUBSCRIPTION_PURCHASE_COUNT%,0]"] = $subscription_purchase_count;
$contact["field[%LIFETIME_SACHETS_BOUGHT%,0]"] = $drinks;
$contact["field[%LIFETIME_DOLLARS_SPENT%,0]"] = $_order_total;
add_user_meta($user_id, '_LIFETIME_DOLLARS_SPENT', $_order_total);
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
echo '<br/>$i = ' . $i . '<br/>';
var_dump($user_id);
var_dump($contact);
}
echo '<br/>Complete.';
exit();
}
if ($_REQUEST['action'] == 'updateLifetimeAffFields') {
if (class_exists('ClassProcessCronjob')) {
echo '<pre>';
$number = (int) $_REQUEST['number'];
if ($number < 1) {
echo 'error number';
}
// $list_order_ids = explode(',', "");
// foreach ($list_order_ids as $order_id) {
$users = get_users_of_blog();
$i = 0;
var_dump(count($users));
foreach ($users as $user) {
$i++;
if ((($number - 1) * 20) > $i) {
continue;
}
if (($number * 20) < $i) {
break;
}
$user_id = $user->ID;
// $user_id = $order->user_id;
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys(wc_get_order_statuses()),
));
if (count($customer_orders) == 0) {
continue;
}
foreach ($customer_orders as $customer_order) {
$list_order_ids[] = $customer_order->ID;
break;
}
$subscription_purchase_count = 0;
$one_time_purchase_count = 0;
$drinks = 0;
$_order_total = 0;
// var_dump($customer_orders);
foreach ($customer_orders as $customer_order) {
$order_id = $customer_order->ID;
$order = new WC_Order($order_id);
$user_id = $order->user_id;
$listReferrals = ClassProcessCronjob::getListReferralsByAffiliateID($order_id);
$affiliateUserId = ClassProcessCronjob::getAffiliateUserID($order_id);
$REFERREE_VALUE = 0;
foreach ($listReferrals as $referral) {
$REFERREE_VALUE += (float) get_user_meta($referral->user_id, '_LIFETIME_DOLLARS_SPENT', true);
}
$aff_contact["field[%_OF_REFERRALS%,0]"] = count($listReferrals);
$aff_contact["field[%REFERREE_VALUE%,0]"] = $REFERREE_VALUE;
FGC_ActiveCampaign::sync_contact_by_user_id($affiliateUserId, $aff_contact);
$userAffiliate = get_userdata($affiliateUserId);
$contact["field[%REFERRING_SOURCE%,0]"] = $userAffiliate->user_login;
// $contact["field[%UNIQUE_REFERRAL_URL%,0]"] = get_user_meta($user_id, '_affiliate_code', true);
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
$affiliateAndReferralUserId = array($user_id, $affiliateUserId);
foreach ($affiliateAndReferralUserId as $userId) {
$contact = array();
$TOTAL_REFERRAL_CREDIT_EARNED = ClassProcessCronjob::getTotalAmountEarnOfCustomer($userId);
$TOTAL_REFERRAL_CREDIT_EARNED = ($TOTAL_REFERRAL_CREDIT_EARNED) ? $TOTAL_REFERRAL_CREDIT_EARNED : 0;
$contact["field[%TOTAL_REFERRAL_CREDIT_EARNED%,0]"] = $TOTAL_REFERRAL_CREDIT_EARNED;
$REFERRAL_CREDIT_PAID_OUT_SO_FAR = ClassProcessCronjob::getTotalUsedOfCustomer($userId);
$REFERRAL_CREDIT_PAID_OUT_SO_FAR = ($REFERRAL_CREDIT_PAID_OUT_SO_FAR) ? $REFERRAL_CREDIT_PAID_OUT_SO_FAR : 0;
$contact["field[%REFERRAL_CREDIT_PAID_OUT_SO_FAR%,0]"] = $REFERRAL_CREDIT_PAID_OUT_SO_FAR;
$REFERRAL_CREDIT_REMAINING = ClassProcessCronjob::getTotalRemainOfCustomer($userId);
$REFERRAL_CREDIT_REMAINING = ($REFERRAL_CREDIT_REMAINING) ? $REFERRAL_CREDIT_REMAINING : 0;
$contact["field[%REFERRAL_CREDIT_REMAINING%,0]"] = $REFERRAL_CREDIT_REMAINING;
FGC_ActiveCampaign::sync_contact_by_user_id($userId, $contact);
}
break;
}
echo '<br/>$i = ' . $i . '<br/>';
var_dump($user_id);
var_dump(count($listReferrals));
var_dump($REFERREE_VALUE);
}
} else {
echo 'not found class';
}
echo '<br/>Complete.';
exit();
}
if ($_REQUEST['action'] == 'updateUniqueCouponCode') {
$number = (int) $_REQUEST['number'];
if ($number < 1) {
echo 'error number';
}
echo '<pre>';
$users = get_users();
var_dump(count($users));
$i = 0;
foreach ($users as $user) {
$i++;
if ((($number - 1) * 20) > $i) {
continue;
}
if (($number * 20) < $i) {
break;
}
$user_id = $user->ID;
$contact["field[%UNIQUE_CUSTOMER_COUPON_CODE%,0]"] = get_user_meta($user_id, '_affiliate_code', true);
if ($contact["field[%UNIQUE_CUSTOMER_COUPON_CODE%,0]"] != '') {
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
}
echo '<br/>$i = ' . $i . '<br/>';
var_dump($user_id);
var_dump($contact["field[%UNIQUE_CUSTOMER_COUPON_CODE%,0]"]);
}
echo '<br/>Complete.';
exit();
}
if ($_REQUEST['action'] == 'checkTagsFreeSample') {
$_REQUEST['email'] = 'tester.phuongan.2911815@gmail.com';
$_return = array('message' => '', 'emptyTag' => 'true');
if (FGC_ActiveCampaign::check_tag_is_exist_by_email('EVENT: Free Sample Requested', $_REQUEST['email'])) {
$_return['emptyTag'] = 'false';
$_return['message'] = "Thanks. We already have your details. Your free sample will be on its way shortly.";
} else if (FGC_ActiveCampaign::check_tag_is_exist_by_email('EVENT: Free Sample Sent', $_REQUEST['email'])) {
$_return['emptyTag'] = 'false';
$_return['message'] = "Thanks for your submission. Our records show that we have already sent a free sample. Unfortunately we can only send one per person. If you didn't receive your sample, please email steve@reize.com.au";
} else {
FGC_ActiveCampaign::add_tag_by_email('EVENT: Free Sample Requested', $_REQUEST['email']);
FGC_ActiveCampaign::fgc_add_lead_sources($_REQUEST['email'], 'Website Free Sample Form');
$_return['message'] = "Thanks. Your free sample will be on its way shortly.";
}
echo json_encode($_return);
exit();
}
}
}
//add_action('woocommerce_payment_complete', 'fgc_update_affiliates_fields', 60);
function fgc_update_affiliates_fields($order_id) {
if (class_exists('ClassProcessCronjob')) {
$order = new WC_Order($order_id);
$user_id = $order->user_id;
$listReferrals = ClassProcessCronjob::getListReferralsByAffiliateID($order_id);
$affiliateUserId = ClassProcessCronjob::getAffiliateUserID($order_id);
$REFERREE_VALUE = 0;
if (!empty($listReferrals)) {
foreach ($listReferrals as $referral) {
$REFERREE_VALUE += (float) get_user_meta($referral->user_id, '_LIFETIME_DOLLARS_SPENT', true);
}
}
$aff_contact["field[%_OF_REFERRALS%,0]"] = count($listReferrals);
$aff_contact["field[%REFERREE_VALUE%,0]"] = $REFERREE_VALUE;
FGC_ActiveCampaign::sync_contact_by_user_id($affiliateUserId, $aff_contact);
$userAffiliate = get_userdata($affiliateUserId);
$contact["field[%REFERRING_SOURCE%,0]"] = $userAffiliate->user_login;
// $contact["field[%UNIQUE_REFERRAL_URL%,0]"] = get_user_meta($user_id, '_affiliate_code', true);
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
$affiliateAndReferralUserId = array($user_id, $affiliateUserId);
foreach ($affiliateAndReferralUserId as $userId) {
$contact = array();
$TOTAL_REFERRAL_CREDIT_EARNED = ClassProcessCronjob::getTotalAmountEarnOfCustomer($userId);
$TOTAL_REFERRAL_CREDIT_EARNED = ($TOTAL_REFERRAL_CREDIT_EARNED) ? $TOTAL_REFERRAL_CREDIT_EARNED : 0;
$contact["field[%TOTAL_REFERRAL_CREDIT_EARNED%,0]"] = $TOTAL_REFERRAL_CREDIT_EARNED;
$REFERRAL_CREDIT_PAID_OUT_SO_FAR = ClassProcessCronjob::getTotalUsedOfCustomer($userId);
$REFERRAL_CREDIT_PAID_OUT_SO_FAR = ($REFERRAL_CREDIT_PAID_OUT_SO_FAR) ? $REFERRAL_CREDIT_PAID_OUT_SO_FAR : 0;
$contact["field[%REFERRAL_CREDIT_PAID_OUT_SO_FAR%,0]"] = $REFERRAL_CREDIT_PAID_OUT_SO_FAR;
$REFERRAL_CREDIT_REMAINING = ClassProcessCronjob::getTotalRemainOfCustomer($userId);
$REFERRAL_CREDIT_REMAINING = ($REFERRAL_CREDIT_REMAINING) ? $REFERRAL_CREDIT_REMAINING : 0;
$contact["field[%REFERRAL_CREDIT_REMAINING%,0]"] = $REFERRAL_CREDIT_REMAINING;
FGC_ActiveCampaign::sync_contact_by_user_id($userId, $contact);
}
}
}
add_action('init', 'fgc_send_ajax_after_load_page', 10);
//add_action('plugins_loaded', 'fgc_send_ajax_after_load_page', 10);
function fgc_send_ajax_after_load_page() {
// if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'updateACAfterRegister') {
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'updateACAfterRegister') {
$contact = array();
$user_id = get_current_user_id();
// setcookie("isNewUser", '', time() - 3600, '/');
$contact['field[%UNIQUE_REFERRAL_URL%,0]'] = do_shortcode('[affiliates_url]');
$contact['field[%FULL_TWITTER_SHARE_URL%,0]'] = "https://twitter.com/intent/tweet?text=I%60m%20LOVING%20%40reizeenergy%21%20Home%20delivered%20%23energydrinks.%20Save%2025%25%20with%20this%20link%20" . rawurlencode(do_shortcode('[affiliates_url]')) . "%20%23energydrink%20pic.twitter.com%2FirO6DrDLOt";
$contact['field[%FULL_FACEBOOK_SHARE_URL%,0]'] = "https://www.facebook.com/sharer.php?u=" . rawurlencode(do_shortcode('[affiliates_url]')) . "&t=Your%20Account";
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, $contact);
$user = get_userdata($user_id);
FGC_ActiveCampaign::fgc_add_lead_sources($user->user_email, 'Website Registration');
// setcookie("UNIQUE_REFERRAL_URL", '', time() - 3600, '/');
exit();
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'firstLoadCheckoutPage') {
$user_id = get_current_user_id();
FGC_ActiveCampaign::add_tag_by_user_id('CHECKOUT: Abandon Zero Completed', $user_id);
FGC_ActiveCampaign::sync_contact_by_user_id($user_id, array('p[3]' => 3));
exit();
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'eventClickToShareChallenge') {
$user_id = get_current_user_id();
if ($user_id > 0) {
FGC_ActiveCampaign::add_tag_by_user_id("EVENT: Click to share the REIZE Challenge", $user_id);
}
exit();
}
if (isset($_COOKIE['DEADPAN_EMAIL']) && $_COOKIE['DEADPAN_EMAIL'] != '') {
if (!empty($_REQUEST['ac-thank-you']) && $_REQUEST['ac-thank-you'] == 1) {
FGC_ActiveCampaign::fgc_add_lead_sources($_COOKIE['DEADPAN_EMAIL'], 'DPG Page');
}
setcookie("DEADPAN_EMAIL", '', time() - 3600, '/');
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'checkAfterPaymentComplete' && isset($_REQUEST['orderid']) && isset($_REQUEST['userid'])) {
$order_id = $_REQUEST['orderid'];
$user_id = $_REQUEST['userid'];
fgc_add_tag_is_upgrade_or_downgrade($order_id, $user_id);
fgc_update_lifetime_fields($order_id);
fgc_update_affiliates_fields($order_id);
exit();
}
}