1: | <?php
2: | namespace Opencart\Catalog\Controller\Api\Sale;
3: | /**
4: | * Class Shipping Address
5: | *
6: | * @package Opencart\Catalog\Controller\Api\Sale
7: | */
8: | class ShippingAddress extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('api/sale/shipping_address');
14: |
15: | $json = [];
16: |
17: | if ($this->cart->hasShipping()) {
18: | // Add keys for missing post vars
19: | $keys = [
20: | 'firstname',
21: | 'lastname',
22: | 'company',
23: | 'address_1',
24: | 'address_2',
25: | 'postcode',
26: | 'city',
27: | 'zone_id',
28: | 'country_id'
29: | ];
30: |
31: | foreach ($keys as $key) {
32: | if (!isset($this->request->post[$key])) {
33: | $this->request->post[$key] = '';
34: | }
35: | }
36: |
37: | if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
38: | $json['error']['firstname'] = $this->language->get('error_firstname');
39: | }
40: |
41: | if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
42: | $json['error']['lastname'] = $this->language->get('error_lastname');
43: | }
44: |
45: | if ((oc_strlen($this->request->post['address_1']) < 3) || (oc_strlen($this->request->post['address_1']) > 128)) {
46: | $json['error']['address_1'] = $this->language->get('error_address_1');
47: | }
48: |
49: | if ((oc_strlen($this->request->post['city']) < 2) || (oc_strlen($this->request->post['city']) > 128)) {
50: | $json['error']['city'] = $this->language->get('error_city');
51: | }
52: |
53: | $this->load->model('localisation/country');
54: |
55: | $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
56: |
57: | if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
58: | $json['error']['postcode'] = $this->language->get('error_postcode');
59: | }
60: |
61: | if (!$country_info || $this->request->post['country_id'] == '') {
62: | $json['error']['country'] = $this->language->get('error_country');
63: | }
64: |
65: | if ($this->request->post['zone_id'] == '') {
66: | $json['error']['zone'] = $this->language->get('error_zone');
67: | }
68: |
69: | // Custom field validation
70: | $this->load->model('account/custom_field');
71: |
72: | $custom_fields = $this->model_account_custom_field->getCustomFields((int)$this->config->get('config_customer_group_id'));
73: |
74: | foreach ($custom_fields as $custom_field) {
75: | if ($custom_field['location'] == 'address') {
76: | if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
77: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
78: | } 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']])) {
79: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
80: | }
81: | }
82: | }
83: | } else {
84: | $json['error']['warning'] = $this->language->get('error_shipping');
85: | }
86: |
87: | if (!$json) {
88: | if ($country_info) {
89: | $country = $country_info['name'];
90: | $iso_code_2 = $country_info['iso_code_2'];
91: | $iso_code_3 = $country_info['iso_code_3'];
92: | $address_format = $country_info['address_format'];
93: | } else {
94: | $country = '';
95: | $iso_code_2 = '';
96: | $iso_code_3 = '';
97: | $address_format = '';
98: | }
99: |
100: | $this->load->model('localisation/zone');
101: |
102: | $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
103: |
104: | if ($zone_info) {
105: | $zone = $zone_info['name'];
106: | $zone_code = $zone_info['code'];
107: | } else {
108: | $zone = '';
109: | $zone_code = '';
110: | }
111: |
112: | $this->session->data['shipping_address'] = [
113: | 'address_id' => $this->request->post['shipping_address_id'],
114: | 'firstname' => $this->request->post['firstname'],
115: | 'lastname' => $this->request->post['lastname'],
116: | 'company' => $this->request->post['company'],
117: | 'address_1' => $this->request->post['address_1'],
118: | 'address_2' => $this->request->post['address_2'],
119: | 'postcode' => $this->request->post['postcode'],
120: | 'city' => $this->request->post['city'],
121: | 'zone_id' => $this->request->post['zone_id'],
122: | 'zone' => $zone,
123: | 'zone_code' => $zone_code,
124: | 'country_id' => (int)$this->request->post['country_id'],
125: | 'country' => $country,
126: | 'iso_code_2' => $iso_code_2,
127: | 'iso_code_3' => $iso_code_3,
128: | 'address_format' => $address_format,
129: | 'custom_field' => $this->request->post['custom_field'] ?? []
130: | ];
131: |
132: | $json['success'] = $this->language->get('text_success');
133: | }
134: |
135: | $this->response->addHeader('Content-Type: application/json');
136: | $this->response->setOutput(json_encode($json));
137: | }
138: | }
139: |

OpenCart API API documentation generated by ApiGen dev-master