1: | <?php
2: | namespace Opencart\Catalog\Controller\Product;
3: | /**
4: | * Class Compare
5: | *
6: | * @package Opencart\Catalog\Controller\Product
7: | */
8: | class Compare extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('product/compare');
14: |
15: | if (!isset($this->session->data['compare'])) {
16: | $this->session->data['compare'] = [];
17: | }
18: |
19: | if (isset($this->request->get['remove'])) {
20: | $key = array_search($this->request->get['remove'], $this->session->data['compare']);
21: |
22: | if ($key !== false) {
23: | unset($this->session->data['compare'][$key]);
24: |
25: | $this->session->data['success'] = $this->language->get('text_remove');
26: | }
27: |
28: | $this->response->redirect($this->url->link('product/compare', 'language=' . $this->config->get('config_language'), true));
29: | }
30: |
31: | $this->document->setTitle($this->language->get('heading_title'));
32: |
33: | $data['breadcrumbs'] = [];
34: |
35: | $data['breadcrumbs'][] = [
36: | 'text' => $this->language->get('text_home'),
37: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
38: | ];
39: |
40: | $data['breadcrumbs'][] = [
41: | 'text' => $this->language->get('heading_title'),
42: | 'href' => $this->url->link('product/compare', 'language=' . $this->config->get('config_language'))
43: | ];
44: |
45: | $data['cart_add'] = $this->url->link('checkout/cart.add', 'language=' . $this->config->get('config_language'));
46: | $data['cart'] = $this->url->link('common/cart.info', 'language=' . $this->config->get('config_language'));
47: |
48: | if (isset($this->session->data['success'])) {
49: | $data['success'] = $this->session->data['success'];
50: |
51: | unset($this->session->data['success']);
52: | } else {
53: | $data['success'] = '';
54: | }
55: |
56: | $data['products'] = [];
57: |
58: | $data['attribute_groups'] = [];
59: |
60: | $this->load->model('catalog/product');
61: | $this->load->model('catalog/manufacturer');
62: | $this->load->model('localisation/stock_status');
63: | $this->load->model('tool/image');
64: |
65: | foreach ($this->session->data['compare'] as $key => $product_id) {
66: | $product_info = $this->model_catalog_product->getProduct($product_id);
67: |
68: | if ($product_info) {
69: | $description = trim(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')));
70: |
71: | if (oc_strlen($description) > $this->config->get('config_product_description_length')) {
72: | $description = oc_substr($description, 0, $this->config->get('config_product_description_length')) . '..';
73: | }
74: |
75: | if ($product_info['image'] && is_file(DIR_IMAGE . html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'))) {
76: | $image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_compare_width'), $this->config->get('config_image_compare_height'));
77: | } else {
78: | $image = '';
79: | }
80: |
81: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
82: | $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
83: | } else {
84: | $price = false;
85: | }
86: |
87: | if ((float)$product_info['special']) {
88: | $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
89: | } else {
90: | $special = false;
91: | }
92: |
93: | $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($product_info['manufacturer_id']);
94: |
95: | if ($manufacturer_info) {
96: | $manufacturer = $manufacturer_info['name'];
97: | } else {
98: | $manufacturer = '';
99: | }
100: |
101: | if ($product_info['quantity'] <= 0) {
102: | $stock_status_info = $this->model_localisation_stock_status->getStockStatus($product_info['stock_status_id']);
103: |
104: | if ($stock_status_info) {
105: | $availability = $stock_status_info['name'];
106: | } else {
107: | $availability = '';
108: | }
109: | } elseif ($this->config->get('config_stock_display')) {
110: | $availability = $product_info['quantity'];
111: | } else {
112: | $availability = $this->language->get('text_instock');
113: | }
114: |
115: | $attribute_data = [];
116: |
117: | $attribute_groups = $this->model_catalog_product->getAttributes($product_id);
118: |
119: | foreach ($attribute_groups as $attribute_group) {
120: | foreach ($attribute_group['attribute'] as $attribute) {
121: | $attribute_data[$attribute['attribute_id']] = $attribute['text'];
122: | }
123: | }
124: |
125: | $data['products'][$product_id] = [
126: | 'product_id' => $product_info['product_id'],
127: | 'name' => $product_info['name'],
128: | 'description' => $description,
129: | 'thumb' => $image,
130: | 'price' => $price,
131: | 'special' => $special,
132: | 'model' => $product_info['model'],
133: | 'manufacturer' => $manufacturer,
134: | 'availability' => $availability,
135: | 'minimum' => $product_info['minimum'] > 0 ? $product_info['minimum'] : 1,
136: | 'rating' => (int)$product_info['rating'],
137: | 'reviews' => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
138: | 'weight' => $this->weight->format($product_info['weight'], $product_info['weight_class_id'], $this->language->get('decimal_point'), $this->language->get('thousand_point')),
139: | 'length' => $this->length->format($product_info['length'], $product_info['length_class_id'], $this->language->get('decimal_point'), $this->language->get('thousand_point')),
140: | 'width' => $this->length->format($product_info['width'], $product_info['length_class_id'], $this->language->get('decimal_point'), $this->language->get('thousand_point')),
141: | 'height' => $this->length->format($product_info['height'], $product_info['length_class_id'], $this->language->get('decimal_point'), $this->language->get('thousand_point')),
142: | 'attribute' => $attribute_data,
143: | 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product_id),
144: | 'remove' => $this->url->link('product/compare', 'language=' . $this->config->get('config_language') . '&remove=' . $product_id)
145: | ];
146: |
147: | foreach ($attribute_groups as $attribute_group) {
148: | $data['attribute_groups'][$attribute_group['attribute_group_id']]['name'] = $attribute_group['name'];
149: |
150: | foreach ($attribute_group['attribute'] as $attribute) {
151: | $data['attribute_groups'][$attribute_group['attribute_group_id']]['attribute'][$attribute['attribute_id']]['name'] = $attribute['name'];
152: | }
153: | }
154: | } else {
155: | unset($this->session->data['compare'][$key]);
156: | }
157: | }
158: |
159: | $data['continue'] = $this->url->link('common/home', 'language=' . $this->config->get('config_language'));
160: |
161: | $data['column_left'] = $this->load->controller('common/column_left');
162: | $data['column_right'] = $this->load->controller('common/column_right');
163: | $data['content_top'] = $this->load->controller('common/content_top');
164: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
165: | $data['footer'] = $this->load->controller('common/footer');
166: | $data['header'] = $this->load->controller('common/header');
167: |
168: | $this->response->setOutput($this->load->view('product/compare', $data));
169: | }
170: |
171: | /**
172: | * Add
173: | *
174: | * @return void
175: | */
176: | public function add(): void {
177: | $this->load->language('product/compare');
178: |
179: | $json = [];
180: |
181: | if (!isset($this->session->data['compare'])) {
182: | $this->session->data['compare'] = [];
183: | }
184: |
185: | if (isset($this->request->post['product_id'])) {
186: | $product_id = (int)$this->request->post['product_id'];
187: | } else {
188: | $product_id = 0;
189: | }
190: |
191: | $this->load->model('catalog/product');
192: |
193: | $product_info = $this->model_catalog_product->getProduct($product_id);
194: |
195: | if (!$product_info) {
196: | $json['error'] = $this->language->get('error_product');
197: | }
198: |
199: | if (!$json) {
200: | // If already in array remove the product_id so it will be added to the back of the array
201: | $key = array_search($this->request->post['product_id'], $this->session->data['compare']);
202: |
203: | if ($key !== false) {
204: | unset($this->session->data['compare'][$key]);
205: | }
206: |
207: | // If we count a numeric value that is greater than 4 products, we remove the first one
208: | if (count($this->session->data['compare']) >= 4) {
209: | array_shift($this->session->data['compare']);
210: | }
211: |
212: | $this->session->data['compare'][] = $this->request->post['product_id'];
213: |
214: | $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('product/compare', 'language=' . $this->config->get('config_language')));
215: |
216: | $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
217: | }
218: |
219: | $this->response->addHeader('Content-Type: application/json');
220: | $this->response->setOutput(json_encode($json));
221: | }
222: | }
223: |

OpenCart API API documentation generated by ApiGen dev-master