$this->language->load('checkout/cart');
$json = array();
if (!$this->cart->hasProducts()) {
$json['error']['warning'] = $this->language->get('error_product');
}
if (!$this->cart->hasShipping()) {
$json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
}
if ($this->request->post['country_id'] == '') {
$json['error']['country'] = $this->language->get('error_country');
}
if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
$json['error']['zone'] = $this->language->get('error_zone');
}
$this->load->model('localisation/country');
$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
$json['error']['postcode'] = $this->language->get('error_postcode');
}
if (!$json) {
$this->tax->setShippingAddress($this->request->post['country_id'], $this->request->post['zone_id']);
// Default Shipping Address
$this->session->data['shipping_country_id'] = $this->request->post['country_id'];
$this->session->data['shipping_zone_id'] = $this->request->post['zone_id'];
$this->session->data['shipping_postcode'] = $this->request->post['postcode'];
if ($country_info) {
$country = $country_info['name'];
$iso_code_2 = $country_info['iso_code_2'];
$iso_code_3 = $country_info['iso_code_3'];
$address_format = $country_info['address_format'];
} else {
$country = '';
$iso_code_2 = '';
$iso_code_3 = '';
$address_format = '';
}
$this->load->model('localisation/zone');
$zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
if ($zone_info) {
$zone = $zone_info['name'];
$zone_code = $zone_info['code'];
} else {
$zone = '';
$zone_code = '';
}
$address_data = array(
'firstname' => '',
'lastname' => '',
'company' => '',
'address_1' => '',
'address_2' => '',
'postcode' => $this->request->post['postcode'],
'city' => '',
'zone_id' => $this->request->post['zone_id'],
'zone' => $zone,
'zone_code' => $zone_code,
'country_id' => $this->request->post['country_id'],
'country' => $country,
'iso_code_2' => $iso_code_2,
'iso_code_3' => $iso_code_3,
'address_format' => $address_format
);
$quote_data = array();
$this->load->model('setting/extension');
$results = $this->model_setting_extension->getExtensions('shipping');
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('shipping/' . $result['code']);
$quote = $this->{'model_shipping_' . $result['code']}->getQuote($address_data);
if ($quote) {
$quote_data[$result['code']] = array(
'title' => $quote['title'],
'quote' => $quote['quote'],
'sort_order' => $quote['sort_order'],
'error' => $quote['error']
);
}
}
}
$sort_order = array();
foreach ($quote_data as $key => $value) {
$sort_order[$key] = $value['sort_order'];
}
array_multisort($sort_order, SORT_ASC, $quote_data);
$this->session->data['shipping_methods'] = $quote_data;
if ($this->session->data['shipping_methods']) {
$json['shipping_method'] = $this->session->data['shipping_methods'];
} else {
$json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
}
}
$this->response->setOutput(json_encode($json));