1: | <?php
2: | namespace Opencart\Catalog\Controller\Common;
3: | /**
4: | * Class Cart
5: | *
6: | * @package Opencart\Catalog\Controller\Common
7: | */
8: | class Cart extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return string
11: | */
12: | public function index(): string {
13: | $this->load->language('common/cart');
14: |
15: | $totals = [];
16: | $taxes = $this->cart->getTaxes();
17: | $total = 0;
18: |
19: | $this->load->model('checkout/cart');
20: |
21: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
22: | ($this->model_checkout_cart->getTotals)($totals, $taxes, $total);
23: | }
24: |
25: | $data['text_items'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency']));
26: |
27: | // Products
28: | $data['products'] = [];
29: |
30: | $products = $this->model_checkout_cart->getProducts();
31: |
32: | foreach ($products as $product) {
33: | if ($product['option']) {
34: | foreach ($product['option'] as $key => $option) {
35: | $product['option'][$key]['value'] = (oc_strlen($option['value']) > 20 ? oc_substr($option['value'], 0, 20) . '..' : $option['value']);
36: | }
37: | }
38: |
39: | // Display prices
40: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
41: | $unit_price = (float)$this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
42: |
43: | $price = $this->currency->format($unit_price, $this->session->data['currency']);
44: | $total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']);
45: | } else {
46: | $price = false;
47: | $total = false;
48: | }
49: |
50: | $description = '';
51: |
52: | if ($product['subscription']) {
53: | if ($product['subscription']['trial_status']) {
54: | $trial_price = $this->currency->format($this->tax->calculate($product['subscription']['trial_price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
55: | $trial_cycle = $product['subscription']['trial_cycle'];
56: | $trial_frequency = $this->language->get('text_' . $product['subscription']['trial_frequency']);
57: | $trial_duration = $product['subscription']['trial_duration'];
58: |
59: | $description .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
60: | }
61: |
62: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
63: | $price = $this->currency->format($this->tax->calculate($product['subscription']['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
64: | }
65: |
66: | $cycle = $product['subscription']['cycle'];
67: | $frequency = $this->language->get('text_' . $product['subscription']['frequency']);
68: | $duration = $product['subscription']['duration'];
69: |
70: | if ($duration) {
71: | $description .= sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
72: | } else {
73: | $description .= sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
74: | }
75: | }
76: |
77: | $data['products'][] = [
78: | 'cart_id' => $product['cart_id'],
79: | 'thumb' => $product['image'],
80: | 'name' => $product['name'],
81: | 'model' => $product['model'],
82: | 'option' => $product['option'],
83: | 'subscription' => $description,
84: | 'quantity' => $product['quantity'],
85: | 'price' => $price,
86: | 'total' => $total,
87: | 'reward' => $product['reward'],
88: | 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product['product_id'])
89: | ];
90: | }
91: |
92: | // Gift Voucher
93: | $data['vouchers'] = [];
94: |
95: | $vouchers = $this->model_checkout_cart->getVouchers();
96: |
97: | foreach ($vouchers as $key => $voucher) {
98: | $data['vouchers'][] = [
99: | 'key' => $key,
100: | 'description' => $voucher['description'],
101: | 'amount' => $this->currency->format($voucher['amount'], $this->session->data['currency'])
102: | ];
103: | }
104: |
105: | // Totals
106: | $data['totals'] = [];
107: |
108: | foreach ($totals as $total) {
109: | $data['totals'][] = [
110: | 'title' => $total['title'],
111: | 'text' => $this->currency->format($total['value'], $this->session->data['currency'])
112: | ];
113: | }
114: |
115: | $data['list'] = $this->url->link('common/cart.info', 'language=' . $this->config->get('config_language'));
116: | $data['product_remove'] = $this->url->link('common/cart.removeProduct', 'language=' . $this->config->get('config_language'));
117: | $data['voucher_remove'] = $this->url->link('common/cart.removeVoucher', 'language=' . $this->config->get('config_language'));
118: |
119: | $data['cart'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'));
120: | $data['checkout'] = $this->url->link('checkout/checkout', 'language=' . $this->config->get('config_language'));
121: |
122: | return $this->load->view('common/cart', $data);
123: | }
124: |
125: | /**
126: | * Info
127: | *
128: | * @return void
129: | */
130: | public function info(): void {
131: | $this->response->setOutput($this->index());
132: | }
133: |
134: | /**
135: | * Remove Product
136: | *
137: | * @return void
138: | */
139: | public function removeProduct(): void {
140: | $this->load->language('checkout/cart');
141: |
142: | $json = [];
143: |
144: | if (isset($this->request->post['key'])) {
145: | $key = (int)$this->request->post['key'];
146: | } else {
147: | $key = 0;
148: | }
149: |
150: | if (!$this->cart->has($key)) {
151: | $json['error'] = $this->language->get('error_product');
152: | }
153: |
154: | if (!$json) {
155: | $this->cart->remove($key);
156: |
157: | $json['success'] = $this->language->get('text_remove');
158: |
159: | unset($this->session->data['shipping_method']);
160: | unset($this->session->data['shipping_methods']);
161: | unset($this->session->data['payment_method']);
162: | unset($this->session->data['payment_methods']);
163: | unset($this->session->data['reward']);
164: | }
165: |
166: | $this->response->addHeader('Content-Type: application/json');
167: | $this->response->setOutput(json_encode($json));
168: | }
169: |
170: | /**
171: | * Remove Voucher
172: | *
173: | * @return void
174: | */
175: | public function removeVoucher(): void {
176: | $this->load->language('checkout/voucher');
177: |
178: | $json = [];
179: |
180: | if (isset($this->request->post['key'])) {
181: | $key = $this->request->post['key'];
182: | } else {
183: | $key = '';
184: | }
185: |
186: | if (!isset($this->session->data['vouchers'][$key])) {
187: | $json['error'] = $this->language->get('error_voucher');
188: | }
189: |
190: | if (!$json) {
191: | $json['success'] = $this->language->get('text_remove');
192: |
193: | unset($this->session->data['vouchers'][$key]);
194: | unset($this->session->data['shipping_method']);
195: | unset($this->session->data['shipping_methods']);
196: | unset($this->session->data['payment_method']);
197: | unset($this->session->data['payment_methods']);
198: | unset($this->session->data['reward']);
199: | }
200: |
201: | $this->response->addHeader('Content-Type: application/json');
202: | $this->response->setOutput(json_encode($json));
203: | }
204: | }
205: |

OpenCart API API documentation generated by ApiGen dev-master