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