/
ContactInformationTrait.php
107 lines (83 loc) · 3.17 KB
/
ContactInformationTrait.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
<?php
namespace MicroweberPackages\Checkout\Http\Controllers\Traits;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
trait ContactInformationTrait {
public function contactInformation() {
$data = [];
$data['errors'] = session_get('errors');
$data['checkout_session'] = session_get('checkout_v2');
if (is_logged()) {
$user = get_user();
if (empty($data['checkout_session']['first_name'])) {
$data['checkout_session']['first_name'] = $user['first_name'];
}
if (empty($data['checkout_session']['last_name'])) {
$data['checkout_session']['last_name'] = $user['last_name'];
}
if (empty($data['checkout_session']['email'])) {
$data['checkout_session']['email'] = $user['email'];
}
if (empty($data['checkout_session']['phone'])) {
$data['checkout_session']['phone'] = $user['phone'];
}
}
session_del('errors');
return $this->_renderView('checkout::contact_information',$data);
}
public function contactInformationSave(Request $request) {
session_append_array('checkout_v2', [
'first_name'=> $request->get('first_name'),
'last_name'=> $request->get('last_name'),
'email'=> $request->get('email'),
'phone'=> $request->get('phone')
]);
$validate = $this->_validateContactInformation($request->all());
if ($validate['valid'] == false) {
session_set('errors', $validate['errors']);
return redirect(route('checkout.contact_information'));
}
// Success
return redirect(route('checkout.shipping_method'));
}
private function _validateContactInformation($inputData = [])
{
$rules = [];
$rules['first_name'] = 'max:500';
$rules['last_name'] = 'max:500';
$rules['email'] = 'email|max:500';
$rules['phone'] = 'max:500';
if (get_option('shop_require_first_name', 'website') == 1) {
$rules['first_name'] .= '|required';
}
if (get_option('shop_require_last_name', 'website') == 1) {
$rules['last_name'] .= '|required';
}
if (get_option('shop_require_email', 'website') == 1) {
$rules['email'] .= '|required';;
}
if (get_option('shop_require_phone', 'website') == 1) {
$rules['phone'] .= '|required';;
}
if (empty($rules)) {
return ['valid'=>true];
}
if (empty($inputData)) {
$inputData = session_get('checkout_v2');
}
if (empty($inputData)) {
return [
'valid'=>false,
'errors'=>[
'form_errors'=>['error'=>_e('Please, fill the contact information data.', true)]
]
];
}
$validator = Validator::make($inputData, $rules);
if ($validator->fails()) {
$errors = $validator->messages()->toArray();
return ['valid'=>false,'errors'=>$errors];
}
return ['valid'=>true];
}
}