-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveAddress.php
122 lines (98 loc) · 3.92 KB
/
saveAddress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?
use \website\core\SQL;
// Get order
try {
$order = $checkout->order();
} catch(\Exception $e) {
$messages->error('No active order.');
$app->redirect('/basket');
}
// Load order lines to calculate totals
$orders->lines()->on($order);
if ($request->method() == 'POST') {
$formData = $request->toArray();
// var_dump($formData);
// die();
if(
isset($formData['address_line_1'])
&& isset($formData['address_line_2'])
&& isset($formData['address_line_city'])
&& isset($formData['address_line_postcode'])
&& !empty($formData['address_line_1'])
&& !empty($formData['address_line_2'])
&& !empty($formData['address_line_city'])
&& !empty($formData['address_line_postcode'])
&& !empty($formData['userEmail'])
) {
$postcode = strtoupper($formData['address_line_postcode']);
// if(preg_match('/^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$/', $postcode)) {
$message = 'correct postcode format';
if(isset($formData['deliveryAddressSame']) && $formData['deliveryAddressSame'] == 1){
$sameLine1 = $formData['shipping_line_1'] = $formData['address_line_1'];
$sameLine2 = $formData['shipping_line_2'] = $formData['address_line_2'];
$sameLineCity = $formData['shipping_line_city'] = $formData['address_line_city'];
$sameLinePostcode = $formData['shipping_line_postcode'] = strtoupper($formData['address_line_postcode']);
// Create billing address
$billing_address_values = [
'address_line_1' => $formData['address_line_1'],
'address_line_2' => $formData['address_line_2'],
'town' => $formData['address_line_city'],
'postcode' => strtoupper($formData['address_line_postcode']),
];
$billing_address = $addresses->post(null, null, $billing_address_values);
// Because billing and shipping address are the same,
// We an just copy the values across
$shipping_address_values = $billing_address_values;
$shipping_address = $addresses->post(null, null, $shipping_address_values);
// We update the address ids in the order
// With the addresses just created
$order_values = [
'shipping_address_id' => $shipping_address->id,
'billing_address_id' => $billing_address->id,
'email' => $formData['userEmail'],
'name' => $formData['userName'],
'order_status_id' => 4,
];
$orders->post(null, $order, $order_values);
// redirect to checkout payment
return $app->redirect('/checkout/payment');
}
// Create billing address
$billing_address_values = [
'address_line_1' => $formData['address_line_1'],
'address_line_2' => $formData['address_line_2'],
'town' => $formData['address_line_city'],
'postcode' => strtoupper($formData['address_line_postcode']),
];
$billing_address = $addresses->post(null, null, $billing_address_values);
// Create shipping address
$shipping_address_values = [
'address_line_1' => $formData['shipping_line_1'],
'address_line_2' => $formData['shipping_line_2'],
'town' => $formData['shipping_line_city'],
'postcode' => strtoupper($formData['shipping_line_postcode']),
];
$shipping_address = $addresses->post(null, null, $shipping_address_values);
// We update the address ids in the order
// With the addresses just created
$order_values = [
'shipping_address_id' => $shipping_address->id,
'billing_address_id' => $billing_address->id,
'email' => $formData['userEmail'],
'name' => $formData['userName'],
'order_status_id' => 4,
];
$orders->post(null, $order, $order_values);
// redirect to checkout payment
return $app->redirect('/checkout/payment');
// }
$message = '<h2 class="alert alert-danger text-center"> There was an error with the address data submitted.</h2>';
return $template('/checkout', array(
'message' => $message,
));
}
$message = '<h2 class="alert alert-danger text-center"> There was an error with the address data submitted!</h2>';
return $template('/checkout', array(
'message' => $message,
));
}