1: <?php
2: namespace Opencart\Catalog\Controller\Api\Sale;
3: /**
4: * Class Shipping Method
5: *
6: * @package Opencart\Catalog\Controller\Api\Sale
7: */
8: class ShippingMethod extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('api/sale/shipping_method');
14:
15: $json = [];
16:
17: if ($this->cart->hasShipping()) {
18: if (!isset($this->session->data['shipping_address'])) {
19: $json['error'] = $this->language->get('error_shipping_address');
20: }
21: } else {
22: $json['error'] = $this->language->get('error_shipping');
23: }
24:
25: if (!$json) {
26: $this->load->model('checkout/shipping_method');
27:
28: $shipping_methods = $this->model_checkout_shipping_method->getMethods($this->session->data['shipping_address']);
29:
30: if ($shipping_methods) {
31: $json['shipping_methods'] = $this->session->data['shipping_methods'] = $shipping_methods;
32: } else {
33: $json['error'] = $this->language->get('error_no_shipping');
34: }
35: }
36:
37: $this->response->addHeader('Content-Type: application/json');
38: $this->response->setOutput(json_encode($json));
39: }
40:
41: /**
42: * Save
43: *
44: * @return void
45: */
46: public function save(): void {
47: $this->load->language('api/sale/shipping_method');
48:
49: $json = [];
50:
51: if ($this->cart->hasShipping()) {
52: if (!isset($this->session->data['shipping_address'])) {
53: $json['error'] = $this->language->get('error_shipping_address');
54: }
55:
56: if (isset($this->request->post['shipping_method'])) {
57: $shipping = explode('.', $this->request->post['shipping_method']);
58:
59: if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
60: $json['error'] = $this->language->get('error_shipping_method');
61: }
62: } else {
63: $json['error'] = $this->language->get('error_shipping_method');
64: }
65: } else {
66: $json['error'] = $this->language->get('error_shipping');
67: }
68:
69: if (!$json) {
70: $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
71:
72: $json['success'] = $this->language->get('text_success');
73: }
74:
75: $this->response->addHeader('Content-Type: application/json');
76: $this->response->setOutput(json_encode($json));
77: }
78: }
79: