vq2-catalog_controller_checkout_cart.php (17282B)
1 <?php 2 class ControllerCheckoutCart extends Controller { 3 public function index() { 4 $this->load->language('checkout/cart'); 5 6 $this->document->setTitle($this->language->get('heading_title')); 7 8 $data['breadcrumbs'] = array(); 9 10 $data['breadcrumbs'][] = array( 11 'href' => $this->url->link('common/home'), 12 'text' => $this->language->get('text_home') 13 ); 14 15 $data['breadcrumbs'][] = array( 16 'href' => $this->url->link('checkout/cart'), 17 'text' => $this->language->get('heading_title') 18 ); 19 20 if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) { 21 if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) { 22 $data['error_warning'] = $this->language->get('error_stock'); 23 } elseif (isset($this->session->data['error'])) { 24 $data['error_warning'] = $this->session->data['error']; 25 26 unset($this->session->data['error']); 27 } else { 28 $data['error_warning'] = ''; 29 } 30 31 if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) { 32 $data['attention'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register')); 33 } else { 34 $data['attention'] = ''; 35 } 36 37 if (isset($this->session->data['success'])) { 38 $data['success'] = $this->session->data['success']; 39 40 unset($this->session->data['success']); 41 } else { 42 $data['success'] = ''; 43 } 44 45 $data['action'] = $this->url->link('checkout/cart/edit', '', true); 46 47 if ($this->config->get('config_cart_weight')) { 48 $data['weight'] = $this->weight->format($this->cart->getWeight(), $this->config->get('config_weight_class_id'), $this->language->get('decimal_point'), $this->language->get('thousand_point')); 49 } else { 50 $data['weight'] = ''; 51 } 52 53 $this->load->model('tool/image'); 54 $this->load->model('tool/upload'); 55 56 $data['products'] = array(); 57 58 $products = $this->cart->getProducts(); 59 60 foreach ($products as $product) { 61 $product_total = 0; 62 63 foreach ($products as $product_2) { 64 if ($product_2['product_id'] == $product['product_id']) { 65 $product_total += $product_2['quantity']; 66 } 67 } 68 69 if ($product['minimum'] > $product_total) { 70 $data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); 71 } 72 73 if ($product['image']) { 74 $image = $this->model_tool_image->resize($product['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_height')); 75 } else { 76 $image = ''; 77 } 78 79 $option_data = array(); 80 81 foreach ($product['option'] as $option) { 82 if ($option['type'] != 'file') { 83 $value = $option['value']; 84 } else { 85 $upload_info = $this->model_tool_upload->getUploadByCode($option['value']); 86 87 if ($upload_info) { 88 $value = $upload_info['name']; 89 } else { 90 $value = ''; 91 } 92 } 93 94 $option_data[] = array( 95 'name' => $option['name'], 96 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) 97 ); 98 } 99 100 // Display prices 101 if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 102 $unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')); 103 104 $price = $this->currency->format($unit_price, $this->session->data['currency']); 105 $total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']); 106 } else { 107 $price = false; 108 $total = false; 109 } 110 111 $recurring = ''; 112 113 if ($product['recurring']) { 114 $frequencies = array( 115 'day' => $this->language->get('text_day'), 116 'week' => $this->language->get('text_week'), 117 'semi_month' => $this->language->get('text_semi_month'), 118 'month' => $this->language->get('text_month'), 119 'year' => $this->language->get('text_year') 120 ); 121 122 if ($product['recurring']['trial']) { 123 $recurring = sprintf($this->language->get('text_trial_description'), $this->currency->format($this->tax->calculate($product['recurring']['trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['trial_cycle'], $frequencies[$product['recurring']['trial_frequency']], $product['recurring']['trial_duration']) . ' '; 124 } 125 126 if ($product['recurring']['duration']) { 127 $recurring .= sprintf($this->language->get('text_payment_description'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']); 128 } else { 129 $recurring .= sprintf($this->language->get('text_payment_cancel'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']); 130 } 131 } 132 133 $data['products'][] = array( 134 'cart_id' => $product['cart_id'], 135 'thumb' => $image, 136 'name' => $product['name'], 137 'model' => $product['model'], 138 'option' => $option_data, 139 'recurring' => $recurring, 140 'quantity' => $product['quantity'], 141 'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')), 142 'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''), 143 'price' => $price, 144 'total' => $total, 145 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']) 146 ); 147 } 148 149 // Gift Voucher 150 $data['vouchers'] = array(); 151 152 if (!empty($this->session->data['vouchers'])) { 153 foreach ($this->session->data['vouchers'] as $key => $voucher) { 154 $data['vouchers'][] = array( 155 'key' => $key, 156 'description' => $voucher['description'], 157 'amount' => $this->currency->format($voucher['amount'], $this->session->data['currency']), 158 'remove' => $this->url->link('checkout/cart', 'remove=' . $key) 159 ); 160 } 161 } 162 163 // Totals 164 $this->load->model('setting/extension'); 165 166 $totals = array(); 167 $taxes = $this->cart->getTaxes(); 168 $total = 0; 169 170 // Because __call can not keep var references so we put them into an array. 171 $total_data = array( 172 'totals' => &$totals, 173 'taxes' => &$taxes, 174 'total' => &$total 175 ); 176 177 // Display prices 178 if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 179 $sort_order = array(); 180 181 $results = $this->model_setting_extension->getExtensions('total'); 182 183 foreach ($results as $key => $value) { 184 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 185 } 186 187 array_multisort($sort_order, SORT_ASC, $results); 188 189 foreach ($results as $result) { 190 if ($this->config->get('total_' . $result['code'] . '_status')) { 191 $this->load->model('extension/total/' . $result['code']); 192 193 // We have to put the totals in an array so that they pass by reference. 194 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 195 } 196 } 197 198 $sort_order = array(); 199 200 foreach ($totals as $key => $value) { 201 $sort_order[$key] = $value['sort_order']; 202 } 203 204 array_multisort($sort_order, SORT_ASC, $totals); 205 } 206 207 $data['totals'] = array(); 208 209 foreach ($totals as $total) { 210 $data['totals'][] = array( 211 'title' => $total['title'], 212 'text' => $this->currency->format($total['value'], $this->session->data['currency']) 213 ); 214 } 215 216 $data['continue'] = $this->url->link('common/home'); 217 218 $data['checkout'] = $this->url->link('checkout/checkout', '', true); 219 220 $this->load->model('setting/extension'); 221 222 $data['modules'] = array(); 223 224 $files = glob(DIR_APPLICATION . '/controller/extension/total/*.php'); 225 226 if ($files) { 227 foreach ($files as $file) { 228 $result = $this->load->controller('extension/total/' . basename($file, '.php')); 229 230 if ($result) { 231 $data['modules'][] = $result; 232 } 233 } 234 } 235 236 $data['column_left'] = $this->load->controller('common/column_left'); 237 $data['column_right'] = $this->load->controller('common/column_right'); 238 $data['content_top'] = $this->load->controller('common/content_top'); 239 $data['content_bottom'] = $this->load->controller('common/content_bottom'); 240 $data['footer'] = $this->load->controller('common/footer'); 241 $data['header'] = $this->load->controller('common/header'); 242 243 $this->response->setOutput($this->load->view('checkout/cart', $data)); 244 } else { 245 $data['text_error'] = $this->language->get('text_empty'); 246 247 $data['continue'] = $this->url->link('common/home'); 248 249 unset($this->session->data['success']); 250 251 $data['column_left'] = $this->load->controller('common/column_left'); 252 $data['column_right'] = $this->load->controller('common/column_right'); 253 $data['content_top'] = $this->load->controller('common/content_top'); 254 $data['content_bottom'] = $this->load->controller('common/content_bottom'); 255 $data['footer'] = $this->load->controller('common/footer'); 256 $data['header'] = $this->load->controller('common/header'); 257 258 $this->response->setOutput($this->load->view('error/not_found', $data)); 259 } 260 } 261 262 public function add() { 263 $this->load->language('checkout/cart'); 264 265 $json = array(); 266 267 if (isset($this->request->post['product_id'])) { 268 $product_id = (int)$this->request->post['product_id']; 269 } else { 270 $product_id = 0; 271 } 272 273 $this->load->model('catalog/product'); 274 275 $product_info = $this->model_catalog_product->getProduct($product_id); 276 277 if ($product_info) { 278 if (isset($this->request->post['quantity'])) { 279 280 $quantity = (float)$this->request->post['quantity']; 281 282 } else { 283 $quantity = 1; 284 } 285 286 if (isset($this->request->post['option'])) { 287 $option = array_filter($this->request->post['option']); 288 } else { 289 $option = array(); 290 } 291 292 $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']); 293 294 foreach ($product_options as $product_option) { 295 if ($product_option['required'] && empty($option[$product_option['product_option_id']])) { 296 $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']); 297 } 298 } 299 300 if (isset($this->request->post['recurring_id'])) { 301 $recurring_id = $this->request->post['recurring_id']; 302 } else { 303 $recurring_id = 0; 304 } 305 306 $recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']); 307 308 if ($recurrings) { 309 $recurring_ids = array(); 310 311 foreach ($recurrings as $recurring) { 312 $recurring_ids[] = $recurring['recurring_id']; 313 } 314 315 if (!in_array($recurring_id, $recurring_ids)) { 316 $json['error']['recurring'] = $this->language->get('error_recurring_required'); 317 } 318 } 319 320 if (!$json) { 321 $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id); 322 323 $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart')); 324 325 // Unset all shipping and payment methods 326 unset($this->session->data['shipping_method']); 327 unset($this->session->data['shipping_methods']); 328 unset($this->session->data['payment_method']); 329 unset($this->session->data['payment_methods']); 330 331 // Totals 332 $this->load->model('setting/extension'); 333 334 $totals = array(); 335 $taxes = $this->cart->getTaxes(); 336 $total = 0; 337 338 // Because __call can not keep var references so we put them into an array. 339 $total_data = array( 340 'totals' => &$totals, 341 'taxes' => &$taxes, 342 'total' => &$total 343 ); 344 345 // Display prices 346 if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 347 $sort_order = array(); 348 349 $results = $this->model_setting_extension->getExtensions('total'); 350 351 foreach ($results as $key => $value) { 352 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 353 } 354 355 array_multisort($sort_order, SORT_ASC, $results); 356 357 foreach ($results as $result) { 358 if ($this->config->get('total_' . $result['code'] . '_status')) { 359 $this->load->model('extension/total/' . $result['code']); 360 361 // We have to put the totals in an array so that they pass by reference. 362 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 363 } 364 } 365 366 $sort_order = array(); 367 368 foreach ($totals as $key => $value) { 369 $sort_order[$key] = $value['sort_order']; 370 } 371 372 array_multisort($sort_order, SORT_ASC, $totals); 373 } 374 375 $json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency'])); 376 } else { 377 $json['redirect'] = str_replace('&', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id'])); 378 } 379 } 380 381 $this->response->addHeader('Content-Type: application/json'); 382 $this->response->setOutput(json_encode($json)); 383 } 384 385 public function edit() { 386 $this->load->language('checkout/cart'); 387 388 $json = array(); 389 390 // Update 391 if (!empty($this->request->post['quantity'])) { 392 foreach ($this->request->post['quantity'] as $key => $value) { 393 $this->cart->update($key, $value); 394 } 395 396 $this->session->data['success'] = $this->language->get('text_remove'); 397 398 unset($this->session->data['shipping_method']); 399 unset($this->session->data['shipping_methods']); 400 unset($this->session->data['payment_method']); 401 unset($this->session->data['payment_methods']); 402 unset($this->session->data['reward']); 403 404 $this->response->redirect($this->url->link('checkout/cart')); 405 } 406 407 $this->response->addHeader('Content-Type: application/json'); 408 $this->response->setOutput(json_encode($json)); 409 } 410 411 public function remove() { 412 $this->load->language('checkout/cart'); 413 414 $json = array(); 415 416 // Remove 417 if (isset($this->request->post['key'])) { 418 $this->cart->remove($this->request->post['key']); 419 420 unset($this->session->data['vouchers'][$this->request->post['key']]); 421 422 $json['success'] = $this->language->get('text_remove'); 423 424 unset($this->session->data['shipping_method']); 425 unset($this->session->data['shipping_methods']); 426 unset($this->session->data['payment_method']); 427 unset($this->session->data['payment_methods']); 428 unset($this->session->data['reward']); 429 430 // Totals 431 $this->load->model('setting/extension'); 432 433 $totals = array(); 434 $taxes = $this->cart->getTaxes(); 435 $total = 0; 436 437 // Because __call can not keep var references so we put them into an array. 438 $total_data = array( 439 'totals' => &$totals, 440 'taxes' => &$taxes, 441 'total' => &$total 442 ); 443 444 // Display prices 445 if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 446 $sort_order = array(); 447 448 $results = $this->model_setting_extension->getExtensions('total'); 449 450 foreach ($results as $key => $value) { 451 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 452 } 453 454 array_multisort($sort_order, SORT_ASC, $results); 455 456 foreach ($results as $result) { 457 if ($this->config->get('total_' . $result['code'] . '_status')) { 458 $this->load->model('extension/total/' . $result['code']); 459 460 // We have to put the totals in an array so that they pass by reference. 461 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 462 } 463 } 464 465 $sort_order = array(); 466 467 foreach ($totals as $key => $value) { 468 $sort_order[$key] = $value['sort_order']; 469 } 470 471 array_multisort($sort_order, SORT_ASC, $totals); 472 } 473 474 $json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency'])); 475 } 476 477 $this->response->addHeader('Content-Type: application/json'); 478 $this->response->setOutput(json_encode($json)); 479 } 480 }