Advertisement
Guest User

cart.php.bk

a guest
Jul 4th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.68 KB | None | 0 0
  1. <?php
  2. class ControllerCheckoutCart extends Controller {
  3. public function index() {
  4. $this->language->load('checkout/cart');
  5.  
  6. if ($this->request->server['REQUEST_METHOD'] == 'POST') {
  7. if (isset($this->request->post['quantity'])) {
  8. if (!is_array($this->request->post['quantity'])) {
  9. if (isset($this->request->post['option'])) {
  10. $option = $this->request->post['option'];
  11. } else {
  12. $option = array();
  13. }
  14.  
  15. $this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
  16. } else {
  17. foreach ($this->request->post['quantity'] as $key => $value) {
  18. $this->cart->update($key, $value);
  19. }
  20. }
  21. }
  22.  
  23. if (isset($this->request->post['remove'])) {
  24. foreach ($this->request->post['remove'] as $key) {
  25. $this->cart->remove($key);
  26. }
  27. }
  28.  
  29. if (isset($this->request->post['voucher']) && $this->request->post['voucher']) {
  30. foreach ($this->request->post['voucher'] as $key) {
  31. if (isset($this->session->data['vouchers'][$key])) {
  32. unset($this->session->data['vouchers'][$key]);
  33. }
  34. }
  35. }
  36.  
  37. if (isset($this->request->post['redirect'])) {
  38. $this->session->data['redirect'] = $this->request->post['redirect'];
  39. }
  40.  
  41. if (isset($this->request->post['quantity']) || isset($this->request->post['remove']) || isset($this->request->post['voucher'])) {
  42. unset($this->session->data['shipping_methods']);
  43. unset($this->session->data['shipping_method']);
  44. unset($this->session->data['payment_methods']);
  45. unset($this->session->data['payment_method']);
  46. unset($this->session->data['reward']);
  47.  
  48. $this->redirect($this->url->link('checkout/cart'));
  49. }
  50. }
  51.  
  52. $this->document->setTitle($this->language->get('heading_title'));
  53.  
  54. $this->data['breadcrumbs'] = array();
  55.  
  56. $this->data['breadcrumbs'][] = array(
  57. 'href' => $this->url->link('common/home'),
  58. 'text' => $this->language->get('text_home'),
  59. 'separator' => false
  60. );
  61.  
  62. $this->data['breadcrumbs'][] = array(
  63. 'href' => $this->url->link('checkout/cart'),
  64. 'text' => $this->language->get('heading_title'),
  65. 'separator' => $this->language->get('text_separator')
  66. );
  67.  
  68. if ($this->cart->hasProducts() || (isset($this->session->data['vouchers']) && $this->session->data['vouchers'])) {
  69. $this->data['heading_title'] = $this->language->get('heading_title');
  70.  
  71. $this->data['text_select'] = $this->language->get('text_select');
  72. $this->data['text_weight'] = $this->language->get('text_weight');
  73.  
  74. $this->data['column_remove'] = $this->language->get('column_remove');
  75. $this->data['column_image'] = $this->language->get('column_image');
  76. $this->data['column_name'] = $this->language->get('column_name');
  77. $this->data['column_model'] = $this->language->get('column_model');
  78. $this->data['column_quantity'] = $this->language->get('column_quantity');
  79. $this->data['column_price'] = $this->language->get('column_price');
  80. $this->data['column_total'] = $this->language->get('column_total');
  81.  
  82. $this->data['button_update'] = $this->language->get('button_update');
  83. $this->data['button_shopping'] = $this->language->get('button_shopping');
  84. $this->data['button_checkout'] = $this->language->get('button_checkout');
  85.  
  86. if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) {
  87. $this->data['attention'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register'));
  88. } else {
  89. $this->data['attention'] = '';
  90. }
  91.  
  92. if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
  93. $this->data['error_warning'] = $this->language->get('error_stock');
  94. } elseif (isset($this->session->data['error'])) {
  95. $this->data['error_warning'] = $this->session->data['error'];
  96.  
  97. unset($this->session->data['error']);
  98. } else {
  99. $this->data['error_warning'] = '';
  100. }
  101.  
  102. if (isset($this->session->data['success'])) {
  103. $this->data['success'] = $this->session->data['success'];
  104.  
  105. unset($this->session->data['success']);
  106. } else {
  107. $this->data['success'] = '';
  108. }
  109.  
  110. $this->data['action'] = $this->url->link('checkout/cart');
  111.  
  112. if ($this->config->get('config_cart_weight')) {
  113. $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'));
  114. } else {
  115. $this->data['weight'] = false;
  116. }
  117.  
  118. $this->load->model('tool/image');
  119.  
  120. $this->data['products'] = array();
  121.  
  122. $products = $this->cart->getProducts();
  123.  
  124. foreach ($products as $product) {
  125. $product_total = 0;
  126.  
  127. foreach ($products as $product_2) {
  128. if ($product_2['product_id'] == $product['product_id']) {
  129. $product_total += $product_2['quantity'];
  130. }
  131. }
  132.  
  133. if ($product['minimum'] > $product_total) {
  134. $this->data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
  135. }
  136.  
  137. if ($product['image']) {
  138. $image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
  139. } else {
  140. $image = '';
  141. }
  142.  
  143. $option_data = array();
  144.  
  145. foreach ($product['option'] as $option) {
  146. if ($option['type'] != 'file') {
  147. $option_data[] = array(
  148. 'name' => $option['name'],
  149. 'value' => (strlen($option['option_value']) > 20 ? substr($option['option_value'], 0, 20) . '..' : $option['option_value'])
  150. );
  151. } else {
  152. $this->load->library('encryption');
  153.  
  154. $encryption = new Encryption($this->config->get('config_encryption'));
  155.  
  156. $file = substr($encryption->decrypt($option['option_value']), 0, strrpos($encryption->decrypt($option['option_value']), '.'));
  157.  
  158. $option_data[] = array(
  159. 'name' => $option['name'],
  160. 'value' => (strlen($file) > 20 ? substr($file, 0, 20) . '..' : $file)
  161. );
  162. }
  163. }
  164.  
  165. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  166. $price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
  167. } else {
  168. $price = false;
  169. }
  170.  
  171. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  172. $total = $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')));
  173. } else {
  174. $total = false;
  175. }
  176.  
  177. $this->data['products'][] = array(
  178. 'key' => $product['key'],
  179. 'thumb' => $image,
  180. 'name' => $product['name'],
  181. 'model' => $product['model'],
  182. 'option' => $option_data,
  183. 'quantity' => $product['quantity'],
  184. 'stock' => $product['stock'],
  185. 'reward' => ($product['reward'] ? sprintf($this->language->get('text_reward'), $product['reward']) : ''),
  186. 'price' => $price,
  187. 'total' => $total,
  188. 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
  189. );
  190. }
  191.  
  192. // Gift Voucher
  193. $this->data['vouchers'] = array();
  194.  
  195. if (isset($this->session->data['vouchers']) && $this->session->data['vouchers']) {
  196. foreach ($this->session->data['vouchers'] as $key => $voucher) {
  197. $this->data['vouchers'][] = array(
  198. 'key' => $key,
  199. 'description' => $voucher['description'],
  200. 'amount' => $this->currency->format($voucher['amount'])
  201. );
  202. }
  203. }
  204.  
  205. $total_data = array();
  206. $total = 0;
  207. $taxes = $this->cart->getTaxes();
  208.  
  209. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  210. $this->load->model('setting/extension');
  211.  
  212. $sort_order = array();
  213.  
  214. $results = $this->model_setting_extension->getExtensions('total');
  215.  
  216. foreach ($results as $key => $value) {
  217. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  218. }
  219.  
  220. array_multisort($sort_order, SORT_ASC, $results);
  221.  
  222. foreach ($results as $result) {
  223. if ($this->config->get($result['code'] . '_status')) {
  224. $this->load->model('total/' . $result['code']);
  225.  
  226. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  227. }
  228. }
  229.  
  230. $sort_order = array();
  231.  
  232. foreach ($total_data as $key => $value) {
  233. $sort_order[$key] = $value['sort_order'];
  234. }
  235.  
  236. array_multisort($sort_order, SORT_ASC, $total_data);
  237. }
  238.  
  239. $this->data['totals'] = $total_data;
  240.  
  241. // Modules
  242. $this->data['modules'] = array();
  243.  
  244. if (isset($results)) {
  245. foreach ($results as $result) {
  246. if ($this->config->get($result['code'] . '_status') && file_exists(DIR_APPLICATION . 'controller/total/' . $result['code'] . '.php')) {
  247. $this->data['modules'][] = $this->getChild('total/' . $result['code']);
  248. }
  249. }
  250. }
  251.  
  252. if (isset($this->session->data['redirect'])) {
  253. $this->data['continue'] = $this->session->data['redirect'];
  254.  
  255. unset($this->session->data['redirect']);
  256. } else {
  257. $this->data['continue'] = $this->url->link('common/home');
  258. }
  259.  
  260. $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
  261.  
  262. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/cart.tpl')) {
  263. $this->template = $this->config->get('config_template') . '/template/checkout/cart.tpl';
  264. } else {
  265. $this->template = 'default/template/checkout/cart.tpl';
  266. }
  267.  
  268. $this->children = array(
  269. 'common/column_left',
  270. 'common/column_right',
  271. 'common/content_top',
  272. 'common/content_bottom',
  273. 'common/footer',
  274. 'common/header'
  275. );
  276.  
  277. $this->response->setOutput($this->render());
  278. } else {
  279. $this->data['heading_title'] = $this->language->get('heading_title');
  280.  
  281. $this->data['text_error'] = $this->language->get('text_empty');
  282.  
  283. $this->data['button_continue'] = $this->language->get('button_continue');
  284.  
  285. $this->data['continue'] = $this->url->link('common/home');
  286.  
  287. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
  288. $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
  289. } else {
  290. $this->template = 'default/template/error/not_found.tpl';
  291. }
  292.  
  293. $this->children = array(
  294. 'common/column_left',
  295. 'common/column_right',
  296. 'common/content_top',
  297. 'common/content_bottom',
  298. 'common/footer',
  299. 'common/header'
  300. );
  301.  
  302. $this->response->setOutput($this->render());
  303. }
  304. }
  305.  
  306. public function update() {
  307. $this->language->load('checkout/cart');
  308.  
  309. $json = array();
  310.  
  311. if (isset($this->request->post['product_id'])) {
  312. $this->load->model('catalog/product');
  313.  
  314. $product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']);
  315.  
  316. if ($product_info) {
  317. if (isset($this->request->post['quantity'])) {
  318. $quantity = $this->request->post['quantity'];
  319. } else {
  320. $quantity = 1;
  321. }
  322.  
  323. $product_total = 0;
  324.  
  325. $products = $this->cart->getProducts();
  326.  
  327. foreach ($products as $product_2) {
  328. if ($product_2['product_id'] == $this->request->post['product_id']) {
  329. $product_total += $product_2['quantity'];
  330. }
  331. }
  332.  
  333. if ($product_info['minimum'] > ($product_total + $quantity)) {
  334. $json['error']['warning'] = sprintf($this->language->get('error_minimum'), $product_info['name'], $product_info['minimum']);
  335. }
  336.  
  337. if (isset($this->request->post['option'])) {
  338. $option = array_filter($this->request->post['option']);
  339. } else {
  340. $option = array();
  341. }
  342.  
  343. $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
  344.  
  345. foreach ($product_options as $product_option) {
  346. if ($product_option['required'] && (!isset($this->request->post['option'][$product_option['product_option_id']]) || !$this->request->post['option'][$product_option['product_option_id']])) {
  347. $json['error'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
  348. }
  349. }
  350. }
  351.  
  352. if (!isset($json['error'])) {
  353. $this->cart->add($this->request->post['product_id'], $quantity, $option);
  354.  
  355. $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'));
  356.  
  357. unset($this->session->data['shipping_methods']);
  358. unset($this->session->data['shipping_method']);
  359. unset($this->session->data['payment_methods']);
  360. unset($this->session->data['payment_method']);
  361. } else {
  362. $json['redirect'] = str_replace('&amp;', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']));
  363. }
  364. }
  365.  
  366. if (isset($this->request->post['remove'])) {
  367. $this->cart->remove($this->request->post['remove']);
  368.  
  369. unset($this->session->data['shipping_methods']);
  370. unset($this->session->data['shipping_method']);
  371. unset($this->session->data['payment_methods']);
  372. unset($this->session->data['payment_method']);
  373. }
  374.  
  375. if (isset($this->request->post['voucher'])) {
  376. if ($this->session->data['vouchers'][$this->request->post['voucher']]) {
  377. unset($this->session->data['vouchers'][$this->request->post['voucher']]);
  378. }
  379. }
  380.  
  381. $this->load->model('tool/image');
  382.  
  383. $this->data['text_empty'] = $this->language->get('text_empty');
  384.  
  385. $this->data['button_checkout'] = $this->language->get('button_checkout');
  386. $this->data['button_remove'] = $this->language->get('button_remove');
  387.  
  388. $this->data['products'] = array();
  389.  
  390. foreach ($this->cart->getProducts() as $result) {
  391. if ($result['image']) {
  392. $image = $this->model_tool_image->resize($result['image'], 40, 40);
  393. } else {
  394. $image = '';
  395. }
  396.  
  397. $option_data = array();
  398.  
  399. foreach ($result['option'] as $option) {
  400. if ($option['type'] != 'file') {
  401. $option_data[] = array(
  402. 'name' => $option['name'],
  403. 'value' => (strlen($option['option_value']) > 20 ? substr($option['option_value'], 0, 20) . '..' : $option['option_value'])
  404. );
  405. } else {
  406. $this->load->library('encryption');
  407.  
  408. $encryption = new Encryption($this->config->get('config_encryption'));
  409.  
  410. $file = substr($encryption->decrypt($option['option_value']), 0, strrpos($encryption->decrypt($option['option_value']), '.'));
  411.  
  412. $option_data[] = array(
  413. 'name' => $option['name'],
  414. 'value' => (strlen($file) > 20 ? substr($file, 0, 20) . '..' : $file)
  415. );
  416. }
  417. }
  418.  
  419. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  420. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
  421. } else {
  422. $price = false;
  423. }
  424.  
  425. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  426. $total = $this->currency->format($this->tax->calculate($result['total'], $result['tax_class_id'], $this->config->get('config_tax')));
  427. } else {
  428. $total = false;
  429. }
  430.  
  431. $this->data['products'][] = array(
  432. 'key' => $result['key'],
  433. 'product_id' => $result['product_id'],
  434. 'thumb' => $image,
  435. 'name' => $result['name'],
  436. 'model' => $result['model'],
  437. 'option' => $option_data,
  438. 'quantity' => $result['quantity'],
  439. 'stock' => $result['stock'],
  440. 'price' => $price,
  441. 'total' => $total,
  442. 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
  443. );
  444. }
  445.  
  446. // Gift Voucher
  447. $this->data['vouchers'] = array();
  448.  
  449. if (isset($this->session->data['vouchers']) && $this->session->data['vouchers']) {
  450. foreach ($this->session->data['vouchers'] as $key => $voucher) {
  451. $this->data['vouchers'][] = array(
  452. 'key' => $key,
  453. 'description' => $voucher['description'],
  454. 'amount' => $this->currency->format($voucher['amount'])
  455. );
  456. }
  457. }
  458.  
  459. // Calculate Totals
  460. $total_data = array();
  461. $total = 0;
  462. $taxes = $this->cart->getTaxes();
  463.  
  464. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  465. $this->load->model('setting/extension');
  466.  
  467. $sort_order = array();
  468.  
  469. $results = $this->model_setting_extension->getExtensions('total');
  470.  
  471. foreach ($results as $key => $value) {
  472. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  473. }
  474.  
  475. array_multisort($sort_order, SORT_ASC, $results);
  476.  
  477. foreach ($results as $result) {
  478. if ($this->config->get($result['code'] . '_status')) {
  479. $this->load->model('total/' . $result['code']);
  480.  
  481. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  482. }
  483. }
  484.  
  485. $sort_order = array();
  486.  
  487. foreach ($total_data as $key => $value) {
  488. $sort_order[$key] = $value['sort_order'];
  489. }
  490.  
  491. array_multisort($sort_order, SORT_ASC, $total_data);
  492. }
  493.  
  494. $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));
  495.  
  496. $this->data['totals'] = $total_data;
  497.  
  498. $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
  499.  
  500. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/cart.tpl')) {
  501. $this->template = $this->config->get('config_template') . '/template/common/cart.tpl';
  502. } else {
  503. $this->template = 'default/template/common/cart.tpl';
  504. }
  505.  
  506. $json['output'] = $this->render();
  507.  
  508. $this->response->setOutput(json_encode($json));
  509. }
  510. }
  511. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement