1: | <?php
|
2: | namespace Opencart\Catalog\Controller\Account;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class WishList extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: |
|
12: | public function index(): void {
|
13: | $this->load->language('account/wishlist');
|
14: |
|
15: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
16: | $this->session->data['redirect'] = $this->url->link('account/wishlist', 'language=' . $this->config->get('config_language'));
|
17: |
|
18: | $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
|
19: | }
|
20: |
|
21: | $this->document->setTitle($this->language->get('heading_title'));
|
22: |
|
23: | $data['breadcrumbs'] = [];
|
24: |
|
25: | $data['breadcrumbs'][] = [
|
26: | 'text' => $this->language->get('text_home'),
|
27: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
|
28: | ];
|
29: |
|
30: | $data['breadcrumbs'][] = [
|
31: | 'text' => $this->language->get('text_account'),
|
32: | 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''))
|
33: | ];
|
34: |
|
35: | $data['breadcrumbs'][] = [
|
36: | 'text' => $this->language->get('heading_title'),
|
37: | 'href' => $this->url->link('account/wishlist', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''))
|
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['list'] = $this->load->controller('account/wishlist.getList');
|
49: |
|
50: | $data['continue'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''));
|
51: |
|
52: | $data['column_left'] = $this->load->controller('common/column_left');
|
53: | $data['column_right'] = $this->load->controller('common/column_right');
|
54: | $data['content_top'] = $this->load->controller('common/content_top');
|
55: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
|
56: | $data['footer'] = $this->load->controller('common/footer');
|
57: | $data['header'] = $this->load->controller('common/header');
|
58: |
|
59: | $this->response->setOutput($this->load->view('account/wishlist', $data));
|
60: | }
|
61: |
|
62: | |
63: | |
64: | |
65: | |
66: |
|
67: | public function list(): void {
|
68: | $this->load->language('account/wishlist');
|
69: |
|
70: | $this->response->setOutput($this->getList());
|
71: | }
|
72: |
|
73: | |
74: | |
75: | |
76: | |
77: |
|
78: | public function getList(): string {
|
79: | $data['wishlist'] = $this->url->link('account/wishlist.list', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''));
|
80: | $data['cart_add'] = $this->url->link('checkout/cart.add', 'language=' . $this->config->get('config_language'));
|
81: | $data['remove'] = $this->url->link('account/wishlist.remove', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''));
|
82: |
|
83: | $data['products'] = [];
|
84: |
|
85: | $this->load->model('account/wishlist');
|
86: | $this->load->model('catalog/product');
|
87: | $this->load->model('tool/image');
|
88: |
|
89: | $results = $this->model_account_wishlist->getWishlist($this->customer->getId());
|
90: |
|
91: | foreach ($results as $result) {
|
92: | $product_info = $this->model_catalog_product->getProduct($result['product_id']);
|
93: |
|
94: | if ($product_info) {
|
95: | if ($product_info['image'] && is_file(DIR_IMAGE . html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'))) {
|
96: | $image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_wishlist_width'), $this->config->get('config_image_wishlist_height'));
|
97: | } else {
|
98: | $image = '';
|
99: | }
|
100: |
|
101: | if ($product_info['quantity'] <= 0) {
|
102: | $this->load->model('localisation/stock_status');
|
103: |
|
104: | $stock_status_info = $this->model_localisation_stock_status->getStockStatus($product_info['stock_status_id']);
|
105: |
|
106: | if ($stock_status_info) {
|
107: | $stock = $stock_status_info['name'];
|
108: | } else {
|
109: | $stock = '';
|
110: | }
|
111: | } elseif ($this->config->get('config_stock_display')) {
|
112: | $stock = $product_info['quantity'];
|
113: | } else {
|
114: | $stock = $this->language->get('text_instock');
|
115: | }
|
116: |
|
117: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
|
118: | $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
|
119: | } else {
|
120: | $price = false;
|
121: | }
|
122: |
|
123: | if ((float)$product_info['special']) {
|
124: | $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
|
125: | } else {
|
126: | $special = false;
|
127: | }
|
128: |
|
129: | $data['products'][] = [
|
130: | 'product_id' => $product_info['product_id'],
|
131: | 'thumb' => $image,
|
132: | 'name' => $product_info['name'],
|
133: | 'model' => $product_info['model'],
|
134: | 'stock' => $stock,
|
135: | 'price' => $price,
|
136: | 'special' => $special,
|
137: | 'minimum' => $product_info['minimum'] > 0 ? $product_info['minimum'] : 1,
|
138: | 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product_info['product_id']),
|
139: | 'remove' => $this->url->link('account/wishlist.remove', 'language=' . $this->config->get('config_language') . '&product_id=' . $product_info['product_id'] . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''))
|
140: | ];
|
141: | } else {
|
142: | $this->model_account_wishlist->deleteWishlist($this->customer->getId(), $result['product_id']);
|
143: | }
|
144: | }
|
145: |
|
146: | return $this->load->view('account/wishlist_list', $data);
|
147: | }
|
148: |
|
149: | |
150: | |
151: | |
152: | |
153: |
|
154: | public function add(): void {
|
155: | $this->load->language('account/wishlist');
|
156: |
|
157: | $json = [];
|
158: |
|
159: | if (isset($this->request->post['product_id'])) {
|
160: | $product_id = (int)$this->request->post['product_id'];
|
161: | } else {
|
162: | $product_id = 0;
|
163: | }
|
164: |
|
165: | $this->load->model('catalog/product');
|
166: |
|
167: | $product_info = $this->model_catalog_product->getProduct($product_id);
|
168: |
|
169: | if (!$product_info) {
|
170: | $json['error'] = $this->language->get('error_product');
|
171: | }
|
172: |
|
173: | if (!$json) {
|
174: | if (!isset($this->session->data['wishlist'])) {
|
175: | $this->session->data['wishlist'] = [];
|
176: | }
|
177: |
|
178: | $this->session->data['wishlist'][] = $product_id;
|
179: |
|
180: | $this->session->data['wishlist'] = array_unique($this->session->data['wishlist']);
|
181: |
|
182: |
|
183: | if ($this->customer->isLogged()) {
|
184: |
|
185: | $this->load->model('account/wishlist');
|
186: |
|
187: | $this->model_account_wishlist->addWishlist($this->customer->getId(), $product_id);
|
188: |
|
189: | $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product_id), $product_info['name'], $this->url->link('account/wishlist', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : '')));
|
190: |
|
191: | $json['total'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist($this->customer->getId()));
|
192: | } else {
|
193: | $json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')), $this->url->link('account/register', 'language=' . $this->config->get('config_language')), $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . (int)$product_id), $product_info['name'], $this->url->link('account/wishlist', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : '')));
|
194: |
|
195: | $json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
|
196: | }
|
197: | }
|
198: |
|
199: | $this->response->addHeader('Content-Type: application/json');
|
200: | $this->response->setOutput(json_encode($json));
|
201: | }
|
202: |
|
203: | |
204: | |
205: | |
206: | |
207: |
|
208: | public function remove(): void {
|
209: | $this->load->language('account/wishlist');
|
210: |
|
211: | $json = [];
|
212: |
|
213: | if (isset($this->request->get['product_id'])) {
|
214: | $product_id = (int)$this->request->get['product_id'];
|
215: | } else {
|
216: | $product_id = 0;
|
217: | }
|
218: |
|
219: | if (!$this->customer->isLogged()) {
|
220: | $json['error'] = sprintf($this->language->get('error_login'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')), $this->url->link('account/register', 'language=' . $this->config->get('config_language')), $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . (int)$product_id), $this->url->link('account/wishlist', 'language=' . $this->config->get('config_language')));
|
221: | }
|
222: |
|
223: | if (!$json) {
|
224: | $this->load->model('account/wishlist');
|
225: |
|
226: | $this->model_account_wishlist->deleteWishlist($this->customer->getId(), $product_id);
|
227: |
|
228: | $json['success'] = $this->language->get('text_remove');
|
229: | }
|
230: |
|
231: | $this->response->addHeader('Content-Type: application/json');
|
232: | $this->response->setOutput(json_encode($json));
|
233: | }
|
234: | }
|
235: | |