shipping_address.php (7450B)
1 <?php 2 class ControllerCheckoutShippingAddress extends Controller { 3 public function index() { 4 $this->load->language('checkout/checkout'); 5 6 if (isset($this->session->data['shipping_address']['address_id'])) { 7 $data['address_id'] = $this->session->data['shipping_address']['address_id']; 8 } else { 9 $data['address_id'] = $this->customer->getAddressId(); 10 } 11 12 $this->load->model('account/address'); 13 14 $data['addresses'] = $this->model_account_address->getAddresses(); 15 16 if (isset($this->session->data['shipping_address']['postcode'])) { 17 $data['postcode'] = $this->session->data['shipping_address']['postcode']; 18 } else { 19 $data['postcode'] = ''; 20 } 21 22 if (isset($this->session->data['shipping_address']['country_id'])) { 23 $data['country_id'] = $this->session->data['shipping_address']['country_id']; 24 } else { 25 $data['country_id'] = $this->config->get('config_country_id'); 26 } 27 28 if (isset($this->session->data['shipping_address']['zone_id'])) { 29 $data['zone_id'] = $this->session->data['shipping_address']['zone_id']; 30 } else { 31 $data['zone_id'] = ''; 32 } 33 34 $this->load->model('localisation/country'); 35 36 $data['countries'] = $this->model_localisation_country->getCountries(); 37 38 // Custom Fields 39 $data['custom_fields'] = array(); 40 41 $this->load->model('account/custom_field'); 42 43 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 44 45 foreach ($custom_fields as $custom_field) { 46 if ($custom_field['location'] == 'address') { 47 $data['custom_fields'][] = $custom_field; 48 } 49 } 50 51 if (isset($this->session->data['shipping_address']['custom_field'])) { 52 $data['shipping_address_custom_field'] = $this->session->data['shipping_address']['custom_field']; 53 } else { 54 $data['shipping_address_custom_field'] = array(); 55 } 56 57 $this->response->setOutput($this->load->view('checkout/shipping_address', $data)); 58 } 59 60 public function save() { 61 $this->load->language('checkout/checkout'); 62 63 $json = array(); 64 65 // Validate if customer is logged in. 66 if (!$this->customer->isLogged()) { 67 $json['redirect'] = $this->url->link('checkout/checkout', '', true); 68 } 69 70 // Validate if shipping is required. If not the customer should not have reached this page. 71 if (!$this->cart->hasShipping()) { 72 $json['redirect'] = $this->url->link('checkout/checkout', '', true); 73 } 74 75 // Validate cart has products and has stock. 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'); 78 } 79 80 // Validate minimum quantity requirements. 81 $products = $this->cart->getProducts(); 82 83 foreach ($products as $product) { 84 $product_total = 0; 85 86 foreach ($products as $product_2) { 87 if ($product_2['product_id'] == $product['product_id']) { 88 $product_total += $product_2['quantity']; 89 } 90 } 91 92 if ($product['minimum'] > $product_total) { 93 $json['redirect'] = $this->url->link('checkout/cart'); 94 95 break; 96 } 97 } 98 99 if (!$json) { 100 $this->load->model('account/address'); 101 102 if (isset($this->request->post['shipping_address']) && $this->request->post['shipping_address'] == 'existing') { 103 if (empty($this->request->post['address_id'])) { 104 $json['error']['warning'] = $this->language->get('error_address'); 105 } elseif (!in_array($this->request->post['address_id'], array_keys($this->model_account_address->getAddresses()))) { 106 $json['error']['warning'] = $this->language->get('error_address'); 107 } 108 109 if (!$json) { 110 $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->request->post['address_id']); 111 112 unset($this->session->data['shipping_method']); 113 unset($this->session->data['shipping_methods']); 114 } 115 } else { 116 if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { 117 $json['error']['firstname'] = $this->language->get('error_firstname'); 118 } 119 120 if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { 121 $json['error']['lastname'] = $this->language->get('error_lastname'); 122 } 123 124 if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) { 125 $json['error']['address_1'] = $this->language->get('error_address_1'); 126 } 127 128 if ((utf8_strlen(trim($this->request->post['city'])) < 2) || (utf8_strlen(trim($this->request->post['city'])) > 128)) { 129 $json['error']['city'] = $this->language->get('error_city'); 130 } 131 132 $this->load->model('localisation/country'); 133 134 $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); 135 136 if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) { 137 $json['error']['postcode'] = $this->language->get('error_postcode'); 138 } 139 140 if ($this->request->post['country_id'] == '') { 141 $json['error']['country'] = $this->language->get('error_country'); 142 } 143 144 if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) { 145 $json['error']['zone'] = $this->language->get('error_zone'); 146 } 147 148 // Custom field validation 149 $this->load->model('account/custom_field'); 150 151 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 152 153 foreach ($custom_fields as $custom_field) { 154 if ($custom_field['location'] == 'address') { 155 if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { 156 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 157 } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { 158 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 159 } 160 } 161 } 162 163 if (!$json) { 164 $address_id = $this->model_account_address->addAddress($this->customer->getId(), $this->request->post); 165 166 $this->session->data['shipping_address'] = $this->model_account_address->getAddress($address_id); 167 168 // If no default address ID set we use the last address 169 if (!$this->customer->getAddressId()) { 170 $this->load->model('account/customer'); 171 172 $this->model_account_customer->editAddressId($this->customer->getId(), $address_id); 173 } 174 175 unset($this->session->data['shipping_method']); 176 unset($this->session->data['shipping_methods']); 177 } 178 } 179 } 180 181 $this->response->addHeader('Content-Type: application/json'); 182 $this->response->setOutput(json_encode($json)); 183 } 184 }