1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Sunrise CMS - Open source CMS for widespread use. |
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
5
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
7
|
|
|
|
8
|
|
|
This file is part of Sunrise CMS. |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
22
|
|
|
|
23
|
|
|
class ControllerCheckoutShippingMethod extends \Sunrise\Engine\Core\Controller |
24
|
|
|
{ |
25
|
|
|
public function index() |
26
|
|
|
{ |
27
|
|
|
$this->load->language('checkout/checkout'); |
|
|
|
|
28
|
|
|
|
29
|
|
|
if (isset($this->session->data['shipping_address'])) { |
|
|
|
|
30
|
|
|
// Shipping Methods |
31
|
|
|
$method_data = array(); |
32
|
|
|
|
33
|
|
|
$this->load->model('extension/extension'); |
34
|
|
|
|
35
|
|
|
$results = $this->model_extension_extension->getExtensions('shipping'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
foreach ($results as $result) { |
38
|
|
|
if ($this->config->get($result['code'] . '_status')) { |
|
|
|
|
39
|
|
|
$this->load->model('extension/shipping/' . $result['code']); |
40
|
|
|
|
41
|
|
|
$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']); |
42
|
|
|
|
43
|
|
|
if ($quote) { |
44
|
|
|
$method_data[$result['code']] = array( |
45
|
|
|
'title' => $quote['title'], |
46
|
|
|
'quote' => $quote['quote'], |
47
|
|
|
'sort_order' => $quote['sort_order'], |
48
|
|
|
'error' => $quote['error'] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$sort_order = array(); |
55
|
|
|
|
56
|
|
|
foreach ($method_data as $key => $value) { |
57
|
|
|
$sort_order[$key] = $value['sort_order']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
array_multisort($sort_order, SORT_ASC, $method_data); |
61
|
|
|
|
62
|
|
|
$this->session->data['shipping_methods'] = $method_data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$data['text_shipping_method'] = $this->language->get('text_shipping_method'); |
|
|
|
|
66
|
|
|
$data['text_comments'] = $this->language->get('text_comments'); |
67
|
|
|
$data['text_loading'] = $this->language->get('text_loading'); |
68
|
|
|
|
69
|
|
|
$data['button_continue'] = $this->language->get('button_continue'); |
70
|
|
|
|
71
|
|
|
if (empty($this->session->data['shipping_methods'])) { |
72
|
|
|
$data['error_warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact')); |
|
|
|
|
73
|
|
|
} else { |
74
|
|
|
$data['error_warning'] = ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($this->session->data['shipping_methods'])) { |
78
|
|
|
$data['shipping_methods'] = $this->session->data['shipping_methods']; |
79
|
|
|
} else { |
80
|
|
|
$data['shipping_methods'] = array(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isset($this->session->data['shipping_method']['code'])) { |
84
|
|
|
$data['code'] = $this->session->data['shipping_method']['code']; |
85
|
|
|
} else { |
86
|
|
|
$data['code'] = ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (isset($this->session->data['comment'])) { |
90
|
|
|
$data['comment'] = $this->session->data['comment']; |
91
|
|
|
} else { |
92
|
|
|
$data['comment'] = ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->response->setOutput($this->load->view('checkout/shipping_method', $data)); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function save() |
99
|
|
|
{ |
100
|
|
|
$this->load->language('checkout/checkout'); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$json = array(); |
103
|
|
|
|
104
|
|
|
// Validate if shipping is required. If not the customer should not have reached this page. |
105
|
|
|
if (!$this->cart->hasShipping()) { |
|
|
|
|
106
|
|
|
$json['redirect'] = $this->url->link('checkout/checkout', '', true); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Validate if shipping address has been set. |
110
|
|
|
if (!isset($this->session->data['shipping_address'])) { |
|
|
|
|
111
|
|
|
$json['redirect'] = $this->url->link('checkout/checkout', '', true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Validate cart has products and has stock. |
115
|
|
|
if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { |
|
|
|
|
116
|
|
|
$json['redirect'] = $this->url->link('checkout/cart'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Validate minimum quantity requirements. |
120
|
|
|
$products = $this->cart->getProducts(); |
121
|
|
|
|
122
|
|
|
foreach ($products as $product) { |
123
|
|
|
$product_total = 0; |
124
|
|
|
|
125
|
|
|
foreach ($products as $product_2) { |
126
|
|
|
if ($product_2['product_id'] == $product['product_id']) { |
127
|
|
|
$product_total += $product_2['quantity']; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($product['minimum'] > $product_total) { |
132
|
|
|
$json['redirect'] = $this->url->link('checkout/cart'); |
133
|
|
|
|
134
|
|
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!isset($this->request->post['shipping_method'])) { |
|
|
|
|
139
|
|
|
$json['error']['warning'] = $this->language->get('error_shipping'); |
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
$shipping = explode('.', $this->request->post['shipping_method']); |
142
|
|
|
|
143
|
|
|
if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { |
144
|
|
|
$json['error']['warning'] = $this->language->get('error_shipping'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!$json) { |
149
|
|
|
$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; |
150
|
|
|
|
151
|
|
|
$this->session->data['comment'] = strip_tags($this->request->post['comment']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->response->addHeader('Content-Type: application/json'); |
|
|
|
|
155
|
|
|
$this->response->setOutput(json_encode($json)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|