shop.balmet.com

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

guest_shipping.php (8882B)


      1 <?php
      2 class ControllerCheckoutGuestShipping extends Controller {
      3 	public function index() {
      4 		$this->load->language('checkout/checkout');
      5 
      6 		if (isset($this->session->data['shipping_address']['firstname'])) {
      7 			$data['firstname'] = $this->session->data['shipping_address']['firstname'];
      8 		} else {
      9 			$data['firstname'] = '';
     10 		}
     11 
     12 		if (isset($this->session->data['shipping_address']['lastname'])) {
     13 			$data['lastname'] = $this->session->data['shipping_address']['lastname'];
     14 		} else {
     15 			$data['lastname'] = '';
     16 		}
     17 
     18 		if (isset($this->session->data['shipping_address']['company'])) {
     19 			$data['company'] = $this->session->data['shipping_address']['company'];
     20 		} else {
     21 			$data['company'] = '';
     22 		}
     23 
     24 		if (isset($this->session->data['shipping_address']['address_1'])) {
     25 			$data['address_1'] = $this->session->data['shipping_address']['address_1'];
     26 		} else {
     27 			$data['address_1'] = '';
     28 		}
     29 
     30 		if (isset($this->session->data['shipping_address']['address_2'])) {
     31 			$data['address_2'] = $this->session->data['shipping_address']['address_2'];
     32 		} else {
     33 			$data['address_2'] = '';
     34 		}
     35 
     36 		if (isset($this->session->data['shipping_address']['postcode'])) {
     37 			$data['postcode'] = $this->session->data['shipping_address']['postcode'];
     38 		} else {
     39 			$data['postcode'] = '';
     40 		}
     41 
     42 		if (isset($this->session->data['shipping_address']['city'])) {
     43 			$data['city'] = $this->session->data['shipping_address']['city'];
     44 		} else {
     45 			$data['city'] = '';
     46 		}
     47 
     48 		if (isset($this->session->data['shipping_address']['country_id'])) {
     49 			$data['country_id'] = $this->session->data['shipping_address']['country_id'];
     50 		} else {
     51 			$data['country_id'] = $this->config->get('config_country_id');
     52 		}
     53 
     54 		if (isset($this->session->data['shipping_address']['zone_id'])) {
     55 			$data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
     56 		} else {
     57 			$data['zone_id'] = '';
     58 		}
     59 
     60 		$this->load->model('localisation/country');
     61 
     62 		$data['countries'] = $this->model_localisation_country->getCountries();
     63 
     64 		// Custom Fields
     65 		$this->load->model('account/custom_field');
     66 		
     67 		$custom_fields = $this->model_account_custom_field->getCustomFields($this->session->data['guest']['customer_group_id']);
     68 
     69 		foreach ($custom_fields as $custom_field) {
     70 			if ($custom_field['location'] == 'address') {
     71 				$data['custom_fields'][] = $custom_field;
     72 			}
     73 		}
     74 		
     75 		if (isset($this->session->data['shipping_address']['custom_field'])) {
     76 			$data['address_custom_field'] = $this->session->data['shipping_address']['custom_field'];
     77 		} else {
     78 			$data['address_custom_field'] = array();
     79 		}
     80 		
     81 		$this->response->setOutput($this->load->view('checkout/guest_shipping', $data));
     82 	}
     83 
     84 	public function save() {
     85 		$this->load->language('checkout/checkout');
     86 
     87 		$json = array();
     88 
     89 		// Validate if customer is logged in.
     90 		if ($this->customer->isLogged()) {
     91 			$json['redirect'] = $this->url->link('checkout/checkout', '', true);
     92 		}
     93 
     94 		// Validate cart has products and has stock.
     95 		if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
     96 			$json['redirect'] = $this->url->link('checkout/cart');
     97 		}
     98 
     99 		// Check if guest checkout is available.
    100 		if (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {
    101 			$json['redirect'] = $this->url->link('checkout/checkout', '', true);
    102 		}
    103 
    104 		if (!$json) {
    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(trim($this->request->post['city'])) < 2) || (utf8_strlen(trim($this->request->post['city'])) > 128)) {
    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->session->data['guest']['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 
    153 		if (!$json) {
    154 			$this->session->data['shipping_address']['firstname'] = $this->request->post['firstname'];
    155 			$this->session->data['shipping_address']['lastname'] = $this->request->post['lastname'];
    156 			$this->session->data['shipping_address']['company'] = $this->request->post['company'];
    157 			$this->session->data['shipping_address']['address_1'] = $this->request->post['address_1'];
    158 			$this->session->data['shipping_address']['address_2'] = $this->request->post['address_2'];
    159 			$this->session->data['shipping_address']['postcode'] = $this->request->post['postcode'];
    160 			$this->session->data['shipping_address']['city'] = $this->request->post['city'];
    161 			$this->session->data['shipping_address']['country_id'] = $this->request->post['country_id'];
    162 			$this->session->data['shipping_address']['zone_id'] = $this->request->post['zone_id'];
    163 
    164 			$this->load->model('localisation/country');
    165 
    166 			$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
    167 
    168 			if ($country_info) {
    169 				$this->session->data['shipping_address']['country'] = $country_info['name'];
    170 				$this->session->data['shipping_address']['iso_code_2'] = $country_info['iso_code_2'];
    171 				$this->session->data['shipping_address']['iso_code_3'] = $country_info['iso_code_3'];
    172 				$this->session->data['shipping_address']['address_format'] = $country_info['address_format'];
    173 			} else {
    174 				$this->session->data['shipping_address']['country'] = '';
    175 				$this->session->data['shipping_address']['iso_code_2'] = '';
    176 				$this->session->data['shipping_address']['iso_code_3'] = '';
    177 				$this->session->data['shipping_address']['address_format'] = '';
    178 			}
    179 
    180 			$this->load->model('localisation/zone');
    181 
    182 			$zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
    183 
    184 			if ($zone_info) {
    185 				$this->session->data['shipping_address']['zone'] = $zone_info['name'];
    186 				$this->session->data['shipping_address']['zone_code'] = $zone_info['code'];
    187 			} else {
    188 				$this->session->data['shipping_address']['zone'] = '';
    189 				$this->session->data['shipping_address']['zone_code'] = '';
    190 			}
    191 
    192 			if (isset($this->request->post['custom_field'])) {
    193 				$this->session->data['shipping_address']['custom_field'] = $this->request->post['custom_field']['address'];
    194 			} else {
    195 				$this->session->data['shipping_address']['custom_field'] = array();
    196 			}
    197 
    198 			unset($this->session->data['shipping_method']);
    199 			unset($this->session->data['shipping_methods']);
    200 		}
    201 
    202 		$this->response->addHeader('Content-Type: application/json');
    203 		$this->response->setOutput(json_encode($json));
    204 	}
    205 }