| 1: | <?php
|
| 2: | namespace Opencart\Catalog\Controller\Extension\Opencart\Total;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Shipping extends \Opencart\System\Engine\Controller {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: |
|
| 14: | public function index(): string {
|
| 15: | if ($this->config->get('total_shipping_status') && $this->config->get('total_shipping_estimator') && $this->cart->hasShipping()) {
|
| 16: | $this->load->language('extension/opencart/total/shipping');
|
| 17: |
|
| 18: | if (isset($this->session->data['shipping_address'])) {
|
| 19: | $data['postcode'] = $this->session->data['shipping_address']['postcode'];
|
| 20: | $data['country_id'] = $this->session->data['shipping_address']['country_id'];
|
| 21: | $data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
|
| 22: | } else {
|
| 23: | $data['postcode'] = '';
|
| 24: | $data['country_id'] = (int)$this->config->get('config_country_id');
|
| 25: | $data['zone_id'] = '';
|
| 26: | }
|
| 27: |
|
| 28: | if (isset($this->session->data['shipping_method'])) {
|
| 29: | $data['code'] = $this->session->data['shipping_method']['code'];
|
| 30: | } else {
|
| 31: | $data['code'] = '';
|
| 32: | }
|
| 33: |
|
| 34: | $this->load->model('localisation/country');
|
| 35: |
|
| 36: | $data['countries'] = $this->model_localisation_country->getCountries();
|
| 37: |
|
| 38: | $data['language'] = $this->config->get('config_language');
|
| 39: |
|
| 40: | return $this->load->view('extension/opencart/total/shipping', $data);
|
| 41: | }
|
| 42: |
|
| 43: | return '';
|
| 44: | }
|
| 45: |
|
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: |
|
| 51: | public function quote(): void {
|
| 52: | $this->load->language('extension/opencart/total/shipping');
|
| 53: |
|
| 54: | $json = [];
|
| 55: |
|
| 56: | $keys = [
|
| 57: | 'postcode',
|
| 58: | 'country_id',
|
| 59: | 'zone_id'
|
| 60: | ];
|
| 61: |
|
| 62: | foreach ($keys as $key) {
|
| 63: | if (!isset($this->request->post[$key])) {
|
| 64: | $this->request->post[$key] = '';
|
| 65: | }
|
| 66: | }
|
| 67: |
|
| 68: | if (!$this->cart->hasProducts()) {
|
| 69: | $json['error']['warning'] = $this->language->get('error_product');
|
| 70: | }
|
| 71: |
|
| 72: | if (!$this->cart->hasShipping()) {
|
| 73: | $json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact', 'language=' . $this->config->get('config_language')));
|
| 74: | }
|
| 75: |
|
| 76: | $this->load->model('localisation/country');
|
| 77: |
|
| 78: | $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
|
| 79: |
|
| 80: | if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
|
| 81: | $json['error']['postcode'] = $this->language->get('error_postcode');
|
| 82: | }
|
| 83: |
|
| 84: | if (!$country_info || $this->request->post['country_id'] == '') {
|
| 85: | $json['error']['country'] = $this->language->get('error_country');
|
| 86: | }
|
| 87: |
|
| 88: | if ($this->request->post['zone_id'] == '') {
|
| 89: | $json['error']['zone'] = $this->language->get('error_zone');
|
| 90: | }
|
| 91: |
|
| 92: | if (!$json) {
|
| 93: | if ($country_info) {
|
| 94: | $country = $country_info['name'];
|
| 95: | $iso_code_2 = $country_info['iso_code_2'];
|
| 96: | $iso_code_3 = $country_info['iso_code_3'];
|
| 97: | $address_format = $country_info['address_format'];
|
| 98: | } else {
|
| 99: | $country = '';
|
| 100: | $iso_code_2 = '';
|
| 101: | $iso_code_3 = '';
|
| 102: | $address_format = '';
|
| 103: | }
|
| 104: |
|
| 105: | $this->load->model('localisation/zone');
|
| 106: |
|
| 107: | $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
|
| 108: |
|
| 109: | if ($zone_info) {
|
| 110: | $zone = $zone_info['name'];
|
| 111: | $zone_code = $zone_info['code'];
|
| 112: | } else {
|
| 113: | $zone = '';
|
| 114: | $zone_code = '';
|
| 115: | }
|
| 116: |
|
| 117: | $this->session->data['shipping_address'] = [
|
| 118: | 'postcode' => $this->request->post['postcode'],
|
| 119: | 'zone_id' => $this->request->post['zone_id'],
|
| 120: | 'zone' => $zone,
|
| 121: | 'zone_code' => $zone_code,
|
| 122: | 'country_id' => $this->request->post['country_id'],
|
| 123: | 'country' => $country,
|
| 124: | 'iso_code_2' => $iso_code_2,
|
| 125: | 'iso_code_3' => $iso_code_3
|
| 126: | ];
|
| 127: |
|
| 128: | $this->tax->setShippingAddress($this->request->post['country_id'], $this->request->post['zone_id']);
|
| 129: |
|
| 130: |
|
| 131: | $this->load->model('checkout/shipping_method');
|
| 132: |
|
| 133: | $shipping_methods = $this->model_checkout_shipping_method->getMethods($this->session->data['shipping_address']);
|
| 134: |
|
| 135: | if ($shipping_methods) {
|
| 136: | $json['shipping_methods'] = $this->session->data['shipping_methods'] = $shipping_methods;
|
| 137: | } else {
|
| 138: | $json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact', 'language=' . $this->config->get('config_language')));
|
| 139: | }
|
| 140: | }
|
| 141: |
|
| 142: | $this->response->addHeader('Content-Type: application/json');
|
| 143: | $this->response->setOutput(json_encode($json));
|
| 144: | }
|
| 145: |
|
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: |
|
| 151: | public function save(): void {
|
| 152: | $this->load->language('extension/opencart/total/shipping');
|
| 153: |
|
| 154: | $json = [];
|
| 155: |
|
| 156: | if (!empty($this->request->post['shipping_method'])) {
|
| 157: | $shipping = explode('.', $this->request->post['shipping_method']);
|
| 158: |
|
| 159: | if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
|
| 160: | $json['error'] = $this->language->get('error_shipping');
|
| 161: | }
|
| 162: | } else {
|
| 163: | $json['error'] = $this->language->get('error_shipping');
|
| 164: | }
|
| 165: |
|
| 166: | if (!$json) {
|
| 167: | $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
|
| 168: |
|
| 169: | $json['success'] = $this->language->get('text_success');
|
| 170: |
|
| 171: | unset($this->session->data['payment_method']);
|
| 172: | unset($this->session->data['payment_methods']);
|
| 173: | }
|
| 174: |
|
| 175: | $this->response->addHeader('Content-Type: application/json');
|
| 176: | $this->response->setOutput(json_encode($json));
|
| 177: | }
|
| 178: | }
|
| 179: | |