PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/controller/product/compare.php

https://gitlab.com/shapcy/opencart
PHP | 195 lines | 151 code | 44 blank | 0 comment | 22 complexity | 7b0bcffc4912ecfc2db0494f49196df3 MD5 | raw file
  1. <?php
  2. class ControllerProductCompare extends Controller {
  3. public function index() {
  4. $this->load->language('product/compare');
  5. $this->load->model('catalog/product');
  6. $this->load->model('tool/image');
  7. if (!isset($this->session->data['compare'])) {
  8. $this->session->data['compare'] = array();
  9. }
  10. if (isset($this->request->get['remove'])) {
  11. $key = array_search($this->request->get['remove'], $this->session->data['compare']);
  12. if ($key !== false) {
  13. unset($this->session->data['compare'][$key]);
  14. }
  15. $this->session->data['success'] = $this->language->get('text_remove');
  16. $this->response->redirect($this->url->link('product/compare'));
  17. }
  18. $this->document->setTitle($this->language->get('heading_title'));
  19. $data['breadcrumbs'] = array();
  20. $data['breadcrumbs'][] = array(
  21. 'text' => $this->language->get('text_home'),
  22. 'href' => $this->url->link('common/home')
  23. );
  24. $data['breadcrumbs'][] = array(
  25. 'text' => $this->language->get('heading_title'),
  26. 'href' => $this->url->link('product/compare')
  27. );
  28. $data['heading_title'] = $this->language->get('heading_title');
  29. $data['text_product'] = $this->language->get('text_product');
  30. $data['text_name'] = $this->language->get('text_name');
  31. $data['text_image'] = $this->language->get('text_image');
  32. $data['text_price'] = $this->language->get('text_price');
  33. $data['text_model'] = $this->language->get('text_model');
  34. $data['text_manufacturer'] = $this->language->get('text_manufacturer');
  35. $data['text_availability'] = $this->language->get('text_availability');
  36. $data['text_rating'] = $this->language->get('text_rating');
  37. $data['text_summary'] = $this->language->get('text_summary');
  38. $data['text_weight'] = $this->language->get('text_weight');
  39. $data['text_dimension'] = $this->language->get('text_dimension');
  40. $data['text_empty'] = $this->language->get('text_empty');
  41. $data['button_continue'] = $this->language->get('button_continue');
  42. $data['button_cart'] = $this->language->get('button_cart');
  43. $data['button_remove'] = $this->language->get('button_remove');
  44. if (isset($this->session->data['success'])) {
  45. $data['success'] = $this->session->data['success'];
  46. unset($this->session->data['success']);
  47. } else {
  48. $data['success'] = '';
  49. }
  50. $data['review_status'] = $this->config->get('config_review_status');
  51. $data['products'] = array();
  52. $data['attribute_groups'] = array();
  53. foreach ($this->session->data['compare'] as $key => $product_id) {
  54. $product_info = $this->model_catalog_product->getProduct($product_id);
  55. if ($product_info) {
  56. if ($product_info['image']) {
  57. $image = $this->model_tool_image->resize($product_info['image'], $this->config->get($this->config->get('config_theme') . '_image_compare_width'), $this->config->get($this->config->get('config_theme') . '_image_compare_height'));
  58. } else {
  59. $image = false;
  60. }
  61. if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
  62. $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  63. } else {
  64. $price = false;
  65. }
  66. if ((float)$product_info['special']) {
  67. $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  68. } else {
  69. $special = false;
  70. }
  71. if ($product_info['quantity'] <= 0) {
  72. $availability = $product_info['stock_status'];
  73. } elseif ($this->config->get('config_stock_display')) {
  74. $availability = $product_info['quantity'];
  75. } else {
  76. $availability = $this->language->get('text_instock');
  77. }
  78. $attribute_data = array();
  79. $attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);
  80. foreach ($attribute_groups as $attribute_group) {
  81. foreach ($attribute_group['attribute'] as $attribute) {
  82. $attribute_data[$attribute['attribute_id']] = $attribute['text'];
  83. }
  84. }
  85. $data['products'][$product_id] = array(
  86. 'product_id' => $product_info['product_id'],
  87. 'name' => $product_info['name'],
  88. 'thumb' => $image,
  89. 'price' => $price,
  90. 'special' => $special,
  91. 'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, 200) . '..',
  92. 'model' => $product_info['model'],
  93. 'manufacturer' => $product_info['manufacturer'],
  94. 'availability' => $availability,
  95. 'minimum' => $product_info['minimum'] > 0 ? $product_info['minimum'] : 1,
  96. 'rating' => (int)$product_info['rating'],
  97. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
  98. 'weight' => $this->weight->format($product_info['weight'], $product_info['weight_class_id']),
  99. 'length' => $this->length->format($product_info['length'], $product_info['length_class_id']),
  100. 'width' => $this->length->format($product_info['width'], $product_info['length_class_id']),
  101. 'height' => $this->length->format($product_info['height'], $product_info['length_class_id']),
  102. 'attribute' => $attribute_data,
  103. 'href' => $this->url->link('product/product', 'product_id=' . $product_id),
  104. 'remove' => $this->url->link('product/compare', 'remove=' . $product_id)
  105. );
  106. foreach ($attribute_groups as $attribute_group) {
  107. $data['attribute_groups'][$attribute_group['attribute_group_id']]['name'] = $attribute_group['name'];
  108. foreach ($attribute_group['attribute'] as $attribute) {
  109. $data['attribute_groups'][$attribute_group['attribute_group_id']]['attribute'][$attribute['attribute_id']]['name'] = $attribute['name'];
  110. }
  111. }
  112. } else {
  113. unset($this->session->data['compare'][$key]);
  114. }
  115. }
  116. $data['continue'] = $this->url->link('common/home');
  117. $data['column_left'] = $this->load->controller('common/column_left');
  118. $data['column_right'] = $this->load->controller('common/column_right');
  119. $data['content_top'] = $this->load->controller('common/content_top');
  120. $data['content_bottom'] = $this->load->controller('common/content_bottom');
  121. $data['footer'] = $this->load->controller('common/footer');
  122. $data['header'] = $this->load->controller('common/header');
  123. $this->response->setOutput($this->load->view('product/compare', $data));
  124. }
  125. public function add() {
  126. $this->load->language('product/compare');
  127. $json = array();
  128. if (!isset($this->session->data['compare'])) {
  129. $this->session->data['compare'] = array();
  130. }
  131. if (isset($this->request->post['product_id'])) {
  132. $product_id = $this->request->post['product_id'];
  133. } else {
  134. $product_id = 0;
  135. }
  136. $this->load->model('catalog/product');
  137. $product_info = $this->model_catalog_product->getProduct($product_id);
  138. if ($product_info) {
  139. if (!in_array($this->request->post['product_id'], $this->session->data['compare'])) {
  140. if (count($this->session->data['compare']) >= 4) {
  141. array_shift($this->session->data['compare']);
  142. }
  143. $this->session->data['compare'][] = $this->request->post['product_id'];
  144. }
  145. $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('product/compare'));
  146. $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
  147. }
  148. $this->response->addHeader('Content-Type: application/json');
  149. $this->response->setOutput(json_encode($json));
  150. }
  151. }