shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

shipping.php (7148B)


      1 <?php
      2 class ControllerExtensionTotalShipping extends Controller {
      3 	public function index() {
      4 		if ($this->config->get('total_shipping_status') && $this->config->get('total_shipping_estimator') && $this->cart->hasShipping()) {
      5 			$this->load->language('extension/total/shipping');
      6 
      7 			if (isset($this->session->data['shipping_address']['country_id'])) {
      8 				$data['country_id'] = $this->session->data['shipping_address']['country_id'];
      9 			} else {
     10 				$data['country_id'] = $this->config->get('config_country_id');
     11 			}
     12 
     13 			$this->load->model('localisation/country');
     14 
     15 			$data['countries'] = $this->model_localisation_country->getCountries();
     16 
     17 			if (isset($this->session->data['shipping_address']['zone_id'])) {
     18 				$data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
     19 			} else {
     20 				$data['zone_id'] = '';
     21 			}
     22 
     23 			if (isset($this->session->data['shipping_address']['postcode'])) {
     24 				$data['postcode'] = $this->session->data['shipping_address']['postcode'];
     25 			} else {
     26 				$data['postcode'] = '';
     27 			}
     28 
     29 			if (isset($this->session->data['shipping_method'])) {
     30 				$data['shipping_method'] = $this->session->data['shipping_method']['code'];
     31 			} else {
     32 				$data['shipping_method'] = '';
     33 			}
     34 
     35 			return $this->load->view('extension/total/shipping', $data);
     36 		}
     37 	}
     38 
     39 	public function quote() {
     40 		$this->load->language('extension/total/shipping');
     41 
     42 		$json = array();
     43 
     44 		if (!$this->cart->hasProducts()) {
     45 			$json['error']['warning'] = $this->language->get('error_product');
     46 		}
     47 
     48 		if (!$this->cart->hasShipping()) {
     49 			$json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
     50 		}
     51 
     52 		if ($this->request->post['country_id'] == '') {
     53 			$json['error']['country'] = $this->language->get('error_country');
     54 		}
     55 
     56 		if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
     57 			$json['error']['zone'] = $this->language->get('error_zone');
     58 		}
     59 
     60 		$this->load->model('localisation/country');
     61 
     62 		$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
     63 
     64 		if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
     65 			$json['error']['postcode'] = $this->language->get('error_postcode');
     66 		}
     67 
     68 		if (!$json) {
     69 			$this->tax->setShippingAddress($this->request->post['country_id'], $this->request->post['zone_id']);
     70 
     71 			if ($country_info) {
     72 				$country = $country_info['name'];
     73 				$iso_code_2 = $country_info['iso_code_2'];
     74 				$iso_code_3 = $country_info['iso_code_3'];
     75 				$address_format = $country_info['address_format'];
     76 			} else {
     77 				$country = '';
     78 				$iso_code_2 = '';
     79 				$iso_code_3 = '';
     80 				$address_format = '';
     81 			}
     82 
     83 			$this->load->model('localisation/zone');
     84 
     85 			$zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
     86 
     87 			if ($zone_info) {
     88 				$zone = $zone_info['name'];
     89 				$zone_code = $zone_info['code'];
     90 			} else {
     91 				$zone = '';
     92 				$zone_code = '';
     93 			}
     94 
     95 			$this->session->data['shipping_address'] = array(
     96 				'firstname'      => '',
     97 				'lastname'       => '',
     98 				'company'        => '',
     99 				'address_1'      => '',
    100 				'address_2'      => '',
    101 				'postcode'       => $this->request->post['postcode'],
    102 				'city'           => '',
    103 				'zone_id'        => $this->request->post['zone_id'],
    104 				'zone'           => $zone,
    105 				'zone_code'      => $zone_code,
    106 				'country_id'     => $this->request->post['country_id'],
    107 				'country'        => $country,
    108 				'iso_code_2'     => $iso_code_2,
    109 				'iso_code_3'     => $iso_code_3,
    110 				'address_format' => $address_format
    111 			);
    112 
    113 			$quote_data = array();
    114 
    115 			$this->load->model('setting/extension');
    116 
    117 			$results = $this->model_setting_extension->getExtensions('shipping');
    118 
    119 			foreach ($results as $result) {
    120 				if ($this->config->get('shipping_' . $result['code'] . '_status')) {
    121 					$this->load->model('extension/shipping/' . $result['code']);
    122 
    123 					$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);
    124 
    125 					if ($quote) {
    126 						$quote_data[$result['code']] = array(
    127 							'title'      => $quote['title'],
    128 							'quote'      => $quote['quote'],
    129 							'sort_order' => $quote['sort_order'],
    130 							'error'      => $quote['error']
    131 						);
    132 					}
    133 				}
    134 			}
    135 
    136 			$sort_order = array();
    137 
    138 			foreach ($quote_data as $key => $value) {
    139 				$sort_order[$key] = $value['sort_order'];
    140 			}
    141 
    142 			array_multisort($sort_order, SORT_ASC, $quote_data);
    143 
    144 			$this->session->data['shipping_methods'] = $quote_data;
    145 
    146 			if ($this->session->data['shipping_methods']) {
    147 				$json['shipping_method'] = $this->session->data['shipping_methods'];
    148 			} else {
    149 				$json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
    150 			}
    151 		}
    152 
    153 		$this->response->addHeader('Content-Type: application/json');
    154 		$this->response->setOutput(json_encode($json));
    155 	}
    156 
    157 	public function shipping() {
    158 		$this->load->language('extension/total/shipping');
    159 
    160 		$json = array();
    161 
    162 		if (!empty($this->request->post['shipping_method'])) {
    163 			$shipping = explode('.', $this->request->post['shipping_method']);
    164 
    165 			if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
    166 				$json['warning'] = $this->language->get('error_shipping');
    167 			}
    168 		} else {
    169 			$json['warning'] = $this->language->get('error_shipping');
    170 		}
    171 
    172 		if (!$json) {
    173 			$shipping = explode('.', $this->request->post['shipping_method']);
    174 
    175 			$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
    176 
    177 			$this->session->data['success'] = $this->language->get('text_success');
    178 
    179 			$json['redirect'] = $this->url->link('checkout/cart');
    180 		}
    181 
    182 		$this->response->addHeader('Content-Type: application/json');
    183 		$this->response->setOutput(json_encode($json));
    184 	}
    185 
    186 	public function country() {
    187 		$json = array();
    188 
    189 		$this->load->model('localisation/country');
    190 
    191 		$country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
    192 
    193 		if ($country_info) {
    194 			$this->load->model('localisation/zone');
    195 
    196 			$json = array(
    197 				'country_id'        => $country_info['country_id'],
    198 				'name'              => $country_info['name'],
    199 				'iso_code_2'        => $country_info['iso_code_2'],
    200 				'iso_code_3'        => $country_info['iso_code_3'],
    201 				'address_format'    => $country_info['address_format'],
    202 				'postcode_required' => $country_info['postcode_required'],
    203 				'zone'              => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
    204 				'status'            => $country_info['status']
    205 			);
    206 		}
    207 
    208 		$this->response->addHeader('Content-Type: application/json');
    209 		$this->response->setOutput(json_encode($json));
    210 	}
    211 }