shipping_method.php (4085B)
1 <?php 2 class ControllerCheckoutShippingMethod extends Controller { 3 public function index() { 4 $this->load->language('checkout/checkout'); 5 6 if (isset($this->session->data['shipping_address'])) { 7 // Shipping Methods 8 $method_data = array(); 9 10 $this->load->model('setting/extension'); 11 12 $results = $this->model_setting_extension->getExtensions('shipping'); 13 14 foreach ($results as $result) { 15 if ($this->config->get('shipping_' . $result['code'] . '_status')) { 16 $this->load->model('extension/shipping/' . $result['code']); 17 18 $quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']); 19 20 if ($quote) { 21 $method_data[$result['code']] = array( 22 'title' => $quote['title'], 23 'quote' => $quote['quote'], 24 'sort_order' => $quote['sort_order'], 25 'error' => $quote['error'] 26 ); 27 } 28 } 29 } 30 31 $sort_order = array(); 32 33 foreach ($method_data as $key => $value) { 34 $sort_order[$key] = $value['sort_order']; 35 } 36 37 array_multisort($sort_order, SORT_ASC, $method_data); 38 39 $this->session->data['shipping_methods'] = $method_data; 40 } 41 42 if (empty($this->session->data['shipping_methods'])) { 43 $data['error_warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact')); 44 } else { 45 $data['error_warning'] = ''; 46 } 47 48 if (isset($this->session->data['shipping_methods'])) { 49 $data['shipping_methods'] = $this->session->data['shipping_methods']; 50 } else { 51 $data['shipping_methods'] = array(); 52 } 53 54 if (isset($this->session->data['shipping_method']['code'])) { 55 $data['code'] = $this->session->data['shipping_method']['code']; 56 } else { 57 $data['code'] = ''; 58 } 59 60 if (isset($this->session->data['comment'])) { 61 $data['comment'] = $this->session->data['comment']; 62 } else { 63 $data['comment'] = ''; 64 } 65 66 $this->response->setOutput($this->load->view('checkout/shipping_method', $data)); 67 } 68 69 public function save() { 70 $this->load->language('checkout/checkout'); 71 72 $json = array(); 73 74 // Validate if shipping is required. If not the customer should not have reached this page. 75 if (!$this->cart->hasShipping()) { 76 $json['redirect'] = $this->url->link('checkout/checkout', '', true); 77 } 78 79 // Validate if shipping address has been set. 80 if (!isset($this->session->data['shipping_address'])) { 81 $json['redirect'] = $this->url->link('checkout/checkout', '', true); 82 } 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 89 // Validate minimum quantity requirements. 90 $products = $this->cart->getProducts(); 91 92 foreach ($products as $product) { 93 $product_total = 0; 94 95 foreach ($products as $product_2) { 96 if ($product_2['product_id'] == $product['product_id']) { 97 $product_total += $product_2['quantity']; 98 } 99 } 100 101 if ($product['minimum'] > $product_total) { 102 $json['redirect'] = $this->url->link('checkout/cart'); 103 104 break; 105 } 106 } 107 108 if (!isset($this->request->post['shipping_method'])) { 109 $json['error']['warning'] = $this->language->get('error_shipping'); 110 } else { 111 $shipping = explode('.', $this->request->post['shipping_method']); 112 113 if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { 114 $json['error']['warning'] = $this->language->get('error_shipping'); 115 } 116 } 117 118 if (!$json) { 119 $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; 120 121 $this->session->data['comment'] = strip_tags($this->request->post['comment']); 122 } 123 124 $this->response->addHeader('Content-Type: application/json'); 125 $this->response->setOutput(json_encode($json)); 126 } 127 }