| 1: | <?php
|
| 2: | namespace Opencart\Catalog\Controller\Checkout;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class ShippingAddress extends \Opencart\System\Engine\Controller {
|
| 9: | |
| 10: | |
| 11: |
|
| 12: | public function index(): string {
|
| 13: | $this->load->language('checkout/shipping_address');
|
| 14: |
|
| 15: | $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
|
| 16: | $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
|
| 17: | $data['payment_address_required'] = $this->config->get('config_checkout_payment_address');
|
| 18: |
|
| 19: | $this->session->data['upload_token'] = oc_token(32);
|
| 20: |
|
| 21: | $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
|
| 22: |
|
| 23: | $this->load->model('account/address');
|
| 24: |
|
| 25: | $data['addresses'] = $this->model_account_address->getAddresses($this->customer->getId());
|
| 26: |
|
| 27: | if (isset($this->session->data['shipping_address']['address_id'])) {
|
| 28: | $data['address_id'] = $this->session->data['shipping_address']['address_id'];
|
| 29: | } else {
|
| 30: | $data['address_id'] = 0;
|
| 31: | }
|
| 32: |
|
| 33: | $this->load->model('localisation/country');
|
| 34: |
|
| 35: | $data['countries'] = $this->model_localisation_country->getCountries();
|
| 36: |
|
| 37: | if (isset($this->session->data['shipping_address'])) {
|
| 38: | $data['postcode'] = $this->session->data['shipping_address']['postcode'];
|
| 39: | $data['country_id'] = $this->session->data['shipping_address']['country_id'];
|
| 40: | $data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
|
| 41: | } else {
|
| 42: | $data['postcode'] = '';
|
| 43: | $data['country_id'] = (int)$this->config->get('config_country_id');
|
| 44: | $data['zone_id'] = '';
|
| 45: | }
|
| 46: |
|
| 47: |
|
| 48: | $data['custom_fields'] = [];
|
| 49: |
|
| 50: | $this->load->model('account/custom_field');
|
| 51: |
|
| 52: | $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
|
| 53: |
|
| 54: | foreach ($custom_fields as $custom_field) {
|
| 55: | if ($custom_field['location'] == 'address') {
|
| 56: | $data['custom_fields'][] = $custom_field;
|
| 57: | }
|
| 58: | }
|
| 59: |
|
| 60: | $data['language'] = $this->config->get('config_language');
|
| 61: |
|
| 62: | return $this->load->view('checkout/shipping_address', $data);
|
| 63: | }
|
| 64: |
|
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: |
|
| 70: | public function save(): void {
|
| 71: | $this->load->language('checkout/shipping_address');
|
| 72: |
|
| 73: | $json = [];
|
| 74: |
|
| 75: |
|
| 76: | if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
|
| 77: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 78: | }
|
| 79: |
|
| 80: |
|
| 81: | $products = $this->cart->getProducts();
|
| 82: |
|
| 83: | foreach ($products as $product) {
|
| 84: | if (!$product['minimum']) {
|
| 85: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 86: |
|
| 87: | break;
|
| 88: | }
|
| 89: | }
|
| 90: |
|
| 91: |
|
| 92: | if (!$this->customer->isLogged() || !isset($this->session->data['customer'])) {
|
| 93: | $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
|
| 94: | }
|
| 95: |
|
| 96: |
|
| 97: | if (!$this->cart->hasShipping()) {
|
| 98: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 99: | }
|
| 100: |
|
| 101: | if (!$json) {
|
| 102: | $keys = [
|
| 103: | 'firstname',
|
| 104: | 'lastname',
|
| 105: | 'company',
|
| 106: | 'address_1',
|
| 107: | 'address_2',
|
| 108: | 'city',
|
| 109: | 'postcode',
|
| 110: | 'country_id',
|
| 111: | 'zone_id',
|
| 112: | 'custom_field'
|
| 113: | ];
|
| 114: |
|
| 115: | foreach ($keys as $key) {
|
| 116: | if (!isset($this->request->post[$key])) {
|
| 117: | $this->request->post[$key] = '';
|
| 118: | }
|
| 119: | }
|
| 120: |
|
| 121: | if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
|
| 122: | $json['error']['firstname'] = $this->language->get('error_firstname');
|
| 123: | }
|
| 124: |
|
| 125: | if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
|
| 126: | $json['error']['lastname'] = $this->language->get('error_lastname');
|
| 127: | }
|
| 128: |
|
| 129: | if ((oc_strlen($this->request->post['address_1']) < 3) || (oc_strlen($this->request->post['address_1']) > 128)) {
|
| 130: | $json['error']['address_1'] = $this->language->get('error_address_1');
|
| 131: | }
|
| 132: |
|
| 133: | if ((oc_strlen($this->request->post['city']) < 2) || (oc_strlen($this->request->post['city']) > 128)) {
|
| 134: | $json['error']['city'] = $this->language->get('error_city');
|
| 135: | }
|
| 136: |
|
| 137: | $this->load->model('localisation/country');
|
| 138: |
|
| 139: | $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
|
| 140: |
|
| 141: | if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
|
| 142: | $json['error']['postcode'] = $this->language->get('error_postcode');
|
| 143: | }
|
| 144: |
|
| 145: | if (!$country_info || $this->request->post['country_id'] == '') {
|
| 146: | $json['error']['country'] = $this->language->get('error_country');
|
| 147: | }
|
| 148: |
|
| 149: | if ($this->request->post['zone_id'] == '') {
|
| 150: | $json['error']['zone'] = $this->language->get('error_zone');
|
| 151: | }
|
| 152: |
|
| 153: |
|
| 154: | $this->load->model('account/custom_field');
|
| 155: |
|
| 156: | $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
|
| 157: |
|
| 158: | foreach ($custom_fields as $custom_field) {
|
| 159: | if ($custom_field['location'] == 'address') {
|
| 160: | if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
|
| 161: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
|
| 162: | } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
|
| 163: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
|
| 164: | }
|
| 165: | }
|
| 166: | }
|
| 167: | }
|
| 168: |
|
| 169: | if (!$json) {
|
| 170: |
|
| 171: | $address_id = $this->customer->getAddressId();
|
| 172: |
|
| 173: | if (!$address_id) {
|
| 174: | $this->request->post['default'] = 1;
|
| 175: | }
|
| 176: |
|
| 177: | $this->load->model('account/address');
|
| 178: |
|
| 179: | $json['address_id'] = $this->model_account_address->addAddress($this->customer->getId(), $this->request->post);
|
| 180: |
|
| 181: | $json['addresses'] = $this->model_account_address->getAddresses($this->customer->getId());
|
| 182: |
|
| 183: | $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getId(), $json['address_id']);
|
| 184: |
|
| 185: | $json['success'] = $this->language->get('text_success');
|
| 186: |
|
| 187: |
|
| 188: | unset($this->session->data['shipping_method']);
|
| 189: | unset($this->session->data['shipping_methods']);
|
| 190: | unset($this->session->data['payment_method']);
|
| 191: | unset($this->session->data['payment_methods']);
|
| 192: | }
|
| 193: |
|
| 194: | $this->response->addHeader('Content-Type: application/json');
|
| 195: | $this->response->setOutput(json_encode($json));
|
| 196: | }
|
| 197: |
|
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: |
|
| 203: | public function address(): void {
|
| 204: | $this->load->language('checkout/shipping_address');
|
| 205: |
|
| 206: | $json = [];
|
| 207: |
|
| 208: | if (isset($this->request->get['address_id'])) {
|
| 209: | $address_id = (int)$this->request->get['address_id'];
|
| 210: | } else {
|
| 211: | $address_id = 0;
|
| 212: | }
|
| 213: |
|
| 214: |
|
| 215: | if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
|
| 216: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 217: | }
|
| 218: |
|
| 219: |
|
| 220: | $products = $this->cart->getProducts();
|
| 221: |
|
| 222: | foreach ($products as $product) {
|
| 223: | if (!$product['minimum']) {
|
| 224: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 225: |
|
| 226: | break;
|
| 227: | }
|
| 228: | }
|
| 229: |
|
| 230: |
|
| 231: | if (!$this->customer->isLogged() || !isset($this->session->data['customer'])) {
|
| 232: | $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
|
| 233: | }
|
| 234: |
|
| 235: |
|
| 236: | if (!$this->cart->hasShipping()) {
|
| 237: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
|
| 238: | }
|
| 239: |
|
| 240: | if (!$json) {
|
| 241: | $this->load->model('account/address');
|
| 242: |
|
| 243: | $address_info = $this->model_account_address->getAddress($this->customer->getId(), $address_id);
|
| 244: |
|
| 245: | if (!$address_info) {
|
| 246: | $json['error'] = $this->language->get('error_address');
|
| 247: |
|
| 248: | unset($this->session->data['shipping_address']);
|
| 249: | unset($this->session->data['shipping_method']);
|
| 250: | unset($this->session->data['shipping_methods']);
|
| 251: | unset($this->session->data['payment_method']);
|
| 252: | unset($this->session->data['payment_methods']);
|
| 253: | }
|
| 254: | }
|
| 255: |
|
| 256: | if (!$json) {
|
| 257: | $this->session->data['shipping_address'] = $address_info;
|
| 258: |
|
| 259: | $json['success'] = $this->language->get('text_success');
|
| 260: |
|
| 261: |
|
| 262: | unset($this->session->data['shipping_method']);
|
| 263: | unset($this->session->data['shipping_methods']);
|
| 264: | unset($this->session->data['payment_method']);
|
| 265: | unset($this->session->data['payment_methods']);
|
| 266: | }
|
| 267: |
|
| 268: | $this->response->addHeader('Content-Type: application/json');
|
| 269: | $this->response->setOutput(json_encode($json));
|
| 270: | }
|
| 271: | }
|
| 272: | |