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