Advertisement
Guest User

cart.php

a guest
Apr 25th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.09 KB | None | 0 0
  1. <?php
  2.  
  3. class ControllerCheckoutCart extends Controller {
  4.  
  5. private $error = array();
  6.  
  7.  
  8.  
  9. public function index() {
  10.  
  11. $this->language->load('checkout/cart');
  12.  
  13.  
  14.  
  15. if (!isset($this->session->data['vouchers'])) {
  16.  
  17. $this->session->data['vouchers'] = array();
  18.  
  19. }
  20.  
  21.  
  22.  
  23. // Update
  24.  
  25. if (!empty($this->request->post['quantity'])) {
  26.  
  27. foreach ($this->request->post['quantity'] as $key => $value) {
  28.  
  29. $this->cart->update($key, $value);
  30.  
  31. }
  32.  
  33.  
  34.  
  35. unset($this->session->data['shipping_method']);
  36.  
  37. unset($this->session->data['shipping_methods']);
  38.  
  39. unset($this->session->data['payment_method']);
  40.  
  41. unset($this->session->data['payment_methods']);
  42.  
  43. unset($this->session->data['reward']);
  44.  
  45.  
  46.  
  47. $this->redirect($this->url->link('checkout/cart'));
  48.  
  49. }
  50.  
  51.  
  52.  
  53. // Remove
  54.  
  55. if (isset($this->request->get['remove'])) {
  56.  
  57. $this->cart->remove($this->request->get['remove']);
  58.  
  59.  
  60.  
  61. unset($this->session->data['vouchers'][$this->request->get['remove']]);
  62.  
  63.  
  64.  
  65. $this->session->data['success'] = $this->language->get('text_remove');
  66.  
  67.  
  68.  
  69. unset($this->session->data['shipping_method']);
  70.  
  71. unset($this->session->data['shipping_methods']);
  72.  
  73. unset($this->session->data['payment_method']);
  74.  
  75. unset($this->session->data['payment_methods']);
  76.  
  77. unset($this->session->data['reward']);
  78.  
  79.  
  80.  
  81. $this->redirect($this->url->link('checkout/cart'));
  82.  
  83. }
  84.  
  85.  
  86.  
  87. // Coupon
  88.  
  89. if (isset($this->request->post['coupon']) && $this->validateCoupon()) {
  90.  
  91. $this->session->data['coupon'] = $this->request->post['coupon'];
  92.  
  93.  
  94.  
  95. $this->session->data['success'] = $this->language->get('text_coupon');
  96.  
  97.  
  98.  
  99. $this->redirect($this->url->link('checkout/cart'));
  100.  
  101. }
  102.  
  103.  
  104.  
  105. // Voucher
  106.  
  107. if (isset($this->request->post['voucher']) && $this->validateVoucher()) {
  108.  
  109. $this->session->data['voucher'] = $this->request->post['voucher'];
  110.  
  111.  
  112.  
  113. $this->session->data['success'] = $this->language->get('text_voucher');
  114.  
  115.  
  116.  
  117. $this->redirect($this->url->link('checkout/cart'));
  118.  
  119. }
  120.  
  121.  
  122.  
  123. // Reward
  124.  
  125. if (isset($this->request->post['reward']) && $this->validateReward()) {
  126.  
  127. $this->session->data['reward'] = $this->request->post['reward'];
  128.  
  129.  
  130.  
  131. $this->session->data['success'] = $this->language->get('text_reward');
  132.  
  133.  
  134.  
  135. $this->redirect($this->url->link('checkout/cart'));
  136.  
  137. }
  138.  
  139.  
  140.  
  141. // Shipping
  142.  
  143. if (isset($this->request->post['shipping_method']) && $this->validateShipping()) {
  144.  
  145. $shipping = explode('.', $this->request->post['shipping_method']);
  146.  
  147.  
  148.  
  149. $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
  150.  
  151.  
  152.  
  153. $this->session->data['success'] = $this->language->get('text_shipping');
  154.  
  155.  
  156.  
  157. $this->redirect($this->url->link('checkout/cart'));
  158.  
  159. }
  160.  
  161.  
  162.  
  163. $this->document->setTitle($this->language->get('heading_title'));
  164.  
  165.  
  166.  
  167. $this->data['breadcrumbs'] = array();
  168.  
  169.  
  170.  
  171. $this->data['breadcrumbs'][] = array(
  172.  
  173. 'href' => $this->url->link('common/home'),
  174.  
  175. 'text' => $this->language->get('text_home'),
  176.  
  177. 'separator' => false
  178.  
  179. );
  180.  
  181.  
  182.  
  183. $this->data['breadcrumbs'][] = array(
  184.  
  185. 'href' => $this->url->link('checkout/cart'),
  186.  
  187. 'text' => $this->language->get('heading_title'),
  188.  
  189. 'separator' => $this->language->get('text_separator')
  190.  
  191. );
  192.  
  193.  
  194.  
  195. if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) {
  196.  
  197. $points = $this->customer->getRewardPoints();
  198.  
  199.  
  200.  
  201. $points_total = 0;
  202.  
  203.  
  204.  
  205. foreach ($this->cart->getProducts() as $product) {
  206.  
  207. if ($product['points']) {
  208.  
  209. $points_total += $product['points'];
  210.  
  211. }
  212.  
  213. }
  214.  
  215.  
  216.  
  217. $this->data['heading_title'] = $this->language->get('heading_title');
  218.  
  219.  
  220.  
  221. $this->data['text_next'] = $this->language->get('text_next');
  222.  
  223. $this->data['text_next_choice'] = $this->language->get('text_next_choice');
  224.  
  225. $this->data['text_use_coupon'] = $this->language->get('text_use_coupon');
  226.  
  227. $this->data['text_use_voucher'] = $this->language->get('text_use_voucher');
  228.  
  229. $this->data['text_use_reward'] = sprintf($this->language->get('text_use_reward'), $points);
  230.  
  231. $this->data['text_shipping_estimate'] = $this->language->get('text_shipping_estimate');
  232.  
  233. $this->data['text_shipping_detail'] = $this->language->get('text_shipping_detail');
  234.  
  235. $this->data['text_shipping_method'] = $this->language->get('text_shipping_method');
  236.  
  237. $this->data['text_select'] = $this->language->get('text_select');
  238.  
  239.  
  240.  
  241. $this->data['column_image'] = $this->language->get('column_image');
  242.  
  243. $this->data['column_name'] = $this->language->get('column_name');
  244.  
  245. $this->data['column_model'] = $this->language->get('column_model');
  246.  
  247. $this->data['column_quantity'] = $this->language->get('column_quantity');
  248.  
  249. $this->data['column_price'] = $this->language->get('column_price');
  250.  
  251. $this->data['column_total'] = $this->language->get('column_total');
  252.  
  253.  
  254.  
  255. $this->data['entry_coupon'] = $this->language->get('entry_coupon');
  256.  
  257. $this->data['entry_voucher'] = $this->language->get('entry_voucher');
  258.  
  259. $this->data['entry_reward'] = sprintf($this->language->get('entry_reward'), $points_total);
  260.  
  261. $this->data['entry_country'] = $this->language->get('entry_country');
  262.  
  263. $this->data['entry_zone'] = $this->language->get('entry_zone');
  264.  
  265. $this->data['entry_postcode'] = $this->language->get('entry_postcode');
  266.  
  267.  
  268.  
  269. $this->data['button_update'] = $this->language->get('button_update');
  270.  
  271. $this->data['button_remove'] = $this->language->get('button_remove');
  272.  
  273. $this->data['button_coupon'] = $this->language->get('button_coupon');
  274.  
  275. $this->data['button_voucher'] = $this->language->get('button_voucher');
  276.  
  277. $this->data['button_reward'] = $this->language->get('button_reward');
  278.  
  279. $this->data['button_quote'] = $this->language->get('button_quote');
  280.  
  281. $this->data['button_shipping'] = $this->language->get('button_shipping');
  282.  
  283. $this->data['button_shopping'] = $this->language->get('button_shopping');
  284.  
  285. $this->data['button_checkout'] = $this->language->get('button_checkout');
  286.  
  287.  
  288.  
  289. if (isset($this->error['warning'])) {
  290.  
  291. $this->data['error_warning'] = $this->error['warning'];
  292.  
  293. } elseif (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
  294.  
  295. $this->data['error_warning'] = $this->language->get('error_stock');
  296.  
  297. } else {
  298.  
  299. $this->data['error_warning'] = '';
  300.  
  301. }
  302.  
  303.  
  304.  
  305. if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) {
  306.  
  307. $this->data['attention'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register'));
  308.  
  309. } else {
  310.  
  311. $this->data['attention'] = '';
  312.  
  313. }
  314.  
  315.  
  316.  
  317. if (isset($this->session->data['success'])) {
  318.  
  319. $this->data['success'] = $this->session->data['success'];
  320.  
  321.  
  322.  
  323. unset($this->session->data['success']);
  324.  
  325. } else {
  326.  
  327. $this->data['success'] = '';
  328.  
  329. }
  330.  
  331.  
  332.  
  333. $this->data['action'] = $this->url->link('checkout/cart');
  334.  
  335.  
  336.  
  337. if ($this->config->get('config_cart_weight')) {
  338.  
  339. $this->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'));
  340.  
  341. } else {
  342.  
  343. $this->data['weight'] = '';
  344.  
  345. }
  346.  
  347.  
  348.  
  349. $this->load->model('tool/image');
  350.  
  351.  
  352.  
  353. $this->data['products'] = array();
  354.  
  355.  
  356.  
  357. $products = $this->cart->getProducts();
  358.  
  359.  
  360.  
  361. foreach ($products as $product) {
  362.  
  363. $product_total = 0;
  364.  
  365.  
  366.  
  367. foreach ($products as $product_2) {
  368.  
  369. if ($product_2['product_id'] == $product['product_id']) {
  370.  
  371. $product_total += $product_2['quantity'];
  372.  
  373. }
  374.  
  375. }
  376.  
  377.  
  378.  
  379. if ($product['minimum'] > $product_total) {
  380.  
  381. $this->data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
  382.  
  383. }
  384.  
  385.  
  386.  
  387. if ($product['image']) {
  388.  
  389. $image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
  390.  
  391. } else {
  392.  
  393. $image = '';
  394.  
  395. }
  396.  
  397.  
  398.  
  399. $option_data = array();
  400.  
  401.  
  402.  
  403. foreach ($product['option'] as $option) {
  404.  
  405. if ($option['type'] != 'file') {
  406.  
  407. $value = $option['option_value'];
  408.  
  409. } else {
  410.  
  411. $filename = $this->encryption->decrypt($option['option_value']);
  412.  
  413.  
  414.  
  415. $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
  416.  
  417. }
  418.  
  419.  
  420.  
  421. $option_data[] = array(
  422.  
  423. 'name' => $option['name'],
  424.  
  425. 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
  426.  
  427. );
  428.  
  429. }
  430.  
  431.  
  432.  
  433. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  434.  
  435. $price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
  436.  
  437. } else {
  438.  
  439. $price = false;
  440.  
  441. }
  442.  
  443.  
  444.  
  445. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  446.  
  447. $total = $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')));
  448.  
  449. } else {
  450.  
  451. $total = false;
  452.  
  453. }
  454.  
  455.  
  456.  
  457. $this->data['products'][] = array(
  458.  
  459. 'key' => $product['key'],
  460.  
  461. 'thumb' => $image,
  462.  
  463. 'name' => $product['name'],
  464.  
  465. 'model' => $product['model'],
  466.  
  467. 'option' => $option_data,
  468.  
  469. 'quantity' => $product['quantity'],
  470.  
  471. 'stock' => $product['stock'],
  472.  
  473. 'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
  474.  
  475. 'price' => $price,
  476.  
  477. 'total' => $total,
  478.  
  479. 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']),
  480.  
  481. 'remove' => $this->url->link('checkout/cart', 'remove=' . $product['key'])
  482.  
  483. );
  484.  
  485. }
  486.  
  487.  
  488.  
  489. // Gift Voucher
  490.  
  491. $this->data['vouchers'] = array();
  492.  
  493.  
  494.  
  495. if (!empty($this->session->data['vouchers'])) {
  496.  
  497. foreach ($this->session->data['vouchers'] as $key => $voucher) {
  498.  
  499. $this->data['vouchers'][] = array(
  500.  
  501. 'key' => $key,
  502.  
  503. 'description' => $voucher['description'],
  504.  
  505. 'amount' => $this->currency->format($voucher['amount']),
  506.  
  507. 'remove' => $this->url->link('checkout/cart', 'remove=' . $key)
  508.  
  509. );
  510.  
  511. }
  512.  
  513. }
  514.  
  515.  
  516.  
  517. if (isset($this->request->post['next'])) {
  518.  
  519. $this->data['next'] = $this->request->post['next'];
  520.  
  521. } else {
  522.  
  523. $this->data['next'] = '';
  524.  
  525. }
  526.  
  527.  
  528.  
  529. $this->data['coupon_status'] = $this->config->get('coupon_status');
  530.  
  531.  
  532.  
  533. if (isset($this->request->post['coupon'])) {
  534.  
  535. $this->data['coupon'] = $this->request->post['coupon'];
  536.  
  537. } elseif (isset($this->session->data['coupon'])) {
  538.  
  539. $this->data['coupon'] = $this->session->data['coupon'];
  540.  
  541. } else {
  542.  
  543. $this->data['coupon'] = '';
  544.  
  545. }
  546.  
  547.  
  548.  
  549. $this->data['voucher_status'] = $this->config->get('voucher_status');
  550.  
  551.  
  552.  
  553. if (isset($this->request->post['voucher'])) {
  554.  
  555. $this->data['voucher'] = $this->request->post['voucher'];
  556.  
  557. } elseif (isset($this->session->data['voucher'])) {
  558.  
  559. $this->data['voucher'] = $this->session->data['voucher'];
  560.  
  561. } else {
  562.  
  563. $this->data['voucher'] = '';
  564.  
  565. }
  566.  
  567.  
  568.  
  569. $this->data['reward_status'] = ($points && $points_total && $this->config->get('reward_status'));
  570.  
  571.  
  572.  
  573. if (isset($this->request->post['reward'])) {
  574.  
  575. $this->data['reward'] = $this->request->post['reward'];
  576.  
  577. } elseif (isset($this->session->data['reward'])) {
  578.  
  579. $this->data['reward'] = $this->session->data['reward'];
  580.  
  581. } else {
  582.  
  583. $this->data['reward'] = '';
  584.  
  585. }
  586.  
  587.  
  588.  
  589. $this->data['shipping_status'] = $this->config->get('shipping_status') && $this->cart->hasShipping();
  590.  
  591.  
  592.  
  593. if (isset($this->request->post['country_id'])) {
  594.  
  595. $this->data['country_id'] = $this->request->post['country_id'];
  596.  
  597. } elseif (isset($this->session->data['guest']['shipping']['country_id'])) {
  598.  
  599. $this->data['country_id'] = $this->session->data['guest']['shipping']['country_id'];
  600.  
  601. } else {
  602.  
  603. $this->data['country_id'] = $this->config->get('config_country_id');
  604.  
  605. }
  606.  
  607.  
  608.  
  609. $this->load->model('localisation/country');
  610.  
  611.  
  612.  
  613. $this->data['countries'] = $this->model_localisation_country->getCountries();
  614.  
  615.  
  616.  
  617. if (isset($this->request->post['zone_id'])) {
  618.  
  619. $this->data['zone_id'] = $this->request->post['zone_id'];
  620.  
  621. } elseif (isset($this->session->data['guest']['shipping']['zone_id'])) {
  622.  
  623. $this->data['zone_id'] = $this->session->data['guest']['shipping']['zone_id'];
  624.  
  625. } else {
  626.  
  627. $this->data['zone_id'] = '';
  628.  
  629. }
  630.  
  631.  
  632.  
  633. if (isset($this->request->post['postcode'])) {
  634.  
  635. $this->data['postcode'] = $this->request->post['postcode'];
  636.  
  637. } elseif (isset($this->session->data['guest']['shipping']['postcode'])) {
  638.  
  639. $this->data['postcode'] = $this->session->data['guest']['shipping']['postcode'];
  640.  
  641. } else {
  642.  
  643. $this->data['postcode'] = '';
  644.  
  645. }
  646.  
  647.  
  648.  
  649. if (isset($this->request->post['shipping_method'])) {
  650.  
  651. $this->data['shipping_method'] = $this->request->post['shipping_method'];
  652.  
  653. } elseif (isset($this->session->data['shipping_method'])) {
  654.  
  655. $this->data['shipping_method'] = $this->session->data['shipping_method']['code'];
  656.  
  657. } else {
  658.  
  659. $this->data['shipping_method'] = '';
  660.  
  661. }
  662.  
  663.  
  664.  
  665. // Totals
  666.  
  667. $this->load->model('setting/extension');
  668.  
  669.  
  670.  
  671. $total_data = array();
  672.  
  673. $total = 0;
  674.  
  675. $taxes = $this->cart->getTaxes();
  676.  
  677.  
  678.  
  679. $sort_order = array();
  680.  
  681.  
  682.  
  683. $results = $this->model_setting_extension->getExtensions('total');
  684.  
  685.  
  686.  
  687. foreach ($results as $key => $value) {
  688.  
  689. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  690.  
  691. }
  692.  
  693.  
  694.  
  695. array_multisort($sort_order, SORT_ASC, $results);
  696.  
  697.  
  698.  
  699. foreach ($results as $result) {
  700.  
  701. if ($this->config->get($result['code'] . '_status')) {
  702.  
  703. $this->load->model('total/' . $result['code']);
  704.  
  705.  
  706.  
  707. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  708.  
  709. }
  710.  
  711.  
  712.  
  713. $sort_order = array();
  714.  
  715.  
  716.  
  717. foreach ($total_data as $key => $value) {
  718.  
  719. $sort_order[$key] = $value['sort_order'];
  720.  
  721. }
  722.  
  723.  
  724.  
  725. array_multisort($sort_order, SORT_ASC, $total_data);
  726.  
  727. }
  728.  
  729.  
  730.  
  731. $this->data['totals'] = $total_data;
  732.  
  733.  
  734.  
  735. $this->data['continue'] = $this->url->link('common/home');
  736.  
  737.  
  738.  
  739. $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
  740.  
  741.  
  742.  
  743. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/cart.tpl')) {
  744.  
  745. $this->template = $this->config->get('config_template') . '/template/checkout/cart.tpl';
  746.  
  747. } else {
  748.  
  749. $this->template = 'default/template/checkout/cart.tpl';
  750.  
  751. }
  752.  
  753.  
  754.  
  755. $this->children = array(
  756.  
  757. 'common/column_left',
  758.  
  759. 'common/column_right',
  760.  
  761. 'common/content_bottom',
  762.  
  763. 'common/content_top',
  764.  
  765. 'common/footer',
  766.  
  767. 'common/header'
  768.  
  769. );
  770.  
  771.  
  772.  
  773. $this->response->setOutput($this->render());
  774.  
  775. } else {
  776.  
  777. $this->data['heading_title'] = $this->language->get('heading_title');
  778.  
  779.  
  780.  
  781. $this->data['text_error'] = $this->language->get('text_empty');
  782.  
  783.  
  784.  
  785. $this->data['button_continue'] = $this->language->get('button_continue');
  786.  
  787.  
  788.  
  789. $this->data['continue'] = $this->url->link('common/home');
  790.  
  791.  
  792.  
  793. unset($this->session->data['success']);
  794.  
  795.  
  796.  
  797. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
  798.  
  799. $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
  800.  
  801. } else {
  802.  
  803. $this->template = 'default/template/error/not_found.tpl';
  804.  
  805. }
  806.  
  807.  
  808.  
  809. $this->children = array(
  810.  
  811. 'common/column_left',
  812.  
  813. 'common/column_right',
  814.  
  815. 'common/content_top',
  816.  
  817. 'common/content_bottom',
  818.  
  819. 'common/footer',
  820.  
  821. 'common/header'
  822.  
  823. );
  824.  
  825.  
  826.  
  827. $this->response->setOutput($this->render());
  828.  
  829. }
  830.  
  831. }
  832.  
  833.  
  834.  
  835. private function validateCoupon() {
  836.  
  837. $this->load->model('checkout/coupon');
  838.  
  839.  
  840.  
  841. $coupon_info = $this->model_checkout_coupon->getCoupon($this->request->post['coupon']);
  842.  
  843.  
  844.  
  845. if (!$coupon_info) {
  846.  
  847. $this->error['warning'] = $this->language->get('error_coupon');
  848.  
  849. }
  850.  
  851.  
  852.  
  853. if (!$this->error) {
  854.  
  855. return true;
  856.  
  857. } else {
  858.  
  859. return false;
  860.  
  861. }
  862.  
  863. }
  864.  
  865.  
  866.  
  867. private function validateVoucher() {
  868.  
  869. $this->load->model('checkout/voucher');
  870.  
  871.  
  872.  
  873. $voucher_info = $this->model_checkout_voucher->getVoucher($this->request->post['voucher']);
  874.  
  875.  
  876.  
  877. if (!$voucher_info) {
  878.  
  879. $this->error['warning'] = $this->language->get('error_voucher');
  880.  
  881. }
  882.  
  883.  
  884.  
  885. if (!$this->error) {
  886.  
  887. return true;
  888.  
  889. } else {
  890.  
  891. return false;
  892.  
  893. }
  894.  
  895. }
  896.  
  897.  
  898.  
  899. private function validateReward() {
  900.  
  901. $points = $this->customer->getRewardPoints();
  902.  
  903.  
  904.  
  905. $points_total = 0;
  906.  
  907.  
  908.  
  909. foreach ($this->cart->getProducts() as $product) {
  910.  
  911. if ($product['points']) {
  912.  
  913. $points_total += $product['points'];
  914.  
  915. }
  916.  
  917. }
  918.  
  919.  
  920.  
  921. if (empty($this->request->post['reward'])) {
  922.  
  923. $this->error['warning'] = $this->language->get('error_reward');
  924.  
  925. }
  926.  
  927.  
  928.  
  929. if ($this->request->post['reward'] > $points) {
  930.  
  931. $this->error['warning'] = sprintf($this->language->get('error_points'), $this->request->post['reward']);
  932.  
  933. }
  934.  
  935.  
  936.  
  937. if ($this->request->post['reward'] > $points_total) {
  938.  
  939. $this->error['warning'] = sprintf($this->language->get('error_maximum'), $points_total);
  940.  
  941. }
  942.  
  943.  
  944.  
  945. if (!$this->error) {
  946.  
  947. return true;
  948.  
  949. } else {
  950.  
  951. return false;
  952.  
  953. }
  954.  
  955. }
  956.  
  957.  
  958.  
  959. private function validateShipping() {
  960.  
  961. if (!empty($this->request->post['shipping_method'])) {
  962.  
  963. $shipping = explode('.', $this->request->post['shipping_method']);
  964.  
  965.  
  966.  
  967. if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
  968.  
  969. $this->error['warning'] = $this->language->get('error_shipping');
  970.  
  971. }
  972.  
  973. } else {
  974.  
  975. $this->error['warning'] = $this->language->get('error_shipping');
  976.  
  977. }
  978.  
  979.  
  980.  
  981. if (!$this->error) {
  982.  
  983. return true;
  984.  
  985. } else {
  986.  
  987. return false;
  988.  
  989. }
  990.  
  991. }
  992.  
  993.  
  994.  
  995. public function add() {
  996.  
  997. $this->language->load('checkout/cart');
  998.  
  999.  
  1000.  
  1001. $json = array();
  1002.  
  1003.  
  1004.  
  1005. if (isset($this->request->post['product_id'])) {
  1006.  
  1007. $product_id = $this->request->post['product_id'];
  1008.  
  1009. } else {
  1010.  
  1011. $product_id = 0;
  1012.  
  1013. }
  1014.  
  1015.  
  1016.  
  1017. $this->load->model('catalog/product');
  1018.  
  1019.  
  1020.  
  1021. $product_info = $this->model_catalog_product->getProduct($product_id);
  1022.  
  1023.  
  1024.  
  1025. if ($product_info) {
  1026.  
  1027. if (isset($this->request->post['quantity'])) {
  1028.  
  1029. $quantity = $this->request->post['quantity'];
  1030.  
  1031. } else {
  1032.  
  1033. $quantity = 1;
  1034.  
  1035. }
  1036.  
  1037.  
  1038.  
  1039. if (isset($this->request->post['option'])) {
  1040.  
  1041. $option = array_filter($this->request->post['option']);
  1042.  
  1043. } else {
  1044.  
  1045. $option = array();
  1046.  
  1047. }
  1048.  
  1049.  
  1050.  
  1051. $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
  1052.  
  1053.  
  1054.  
  1055. foreach ($product_options as $product_option) {
  1056.  
  1057. if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
  1058.  
  1059. $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
  1060.  
  1061. }
  1062.  
  1063. }
  1064.  
  1065.  
  1066.  
  1067. if (!$json) {
  1068.  
  1069. $this->cart->add($this->request->post['product_id'], $quantity, $option);
  1070.  
  1071.  
  1072.  
  1073. $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'));
  1074.  
  1075.  
  1076.  
  1077. unset($this->session->data['shipping_method']);
  1078.  
  1079. unset($this->session->data['shipping_methods']);
  1080.  
  1081. unset($this->session->data['payment_method']);
  1082.  
  1083. unset($this->session->data['payment_methods']);
  1084.  
  1085.  
  1086.  
  1087. // Totals
  1088.  
  1089. $this->load->model('setting/extension');
  1090.  
  1091.  
  1092.  
  1093. $total_data = array();
  1094.  
  1095. $total = 0;
  1096.  
  1097. $taxes = $this->cart->getTaxes();
  1098.  
  1099.  
  1100.  
  1101. $sort_order = array();
  1102.  
  1103.  
  1104.  
  1105. $results = $this->model_setting_extension->getExtensions('total');
  1106.  
  1107.  
  1108.  
  1109. foreach ($results as $key => $value) {
  1110.  
  1111. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  1112.  
  1113. }
  1114.  
  1115.  
  1116.  
  1117. array_multisort($sort_order, SORT_ASC, $results);
  1118.  
  1119.  
  1120.  
  1121. foreach ($results as $result) {
  1122.  
  1123. if ($this->config->get($result['code'] . '_status')) {
  1124.  
  1125. $this->load->model('total/' . $result['code']);
  1126.  
  1127.  
  1128.  
  1129. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  1130.  
  1131. }
  1132.  
  1133.  
  1134.  
  1135. $sort_order = array();
  1136.  
  1137.  
  1138.  
  1139. foreach ($total_data as $key => $value) {
  1140.  
  1141. $sort_order[$key] = $value['sort_order'];
  1142.  
  1143. }
  1144.  
  1145.  
  1146.  
  1147. array_multisort($sort_order, SORT_ASC, $total_data);
  1148.  
  1149. }
  1150.  
  1151.  
  1152.  
  1153. $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));
  1154.  
  1155. } else {
  1156.  
  1157. $json['redirect'] = str_replace('&amp;', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']));
  1158.  
  1159. }
  1160.  
  1161. }
  1162.  
  1163.  
  1164.  
  1165. $this->response->setOutput(json_encode($json));
  1166.  
  1167. }
  1168.  
  1169.  
  1170.  
  1171. public function quote() {
  1172.  
  1173. $this->language->load('checkout/cart');
  1174.  
  1175.  
  1176.  
  1177. $json = array();
  1178.  
  1179.  
  1180.  
  1181. if (!$this->cart->hasProducts()) {
  1182.  
  1183. $json['error']['warning'] = $this->language->get('error_product');
  1184.  
  1185. }
  1186.  
  1187.  
  1188.  
  1189. if (!$this->cart->hasShipping()) {
  1190.  
  1191. $json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
  1192.  
  1193. }
  1194.  
  1195.  
  1196.  
  1197. if ($this->request->post['country_id'] == '') {
  1198.  
  1199. $json['error']['country'] = $this->language->get('error_country');
  1200.  
  1201. }
  1202.  
  1203.  
  1204.  
  1205. if ($this->request->post['zone_id'] == '') {
  1206.  
  1207. $json['error']['zone'] = $this->language->get('error_zone');
  1208.  
  1209. }
  1210.  
  1211.  
  1212.  
  1213. $this->load->model('localisation/country');
  1214.  
  1215.  
  1216.  
  1217. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  1218.  
  1219.  
  1220.  
  1221. if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
  1222.  
  1223. $json['error']['postcode'] = $this->language->get('error_postcode');
  1224.  
  1225. }
  1226.  
  1227.  
  1228.  
  1229. if (!$json) {
  1230.  
  1231. $this->tax->setShippingAddress($this->request->post['country_id'], $this->request->post['zone_id']);
  1232.  
  1233.  
  1234.  
  1235. $this->session->data['guest']['shipping']['country_id'] = $this->request->post['country_id'];
  1236.  
  1237. $this->session->data['guest']['shipping']['zone_id'] = $this->request->post['zone_id'];
  1238.  
  1239. $this->session->data['guest']['shipping']['postcode'] = $this->request->post['postcode'];
  1240.  
  1241.  
  1242.  
  1243. if ($country_info) {
  1244.  
  1245. $country = $country_info['name'];
  1246.  
  1247. $iso_code_2 = $country_info['iso_code_2'];
  1248.  
  1249. $iso_code_3 = $country_info['iso_code_3'];
  1250.  
  1251. $address_format = $country_info['address_format'];
  1252.  
  1253. } else {
  1254.  
  1255. $country = '';
  1256.  
  1257. $iso_code_2 = '';
  1258.  
  1259. $iso_code_3 = '';
  1260.  
  1261. $address_format = '';
  1262.  
  1263. }
  1264.  
  1265.  
  1266.  
  1267. $this->load->model('localisation/zone');
  1268.  
  1269.  
  1270.  
  1271. $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
  1272.  
  1273.  
  1274.  
  1275. if ($zone_info) {
  1276.  
  1277. $zone = $zone_info['name'];
  1278.  
  1279. $code = $zone_info['code'];
  1280.  
  1281. } else {
  1282.  
  1283. $zone = '';
  1284.  
  1285. $code = '';
  1286.  
  1287. }
  1288.  
  1289.  
  1290.  
  1291. $address_data = array(
  1292.  
  1293. 'firstname' => '',
  1294.  
  1295. 'lastname' => '',
  1296.  
  1297. 'company' => '',
  1298.  
  1299. 'address_1' => '',
  1300.  
  1301. 'address_2' => '',
  1302.  
  1303. 'postcode' => $this->request->post['postcode'],
  1304.  
  1305. 'city' => '',
  1306.  
  1307. 'zone_id' => $this->request->post['zone_id'],
  1308.  
  1309. 'zone' => $zone,
  1310.  
  1311. 'zone_code' => $code,
  1312.  
  1313. 'country_id' => $this->request->post['country_id'],
  1314.  
  1315. 'country' => $country,
  1316.  
  1317. 'iso_code_2' => $iso_code_2,
  1318.  
  1319. 'iso_code_3' => $iso_code_3,
  1320.  
  1321. 'address_format' => $address_format
  1322.  
  1323. );
  1324.  
  1325.  
  1326.  
  1327. $quote_data = array();
  1328.  
  1329.  
  1330.  
  1331. $this->load->model('setting/extension');
  1332.  
  1333.  
  1334.  
  1335. $results = $this->model_setting_extension->getExtensions('shipping');
  1336.  
  1337.  
  1338.  
  1339. foreach ($results as $result) {
  1340.  
  1341. if ($this->config->get($result['code'] . '_status')) {
  1342.  
  1343. $this->load->model('shipping/' . $result['code']);
  1344.  
  1345.  
  1346.  
  1347. $quote = $this->{'model_shipping_' . $result['code']}->getQuote($address_data);
  1348.  
  1349.  
  1350.  
  1351. if ($quote) {
  1352.  
  1353. $quote_data[$result['code']] = array(
  1354.  
  1355. 'title' => $quote['title'],
  1356.  
  1357. 'quote' => $quote['quote'],
  1358.  
  1359. 'sort_order' => $quote['sort_order'],
  1360.  
  1361. 'error' => $quote['error']
  1362.  
  1363. );
  1364.  
  1365. }
  1366.  
  1367. }
  1368.  
  1369. }
  1370.  
  1371.  
  1372.  
  1373. $sort_order = array();
  1374.  
  1375.  
  1376.  
  1377. foreach ($quote_data as $key => $value) {
  1378.  
  1379. $sort_order[$key] = $value['sort_order'];
  1380.  
  1381. }
  1382.  
  1383.  
  1384.  
  1385. array_multisort($sort_order, SORT_ASC, $quote_data);
  1386.  
  1387.  
  1388.  
  1389. $this->session->data['shipping_methods'] = $quote_data;
  1390.  
  1391.  
  1392.  
  1393. if ($this->session->data['shipping_methods']) {
  1394.  
  1395. $json['shipping_method'] = $this->session->data['shipping_methods'];
  1396.  
  1397. } else {
  1398.  
  1399. $json['error']['warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
  1400.  
  1401. }
  1402.  
  1403. }
  1404.  
  1405.  
  1406.  
  1407. $this->response->setOutput(json_encode($json));
  1408.  
  1409. }
  1410.  
  1411.  
  1412.  
  1413. public function zone() {
  1414.  
  1415. $output = '<option value="">' . $this->language->get('text_select') . '</option>';
  1416.  
  1417.  
  1418.  
  1419. $this->load->model('localisation/zone');
  1420.  
  1421.  
  1422.  
  1423. $results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
  1424.  
  1425.  
  1426.  
  1427. foreach ($results as $result) {
  1428.  
  1429. $output .= '<option value="' . $result['zone_id'] . '"';
  1430.  
  1431.  
  1432.  
  1433. if (isset($this->request->get['zone_id']) && ($this->request->get['zone_id'] == $result['zone_id'])) {
  1434.  
  1435. $output .= ' selected="selected"';
  1436.  
  1437. }
  1438.  
  1439.  
  1440.  
  1441. $output .= '>' . $result['name'] . '</option>';
  1442.  
  1443. }
  1444.  
  1445.  
  1446.  
  1447. if (!$results) {
  1448.  
  1449. $output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
  1450.  
  1451. }
  1452.  
  1453.  
  1454.  
  1455. $this->response->setOutput($output);
  1456.  
  1457. }
  1458.  
  1459. }
  1460.  
  1461. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement