1: <?php
2: namespace Opencart\Catalog\Controller\Checkout;
3: /**
4: * Class ShippingMethod
5: *
6: * @package Opencart\Catalog\Controller\Checkout
7: */
8: class ShippingMethod extends \Opencart\System\Engine\Controller {
9: /**
10: * @return string
11: */
12: public function index(): string {
13: $this->load->language('checkout/shipping_method');
14:
15: if (isset($this->session->data['shipping_method'])) {
16: $data['shipping_method'] = $this->session->data['shipping_method']['name'];
17: $data['code'] = $this->session->data['shipping_method']['code'];
18: } else {
19: $data['shipping_method'] = '';
20: $data['code'] = '';
21: }
22:
23: $data['language'] = $this->config->get('config_language');
24:
25: return $this->load->view('checkout/shipping_method', $data);
26: }
27:
28: /**
29: * Quote
30: *
31: * @return void
32: */
33: public function quote(): void {
34: $this->load->language('checkout/shipping_method');
35:
36: $json = [];
37:
38: // Validate cart has products and has stock.
39: if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
40: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
41: }
42:
43: // Validate minimum quantity requirements.
44: $products = $this->cart->getProducts();
45:
46: foreach ($products as $product) {
47: if (!$product['minimum']) {
48: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
49:
50: break;
51: }
52: }
53:
54: if (!$json) {
55: // Validate if customer data is set
56: if (!isset($this->session->data['customer'])) {
57: $json['error'] = $this->language->get('error_customer');
58: }
59:
60: // Validate if payment address is set if required in settings
61: if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
62: $json['error'] = $this->language->get('error_payment_address');
63: }
64:
65: // Validate if shipping not required. If not the customer should not have reached this page.
66: if ($this->cart->hasShipping() && !isset($this->session->data['shipping_address']['address_id'])) {
67: $json['error'] = $this->language->get('error_shipping_address');
68: }
69: }
70:
71: if (!$json) {
72: // Shipping methods
73: $this->load->model('checkout/shipping_method');
74:
75: $shipping_methods = $this->model_checkout_shipping_method->getMethods($this->session->data['shipping_address']);
76:
77: if ($shipping_methods) {
78: $json['shipping_methods'] = $this->session->data['shipping_methods'] = $shipping_methods;
79: } else {
80: $json['error'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact', 'language=' . $this->config->get('config_language')));
81: }
82: }
83:
84: $this->response->addHeader('Content-Type: application/json');
85: $this->response->setOutput(json_encode($json));
86: }
87:
88: /**
89: * Save
90: *
91: * @return void
92: */
93: public function save(): void {
94: $this->load->language('checkout/shipping_method');
95:
96: $json = [];
97:
98: // Validate cart has products and has stock.
99: if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
100: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
101: }
102:
103: // Validate minimum quantity requirements.
104: $products = $this->cart->getProducts();
105:
106: foreach ($products as $product) {
107: if (!$product['minimum']) {
108: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
109:
110: break;
111: }
112: }
113:
114: if (!$json) {
115: // Validate if customer is logged in or customer session data is not set
116: if (!isset($this->session->data['customer'])) {
117: $json['error'] = $this->language->get('error_customer');
118: }
119:
120: // Validate if payment address is set if required in settings
121: if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
122: $json['error'] = $this->language->get('error_payment_address');
123: }
124:
125: // Validate if shipping not required. If not the customer should not have reached this page.
126: if ($this->cart->hasShipping() && !isset($this->session->data['shipping_address']['address_id'])) {
127: $json['error'] = $this->language->get('error_shipping_address');
128: }
129:
130: if (isset($this->request->post['shipping_method'])) {
131: $shipping = explode('.', $this->request->post['shipping_method']);
132:
133: if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
134: $json['error'] = $this->language->get('error_shipping_method');
135: }
136: } else {
137: $json['error'] = $this->language->get('error_shipping_method');
138: }
139: }
140:
141: if (!$json) {
142: $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
143:
144: $json['success'] = $this->language->get('text_success');
145:
146: // Clear payment methods
147: unset($this->session->data['payment_method']);
148: unset($this->session->data['payment_methods']);
149: }
150:
151: $this->response->addHeader('Content-Type: application/json');
152: $this->response->setOutput(json_encode($json));
153: }
154: }
155: