1: | <?php
2: | namespace Opencart\Catalog\Controller\Api\Sale;
3: | /**
4: | * Class Cart
5: | *
6: | * @package Opencart\Catalog\Controller\Api\Sale
7: | */
8: | class Cart extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('api/sale/cart');
14: |
15: | $json = [];
16: |
17: | // Stock
18: | if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
19: | $json['error']['stock'] = $this->language->get('error_stock');
20: | }
21: |
22: | $totals = [];
23: | $taxes = $this->cart->getTaxes();
24: | $total = 0;
25: |
26: | $this->load->model('checkout/cart');
27: |
28: | ($this->model_checkout_cart->getTotals)($totals, $taxes, $total);
29: |
30: | $json['products'] = [];
31: |
32: | $products = $this->model_checkout_cart->getProducts();
33: |
34: | foreach ($products as $product) {
35: | $description = '';
36: |
37: | if ($product['subscription']) {
38: | if ($product['subscription']['trial_status']) {
39: | $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']);
40: | $trial_cycle = $product['subscription']['trial_cycle'];
41: | $trial_frequency = $this->language->get('text_' . $product['subscription']['trial_frequency']);
42: | $trial_duration = $product['subscription']['trial_duration'];
43: |
44: | $description .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
45: | }
46: |
47: | $price = $this->currency->format($this->tax->calculate($product['subscription']['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
48: | $cycle = $product['subscription']['cycle'];
49: | $frequency = $this->language->get('text_' . $product['subscription']['frequency']);
50: | $duration = $product['subscription']['duration'];
51: |
52: | if ($duration) {
53: | $description .= sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
54: | } else {
55: | $description .= sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
56: | }
57: | }
58: |
59: | $json['products'][] = [
60: | 'cart_id' => $product['cart_id'],
61: | 'product_id' => $product['product_id'],
62: | 'name' => $product['name'],
63: | 'model' => $product['model'],
64: | 'option' => $product['option'],
65: | 'subscription' => $description,
66: | 'quantity' => $product['quantity'],
67: | 'stock' => $product['stock'],
68: | 'minimum' => $product['minimum'],
69: | 'reward' => $product['reward'],
70: | 'price' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']),
71: | 'total' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity'], $this->session->data['currency'])
72: | ];
73: | }
74: |
75: | $json['vouchers'] = [];
76: |
77: | $vouchers = $this->model_checkout_cart->getVouchers();
78: |
79: | foreach ($vouchers as $key => $voucher) {
80: | $json['vouchers'][] = [
81: | 'key' => $key,
82: | 'description' => sprintf($this->language->get('text_for'), $this->currency->format($voucher['amount'], $this->session->data['currency']), $voucher['to_name']),
83: | 'amount' => $this->currency->format($voucher['amount'], $this->session->data['currency'])
84: | ];
85: | }
86: |
87: | $json['totals'] = [];
88: |
89: | foreach ($totals as $total) {
90: | $json['totals'][] = [
91: | 'title' => $total['title'],
92: | 'text' => $this->currency->format($total['value'], $this->session->data['currency'])
93: | ];
94: | }
95: |
96: | $json['shipping_required'] = $this->cart->hasShipping();
97: |
98: | $this->response->addHeader('Content-Type: application/json');
99: | $this->response->setOutput(json_encode($json));
100: | }
101: |
102: | /**
103: | * Add
104: | *
105: | * @return void
106: | */
107: | public function add(): void {
108: | $this->load->language('api/sale/cart');
109: |
110: | $json = [];
111: |
112: | if (isset($this->request->post['product_id'])) {
113: | $product_id = (int)$this->request->post['product_id'];
114: | } else {
115: | $product_id = 0;
116: | }
117: |
118: | if (isset($this->request->post['quantity'])) {
119: | $quantity = (int)$this->request->post['quantity'];
120: | } else {
121: | $quantity = 1;
122: | }
123: |
124: | if (isset($this->request->post['option'])) {
125: | $option = array_filter($this->request->post['option']);
126: | } else {
127: | $option = [];
128: | }
129: |
130: | if (isset($this->request->post['subscription_plan_id'])) {
131: | $subscription_plan_id = (int)$this->request->post['subscription_plan_id'];
132: | } else {
133: | $subscription_plan_id = 0;
134: | }
135: |
136: | $this->load->model('catalog/product');
137: |
138: | $product_info = $this->model_catalog_product->getProduct($product_id);
139: |
140: | if ($product_info) {
141: | // If variant get master product
142: | if ($product_info['master_id']) {
143: | $product_id = $product_info['master_id'];
144: | }
145: |
146: | // Merge variant code with options
147: | foreach ($product_info['variant'] as $key => $value) {
148: | $option[$key] = $value;
149: | }
150: |
151: | // Validate options
152: | $product_options = $this->model_catalog_product->getOptions($product_id);
153: |
154: | foreach ($product_options as $product_option) {
155: | if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
156: | $json['error']['option_' . $product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
157: | }
158: | }
159: |
160: | // Validate Subscription plan
161: | $subscriptions = $this->model_catalog_product->getSubscriptions($product_id);
162: |
163: | if ($subscriptions) {
164: | $subscription_plan_ids = [];
165: |
166: | foreach ($subscriptions as $subscription) {
167: | $subscription_plan_ids[] = $subscription['subscription_plan_id'];
168: | }
169: |
170: | if (!in_array($subscription_plan_id, $subscription_plan_ids)) {
171: | $json['error']['subscription'] = $this->language->get('error_subscription');
172: | }
173: | }
174: | } else {
175: | $json['error']['warning'] = $this->language->get('error_product');
176: | }
177: |
178: | if (!$json) {
179: | $this->cart->add($product_id, $quantity, $option, $subscription_plan_id);
180: |
181: | $json['success'] = $this->language->get('text_success');
182: | }
183: |
184: | $this->response->addHeader('Content-Type: application/json');
185: | $this->response->setOutput(json_encode($json));
186: | }
187: |
188: | /**
189: | * Edit
190: | *
191: | * @return void
192: | */
193: | public function edit(): void {
194: | $this->load->language('api/sale/cart');
195: |
196: | $json = [];
197: |
198: | if (isset($this->request->post['key'])) {
199: | $key = (int)$this->request->post['key'];
200: | } else {
201: | $key = 0;
202: | }
203: |
204: | if (isset($this->request->post['quantity'])) {
205: | $quantity = (int)$this->request->post['quantity'];
206: | } else {
207: | $quantity = 1;
208: | }
209: |
210: | $this->cart->update($key, $quantity);
211: |
212: | $json['success'] = $this->language->get('text_success');
213: |
214: | unset($this->session->data['reward']);
215: |
216: | $this->response->addHeader('Content-Type: application/json');
217: | $this->response->setOutput(json_encode($json));
218: | }
219: |
220: | /**
221: | * Remove
222: | *
223: | * @return void
224: | */
225: | public function remove(): void {
226: | $this->load->language('api/sale/cart');
227: |
228: | $json = [];
229: |
230: | if (isset($this->request->post['key'])) {
231: | $key = (int)$this->request->post['key'];
232: | } else {
233: | $key = 0;
234: | }
235: |
236: | // Remove
237: | $this->cart->remove($key);
238: |
239: | $json['success'] = $this->language->get('text_success');
240: |
241: | unset($this->session->data['reward']);
242: |
243: | $this->response->addHeader('Content-Type: application/json');
244: | $this->response->setOutput(json_encode($json));
245: | }
246: | }
247: |

OpenCart API API documentation generated by ApiGen dev-master