woocommerce-variable-product-shipping.php
28 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
<?php
/**
* Plugin Name: Fgc WooCommerce Subscriptions Variable Product Shipping
* Plugin URI: http://fgc.vn
* Description: Fgc WooCommerce Subscriptions Variable Product Shipping.
* 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
*/
add_action('wp_head', 'load_custom_card_info', 80);
function load_custom_card_info() {
if (is_checkout()) {
$key = '_customer_card_info';
$value = get_user_meta(get_current_user_id(), $key, true);
if ($value != '') {
$card_info = explode('|', $value);
if ($card_info[0] == 'Visa') {
?>
<script>
jQuery(document).ready(function ($) {
if ($('#stripe-card-number').length > 0) {
$('#stripe-card-number').val('<?php echo trim($card_info[1], ' ') ?>');
}
});
</script>
<?php
}
}
}
}
//add_action('plugins_loaded', 'fgc_save_addresss', 80);
function fgc_get_shipping_rate() {
global $wpdb;
global $woocommerce;
$response = array();
$user_id = get_current_user_id();
$subscription_result = FGC_ActiveCampaign::get_info_of_active_or_onhold_subscription($user_id);
if ($subscription_result['has_subscription']) {
if ($subscription_result['status'] == 'active') {
$status_check = 'wc-active';
} else {
$status_check = 'wc-on-hold';
}
// Get Active Subscription ID
$query = "SELECT * FROM '.$wpdb->prefix . 'postmeta WHERE meta_key LIKE '_customer_user' AND meta_value LIKE '$user_id' ";
$result = $wpdb->get_results($query);
if (!empty($result)) {
foreach ($result as $value) {
$query_get_subscription_id = "SELECT post_status FROM '.$wpdb->prefix . 'posts WHERE ID = $value->post_id";
$result_get_subscription_id = $wpdb->get_results($query_get_subscription_id);
if (!empty($result_get_subscription_id)) {
if ($result_get_subscription_id[0]->post_status == $status_check) {
$subscription_id = $value->post_id;
}
}
}
}
// If active subscription is exist.
if (isset($subscription_id)) {
// $old_shipping_cost = get_post_meta($subscription_id, '_order_shipping');
// $old_total = get_post_meta($subscription_id, '_order_total');
$select_item_id = "SELECT order_item_id FROM `'.$wpdb->prefix . 'woocommerce_order_items` WHERE order_id = $subscription_id and order_item_type = 'line_item'";
$result_item_id = $wpdb->get_results($select_item_id);
if (count($result_item_id) > 0) {
if (isset($result_item_id[0]->order_item_id)) {
$item_id = $result_item_id[0]->order_item_id;
if ($item_id != '' && $item_id > 0) {
$select_item_price = "SELECT meta_value FROM `'.$wpdb->prefix . 'woocommerce_order_itemmeta` WHERE `order_item_id` = $item_id AND `meta_key` = '_line_total'";
$result_item_price = $wpdb->get_results($select_item_price);
if (count($result_item_price) > 0) {
$item_price = $result_item_price[0]->meta_value;
}
}
// var_dump($result_item_price);
}
}
if (isset($item_price) && $item_price != '' && $item_price > 0) {
// if ($old_shipping_cost) {
// $old_shipping_cost = (float) $old_shipping_cost[0];
// $old_total = (float) $old_total[0];
// Get product id and varition id.
$order = new WC_Order($subscription_id);
$items = $order->get_items();
foreach ($items as $item) {
foreach ($item['item_meta_array'] as $value) {
if ($value->key == '_product_id') {
$product_id = $value->value;
}
if ($value->key == '_variation_id') {
$variation_id = $value->value;
}
}
}
if (isset($product_id) && isset($variation_id)) {
$shipping_cost = 0;
$weight = get_post_meta($variation_id, '_weight')[0];
if ($weight == '') {
$weight = get_post_meta($product_id, '_weight')[0];
}
$weight = (float) $weight;
$result_shipping_zones = get_option('be_woocommerce_shipping_zones');
$shipping_country = $_POST['shipping_country'];
update_post_meta($subscription_id, '_shipping_country', $shipping_country);
if (count($result_shipping_zones)) {
foreach ($result_shipping_zones as $value) {
if ($value['zone_country'] == $shipping_country) {
$zone_id = $value['zone_id'];
}
}
}
if (isset($zone_id)) {
$result_shipping_rates_table = get_option('woocommerce_table_rates');
// var_dump($result_shipping_rates_table);
foreach ($result_shipping_rates_table as $value) {
if ($value['zone'] == $zone_id && $value['cond'] == 'weight') {
if ($weight >= $value['min'] && $weight <= $value['max']) {
$shipping_cost = $value['cost'];
// $shipping_label = $value['title'];
}
}
}
$shipping_label = $woocommerce->countries->countries[$shipping_country];
}
// $new_total = $old_total - $old_shipping_cost + (float) $shipping_cost;
$new_total = (float) $item_price + (float) $shipping_cost;
update_post_meta($subscription_id, '_order_shipping', $shipping_cost);
update_post_meta($subscription_id, '_order_total', $new_total);
if (!isset($shipping_label)) {
$shipping_label = "FREE";
}
update_post_meta($subscription_id, '_order_shipping_label', $shipping_label);
$response['shippingcost'] = $shipping_cost;
$response['shippinglabel'] = $shipping_label;
$response['total'] = $new_total;
}
}
}
if (isset($shipping_cost)) {
$response['shippingcost'] = $shipping_cost;
} else {
$response['shippingcost'] = 0;
}
if (isset($shipping_label)) {
$response['shippinglabel'] = $shipping_label;
} else {
$response['shippinglabel'] = 'Australia';
}
if (isset($new_total)) {
$response['total'] = $new_total;
} else {
$response['total'] = 0;
}
}
return $response;
// echo json_encode($response);
// exit();
// }
}
add_action('plugins_loaded', 'fgc_check_pass', 99);
function fgc_check_pass() {
if (isset($_GET['checkPass']) && $_GET['checkPass'] != '') {
$current_user = wp_get_current_user();
$current_pass = isset($current_user->user_pass) ? $current_user->user_pass : '';
if (!wp_check_password($_GET['checkPass'], $current_pass)) {
echo 'Incorrect';
} else {
echo 'Correct';
}
exit();
}
}
add_action('init', 'fgc_save_address', 99);
function fgc_save_address() {
if (isset($_GET['fgcSaveAddress']) && isset($_GET['type'])) {
if ($_GET['type'] == 'updateaccountinfo') {
echo json_encode(fgc_change_account_info());
} else {
fgc_custom_save_address($_GET['type']);
}
exit();
}
}
/**
* Proccess update user info
* @return string|array
*/
function fgc_change_account_info() {
$data = $_REQUEST;
$email = isset($_REQUEST['accout_emial']) ? $_REQUEST['accout_emial'] : '';
$old_email = isset($_REQUEST['old_customer_email']) ? $_REQUEST['old_customer_email'] : '';
$customer_id = isset($_REQUEST['customer_id']) ? $_REQUEST['customer_id'] : '';
$password = isset($_REQUEST['accout_password_new']) ? $_REQUEST['accout_password_new'] : '';
$card_number = isset($_REQUEST['accout_number']) ? $_REQUEST['accout_number'] : '';
$error = array();
$error['email'] = '';
$error['password'] = '';
$error['cardnumber'] = '';
if ($email) {
if (fgc_checkExistEmailChange($email, $old_email)) {
$error['email'] = 'Email is exist';
}
}
if ($password) {
if (!fgcCheckPassword($password)) {
$error['password'] = 'Password is must be greater than 6 character';
}
}
if ($error['email'] || $error['password']) {
$card_numberNew = fgc_get_cartnumber_info($customer_id);
$result = array('error' => $error['email'] . " " . $error['password'], 'content' => array('acount_email' => $old_email, 'card_number' => $card_numberNew));
return $result;
}
if (!$email) {
$result = array('error' => 'Email is not null', 'content' => array('acount_email' => $old_email, 'card_number' => $card_numberNew));
return $result;
}
if ($email && $email != $old_email) {
$result_email = fgcUpdateEmailByCustomerId($email, $old_email, $customer_id);
if (!$result_email['success']) {
$error['email'] = $result_email['message'];
} else {
FGC_ActiveCampaign::edit_email_by_contact_id($old_email, $email);
}
}
$oldPassword = fgcGetPassWordByCustomerId($customer_id);
$updatePassSuccess = false;
if ($password && md5($password) != $oldPassword) {
wp_set_password($password, $customer_id);
$updatePassSuccess = true;
//auto login after change password
$info_login = array();
$info_login['user_login'] = $email;
$info_login['user_password'] = $password;
$info_login['remember'] = true;
$user_signon = wp_signon($info_login, true);
wp_set_auth_cookie($user_signon->ID);
wp_set_current_user($user_signon->ID, $email);
}
// if( $data['card_num'] == "")
$new_card_num = $data['card_num'];
// $data['exp_month'] = isset($data['exp_month']) ? $data['exp_month'] : $data['exp_month_a'];
$data['exp_month'] = (isset($data['exp_month']) && $data['exp_month'] != '') ? $data['exp_month'] : $data['exp_month_a'];
$data['exp_year'] = (isset($data['exp_year']) && $data['exp_year'] != '') ? $data['exp_year'] : $data['exp_year_a'];
$cvc = $data['cvc'];
// var_dump($data);
if ($new_card_num != '' && $data['exp_month'] != '' && $data['exp_year'] != '' && $cvc != '') {
if (!is_numeric($result_save_card = fgcUpdateCardInfo($data))) {
$error['cardnumber'] = $result_save_card;
}
}
if (($new_card_num != '' && $cvc == '') || ($new_card_num == '' && $cvc != '')) {
$error['cardnumber'] = "Please fill entire card info";
}
ob_start();
include_once 'data_accountinfo.php';
$datahtml = ob_get_clean();
$emailInfo = fgGetEmailByCustomerId($customer_id);
if ($error['email'] || $error['password'] || ($error['cardnumber'] && $error['cardnumber'] != 'true')) {
$result = array('error' => $error['email'] . " " . $error['password'] . " " . $error['cardnumber'], 'content' => array('acount_email' => $emailInfo), 'datahtml' => $datahtml);
} else {
$result = array('error' => 'false', 'message' => 'Your changes have been saved', 'content' => array('acount_email' => $emailInfo, 'update_pass_success' => $updatePassSuccess), 'datahtml' => $datahtml);
}
return $result;
}
/**
* Get card infomation
* @param type $customer_id
* @return string
*/
function fgc_get_cartnumber_info($customer_id) {
$key = '_customer_card_info';
$value = get_user_meta($customer_id, $key, true);
if (!$value) {
$stripe = new WC_Gateway_Stripe();
$cards = $stripe->fgc_get_saved_cards($customer_id);
if (!empty($cards)) {
return $cards[0]->brand . "....." . $cards[0]->last4;
} else {
return '';
}
} else {
$values = explode('|', $value);
return $values[0] . "....." . substr($values[1], -4);
}
}
/**
*
* @global type $wpdb
* @param type $email
* @param type $old_email
* @return boolean
*/
function fgc_checkExistEmailChange($email, $old_email) {
if (trim($email) != trim($old_email)) {
global $wpdb;
$query = "SELECT * FROM $wpdb->users WHERE `user_email` = '$email'";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
*
* @global type $wpdb
* @param type $customerID
* @return boolean
*/
function fgGetEmailByCustomerId($customerID) {
global $wpdb;
$query = "SELECT * FROM $wpdb->users WHERE `ID` = '$customerID'";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
return $resut[0]->user_email;
} else {
return false;
}
}
/**
*
* @global type $wpdb
* @param type $customer_id
* @return boolean
*/
function fgcGetPassWordByCustomerId($customer_id) {
global $wpdb;
$query = "SELECT * FROM $wpdb->users WHERE `ID` = '$customer_id'";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
return $resut[0]->user_pass;
} else {
return false;
}
}
/**
*
* @param type $password
* @return boolean
*/
function fgcCheckPassword($password) {
if (strlen($password) >= 6) {
return true;
} else {
return false;
}
}
function fgcUpdateEmailByCustomerId($email, $old_email, $customer_id) {
global $wpdb;
$query = "SELECT * FROM $wpdb->users WHERE `ID` = '$customer_id'";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
if ($old_email == $resut[0]->user_email) {
$query = "UPDATE `$wpdb->users` SET `user_email` = '$email' WHERE `$wpdb->users`.`ID` = '$customer_id';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update user email.');
}
}
if ($old_email == $resut[0]->user_nicename) {
$query = "UPDATE `$wpdb->users` SET `user_nicename` = '$email' WHERE `$wpdb->users`.`ID` = '$customer_id';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update user nickname.');
}
}
if ($old_email == $resut[0]->user_login) {
$query = "UPDATE `$wpdb->users` SET `user_login` = '$email' WHERE `$wpdb->users`.`ID` = '$customer_id';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update user login.');
}
}
if ($old_email == $resut[0]->display_name) {
$query = "UPDATE `$wpdb->users` SET `display_name` = '$email' WHERE `$wpdb->users`.`ID` = '$customer_id';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update display name.');
}
}
} else {
return array('success' => false, 'message' => 'Not found user.');
}
// $query = "SELECT * FROM `$wpdb->usermeta` WHERE `$wpdb->usermeta`.`meta_key` = 'nickname' AND `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_value` = '$old_email';";
// $resut = $wpdb->get_results($query);
// if (count($resut) > 0) {
// $query = "UPDATE `$wpdb->usermeta` SET `meta_value` = '$email' WHERE `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_key` = 'nickname';";
// if (!$wpdb->query($query)) {
// return array('success' => false, 'message' => 'Cannot update nickname.');
// }
// }
// $query = "SELECT * FROM `$wpdb->usermeta` WHERE `$wpdb->usermeta`.`meta_key` = 'first_name' AND `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_value` = '$old_email';";
// $resut = $wpdb->get_results($query);
// if (count($resut) > 0) {
// $query = "UPDATE `$wpdb->usermeta` SET `meta_value` = '$email' WHERE `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_key` = 'first_name';";
// if (!$wpdb->query($query)) {
// return array('success' => false, 'message' => 'Cannot update first_name.');
// }
// }
$query = "SELECT * FROM `$wpdb->usermeta` WHERE `$wpdb->usermeta`.`meta_key` = 'billing_email' AND `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_value` = '$old_email';";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
$query = "UPDATE `$wpdb->usermeta` SET `meta_value` = '$email' WHERE `$wpdb->usermeta`.`user_id` = $customer_id AND `$wpdb->usermeta`.`meta_key` = 'billing_email';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update billing_email.');
}
}
$query = "SELECT * FROM `$wpdb->postmeta` WHERE `$wpdb->postmeta`.`meta_key` = '_billing_email' AND `$wpdb->postmeta`.`meta_value` = '$old_email';";
$resut = $wpdb->get_results($query);
if (count($resut) > 0) {
foreach ($resut as $postmeta) {
if ($postmeta->meta_value == $old_email) {
$query = "UPDATE `$wpdb->postmeta` SET `meta_value` = '$email' WHERE `$wpdb->postmeta`.`meta_id` = $postmeta->meta_id AND `$wpdb->postmeta`.`meta_key` = '_billing_email';";
if (!$wpdb->query($query)) {
return array('success' => false, 'message' => 'Cannot update billing_email.');
}
}
}
}
return array('success' => true, 'message' => '');
}
//Action will run after save address successfull
//woocommerce_customer_save_address
//wc_notice_count( 'error' ) == 0
/**
* Save and and update a billing or shipping address if the.
* form was submitted through the user account page.
*/
function fgc_custom_save_address($load_address) {
global $wp;
$user_id = get_current_user_id();
if ($user_id <= 0) {
return;
}
WC()->init();
$address = WC()->countries->get_address_fields(esc_attr($_POST[$load_address . '_country']), $load_address . '_');
$error = '';
$iserror = '';
foreach ($address as $key => $field) {
if (!isset($field['type'])) {
$field['type'] = 'text';
}
// Get Value
switch ($field['type']) {
case "checkbox" :
$_POST[$key] = isset($_POST[$key]) ? 1 : 0;
break;
default :
$_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : '';
break;
}
// Hook to allow modification of value
$_POST[$key] = apply_filters('woocommerce_process_myaccount_field_' . $key, $_POST[$key]);
// Validation: Required fields
if (!empty($field['required']) && empty($_POST[$key])) {
$iserror = 'true';
$error .= $field['label'] . ' ' . __('is a required field. ', 'woocommerce');
}
if (!empty($_POST[$key])) {
// Validation rules
if (!empty($field['validate']) && is_array($field['validate'])) {
$iserror = 'true';
foreach ($field['validate'] as $rule) {
switch ($rule) {
case 'postcode' :
$_POST[$key] = strtoupper(str_replace(' ', '', $_POST[$key]));
if (!WC_Validation::is_postcode($_POST[$key], $_POST[$load_address . '_country'])) {
$error .= __('Please enter a valid postcode/ZIP. ', 'woocommerce');
} else {
$_POST[$key] = wc_format_postcode($_POST[$key], $_POST[$load_address . '_country']);
}
break;
case 'phone' :
$_POST[$key] = wc_format_phone_number($_POST[$key]);
if (!WC_Validation::is_phone($_POST[$key])) {
$error .= '<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number. ', 'woocommerce');
}
break;
case 'email' :
$_POST[$key] = strtolower($_POST[$key]);
if (!is_email($_POST[$key])) {
$error .= '<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address. ', 'woocommerce');
}
break;
}
}
}
}
}
if ($error == '') {
$all_subscriptions = wcs_get_users_subscriptions($user_id);
foreach ($all_subscriptions as $subscription_id => $_subscription) {
// $post_parent_id = get_post_field('post_parent', $subscription_id);
if ($_subscription->post_status == 'wc-on-hold' || $_subscription->post_status == 'wc-active') {
foreach ($address as $key => $field) {
// update_post_meta($post_parent_id, '_' . $key, $_POST[$key]);
update_post_meta($subscription_id, '_' . $key, $_POST[$key]);
}
}
}
foreach ($address as $key => $field) {
update_user_meta($user_id, $key, $_POST[$key]);
}
$response = array();
if (isset($_GET['fgcSaveAddress']) && isset($_GET['type'])) {
if ($_GET['type'] == 'shipping') {
$response = fgc_get_shipping_rate();
}
}
do_action('woocommerce_customer_save_address', $user_id, $load_address);
ob_start();
include_once 'data_address.php';
$content = ob_get_clean();
$response['iserror'] = 'false';
$response['datahtml'] = $content;
$response['mess'] = 'Address changed successfully.';
} else {
$response['iserror'] = $iserror;
$response['iserror'] = 'Unsuccessfully';
$response['mess'] = $error;
}
echo json_encode($response);
exit();
}
function fgc_cardType($number) {
$number = preg_replace('/[^\d]/', '', $number);
if (preg_match('/^(4026|417500|4405|4508|4844|4913|4917)\d+$/', $number)) {
return 'Electron';
} elseif (preg_match('/^(5018|5020|5038|5612|5893|6304|6759|6761|6762|6763|0604|6390)\d+$/', $number)) {
return 'Maestro';
} elseif (preg_match('/^(5019)\d+$/', $number)) {
return 'Dankort';
} elseif (preg_match('/^(636)\d+$/', $number)) {
return 'Interpayment';
} elseif (preg_match('/^(62|88)\d+$/', $number)) {
return 'Unionpay';
} elseif (preg_match('/^3[47][0-9]{13}$/', $number)) {
return 'American Express';
} elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $number)) {
return 'Diners Club';
} elseif (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/', $number)) {
return 'Discover';
} elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/', $number)) {
return 'JCB';
} elseif (preg_match('/^5[1-5][0-9]{14}$/', $number)) {
return 'MasterCard';
} elseif (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $number)) {
return 'Visa';
} else {
return 'Other';
}
}
/**
* Update card info of Stripe customer
*/
function fgcUpdateCardInfo($data = array()) {
require('init.php');
$stripe = new WC_Gateway_Stripe();
\Stripe\Stripe::setApiKey($stripe->secret_key);
$stripe_customer_id = get_user_meta(get_current_user_id(), '_stripe_customer_id', true);
$customer = \Stripe\Customer::retrieve($stripe_customer_id);
if ($data['card_num'] != '' && $data['exp_month'] != '' && $data['exp_year'] != '' && $data['cvc'] != '' && $data['card_id'] != '') {
$customer->card = array(
"number" => $data['card_num'],
"exp_month" => $data['exp_month'],
"exp_year" => $data['exp_year'],
"cvc" => $data['cvc']
);
// $deleted = $customer->sources->retrieve($data['card_id'])->delete();
// if ($deleted->deleted == 'true') {
// $result = $customer->save();
// }
try {
$customer->save();
fgc_update_card_id($stripe_customer_id);
//update exp date to ActiveCampaign
require_once(WP_CONTENT_DIR . "/plugins/pl-sync-ac/activecampaign/includes/ActiveCampaign.class.php");
$setting_ac = get_option("settings_activecampaign");
$ac = new ActiveCampaign($setting_ac["api_url"], $setting_ac["api_key"]);
if ($data['exp_month'] < 10) {
$exp_month = '0' . $data['exp_month'];
} else {
$exp_month = $data['exp_month'];
}
$contact["email"] = $data['accout_emial'];
$contact["field[%CREDIT_CARD_EXPIRY_DATE%,0]"] = $data['exp_year'] . '-' . $exp_month . '-01';
$res = $ac->api("contact/sync", $contact);
//END update exp date to ActiveCampaign
return 'true';
} catch (Exception $ex) {
return $ex->getMessage();
}
} else {
return 'false';
}
}
add_action('woocommerce_payment_complete', 'fgc_update_shipping_cost_after_upgrade', 10);
function fgc_update_shipping_cost_after_upgrade($order_id) {
fgc_get_shipping_rate();
}
add_filter('woocommerce_billing_fields', 'fgc_set_required_phone_billing', 10, 1);
function fgc_set_required_phone_billing($address_fields) {
if ($_POST['shipping_country'] == 'AU') {
$address_fields['billing_phone']['required'] = false;
} else {
$address_fields['billing_phone']['required'] = true;
}
return $address_fields;
}
function fgc_update_card_id($customer_id) {
if (!class_exists('WC_Gateway_Stripe')) {
require_once(WP_CONTENT_DIR . "/plugins/woocommerce-gateway-stripe/includes/legacy/class-wc-gateway-stripe.php");
}
$customer = WC_Gateway_Stripe::fgc_get_saved_cards($customer_id);
$stripe_card_id = $customer->id;
$orders = get_order_ids_from_customer_id($customer_id);
foreach ($orders as $order) {
$current_stripe_card_id = get_post_meta($order->post_id, '_stripe_card_id', true);
if ($current_stripe_card_id != NULL && $current_stripe_card_id != '' && $current_stripe_card_id != $stripe_card_id) {
update_post_meta($order->post_id, '_stripe_card_id', $stripe_card_id);
}
}
}
function get_order_ids_from_customer_id($customer_id) {
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_stripe_customer_id' AND `meta_value` = '$customer_id'";
$result = $wpdb->get_results($query);
if (!empty($result)) {
return $result;
}
return false;
}