/upload/catalog/controller/checkout/guest_shipping.php

https://bitbucket.org/deringer/opencart · PHP · 198 lines · 155 code · 40 blank · 3 comment · 51 complexity · 407baa67ae19e6d83a8071fa68d629ad MD5 · raw file

  1. <?php
  2. class ControllerCheckoutGuestShipping extends Controller {
  3. public function index() {
  4. $this->language->load('checkout/checkout');
  5. $this->data['text_select'] = $this->language->get('text_select');
  6. $this->data['text_none'] = $this->language->get('text_none');
  7. $this->data['entry_firstname'] = $this->language->get('entry_firstname');
  8. $this->data['entry_lastname'] = $this->language->get('entry_lastname');
  9. $this->data['entry_company'] = $this->language->get('entry_company');
  10. $this->data['entry_address_1'] = $this->language->get('entry_address_1');
  11. $this->data['entry_address_2'] = $this->language->get('entry_address_2');
  12. $this->data['entry_postcode'] = $this->language->get('entry_postcode');
  13. $this->data['entry_city'] = $this->language->get('entry_city');
  14. $this->data['entry_country'] = $this->language->get('entry_country');
  15. $this->data['entry_zone'] = $this->language->get('entry_zone');
  16. $this->data['button_continue'] = $this->language->get('button_continue');
  17. if (isset($this->session->data['guest']['shipping']['firstname'])) {
  18. $this->data['firstname'] = $this->session->data['guest']['shipping']['firstname'];
  19. } else {
  20. $this->data['firstname'] = '';
  21. }
  22. if (isset($this->session->data['guest']['shipping']['lastname'])) {
  23. $this->data['lastname'] = $this->session->data['guest']['shipping']['lastname'];
  24. } else {
  25. $this->data['lastname'] = '';
  26. }
  27. if (isset($this->session->data['guest']['shipping']['company'])) {
  28. $this->data['company'] = $this->session->data['guest']['shipping']['company'];
  29. } else {
  30. $this->data['company'] = '';
  31. }
  32. if (isset($this->session->data['guest']['shipping']['address_1'])) {
  33. $this->data['address_1'] = $this->session->data['guest']['shipping']['address_1'];
  34. } else {
  35. $this->data['address_1'] = '';
  36. }
  37. if (isset($this->session->data['guest']['shipping']['address_2'])) {
  38. $this->data['address_2'] = $this->session->data['guest']['shipping']['address_2'];
  39. } else {
  40. $this->data['address_2'] = '';
  41. }
  42. if (isset($this->session->data['guest']['shipping']['postcode'])) {
  43. $this->data['postcode'] = $this->session->data['guest']['shipping']['postcode'];
  44. } elseif (isset($this->session->data['shipping_postcode'])) {
  45. $this->data['postcode'] = $this->session->data['shipping_postcode'];
  46. } else {
  47. $this->data['postcode'] = '';
  48. }
  49. if (isset($this->session->data['guest']['shipping']['city'])) {
  50. $this->data['city'] = $this->session->data['guest']['shipping']['city'];
  51. } else {
  52. $this->data['city'] = '';
  53. }
  54. if (isset($this->session->data['guest']['shipping']['country_id'])) {
  55. $this->data['country_id'] = $this->session->data['guest']['shipping']['country_id'];
  56. } elseif (isset($this->session->data['shipping_country_id'])) {
  57. $this->data['country_id'] = $this->session->data['shipping_country_id'];
  58. } else {
  59. $this->data['country_id'] = $this->config->get('config_country_id');
  60. }
  61. if (isset($this->session->data['guest']['shipping']['zone_id'])) {
  62. $this->data['zone_id'] = $this->session->data['guest']['shipping']['zone_id'];
  63. } elseif (isset($this->session->data['shipping_zone_id'])) {
  64. $this->data['zone_id'] = $this->session->data['shipping_zone_id'];
  65. } else {
  66. $this->data['zone_id'] = '';
  67. }
  68. $this->load->model('localisation/country');
  69. $this->data['countries'] = $this->model_localisation_country->getCountries();
  70. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/guest_shipping.tpl')) {
  71. $this->template = $this->config->get('config_template') . '/template/checkout/guest_shipping.tpl';
  72. } else {
  73. $this->template = 'default/template/checkout/guest_shipping.tpl';
  74. }
  75. $this->response->setOutput($this->render());
  76. }
  77. public function validate() {
  78. $this->language->load('checkout/checkout');
  79. $json = array();
  80. // Validate if customer is logged in.
  81. if ($this->customer->isLogged()) {
  82. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  83. }
  84. // Validate cart has products and has stock.
  85. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  86. $json['redirect'] = $this->url->link('checkout/cart');
  87. }
  88. // Check if guest checkout is avaliable.
  89. if (!$this->config->get('config_guest_checkout') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {
  90. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  91. }
  92. if (!$json) {
  93. if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
  94. $json['error']['firstname'] = $this->language->get('error_firstname');
  95. }
  96. if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
  97. $json['error']['lastname'] = $this->language->get('error_lastname');
  98. }
  99. if ((utf8_strlen($this->request->post['address_1']) < 3) || (utf8_strlen($this->request->post['address_1']) > 128)) {
  100. $json['error']['address_1'] = $this->language->get('error_address_1');
  101. }
  102. if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 128)) {
  103. $json['error']['city'] = $this->language->get('error_city');
  104. }
  105. $this->load->model('localisation/country');
  106. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  107. if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
  108. $json['error']['postcode'] = $this->language->get('error_postcode');
  109. }
  110. if ($this->request->post['country_id'] == '') {
  111. $json['error']['country'] = $this->language->get('error_country');
  112. }
  113. if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
  114. $json['error']['zone'] = $this->language->get('error_zone');
  115. }
  116. }
  117. if (!$json) {
  118. $this->session->data['guest']['shipping']['firstname'] = trim($this->request->post['firstname']);
  119. $this->session->data['guest']['shipping']['lastname'] = trim($this->request->post['lastname']);
  120. $this->session->data['guest']['shipping']['company'] = trim($this->request->post['company']);
  121. $this->session->data['guest']['shipping']['address_1'] = $this->request->post['address_1'];
  122. $this->session->data['guest']['shipping']['address_2'] = $this->request->post['address_2'];
  123. $this->session->data['guest']['shipping']['postcode'] = $this->request->post['postcode'];
  124. $this->session->data['guest']['shipping']['city'] = $this->request->post['city'];
  125. $this->session->data['guest']['shipping']['country_id'] = $this->request->post['country_id'];
  126. $this->session->data['guest']['shipping']['zone_id'] = $this->request->post['zone_id'];
  127. $this->load->model('localisation/country');
  128. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  129. if ($country_info) {
  130. $this->session->data['guest']['shipping']['country'] = $country_info['name'];
  131. $this->session->data['guest']['shipping']['iso_code_2'] = $country_info['iso_code_2'];
  132. $this->session->data['guest']['shipping']['iso_code_3'] = $country_info['iso_code_3'];
  133. $this->session->data['guest']['shipping']['address_format'] = $country_info['address_format'];
  134. } else {
  135. $this->session->data['guest']['shipping']['country'] = '';
  136. $this->session->data['guest']['shipping']['iso_code_2'] = '';
  137. $this->session->data['guest']['shipping']['iso_code_3'] = '';
  138. $this->session->data['guest']['shipping']['address_format'] = '';
  139. }
  140. $this->load->model('localisation/zone');
  141. $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
  142. if ($zone_info) {
  143. $this->session->data['guest']['shipping']['zone'] = $zone_info['name'];
  144. $this->session->data['guest']['shipping']['zone_code'] = $zone_info['code'];
  145. } else {
  146. $this->session->data['guest']['shipping']['zone'] = '';
  147. $this->session->data['guest']['shipping']['zone_code'] = '';
  148. }
  149. $this->session->data['shipping_country_id'] = $this->request->post['country_id'];
  150. $this->session->data['shipping_zone_id'] = $this->request->post['zone_id'];
  151. $this->session->data['shipping_postcode'] = $this->request->post['postcode'];
  152. unset($this->session->data['shipping_method']);
  153. unset($this->session->data['shipping_methods']);
  154. }
  155. $this->response->setOutput(json_encode($json));
  156. }
  157. }
  158. ?>