/store/catalog/controller/product/compare.php

https://github.com/elleeott/WPOC-boilerplate · PHP · 197 lines · 154 code · 43 blank · 0 comment · 23 complexity · 2c097a11b32751206978e2910e7e9c0b MD5 · raw file

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