load->language('checkout/cart'); $this->document->setTitle($this->language->get('heading_title')); $data['breadcrumbs'] = []; $data['breadcrumbs'][] = [ 'text' => $this->language->get('text_home'), 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language')) ]; $data['breadcrumbs'][] = [ 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language')) ]; if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) { if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) { $data['error_warning'] = $this->language->get('error_stock'); } elseif (isset($this->session->data['error'])) { $data['error_warning'] = $this->session->data['error']; unset($this->session->data['error']); } else { $data['error_warning'] = ''; } if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) { $data['attention'] = 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'))); } else { $data['attention'] = ''; } if (isset($this->session->data['success'])) { $data['success'] = $this->session->data['success']; unset($this->session->data['success']); } else { $data['success'] = ''; } $data['action'] = $this->url->link('checkout/cart|edit', 'language=' . $this->config->get('config_language')); if ($this->config->get('config_cart_weight')) { $data['weight'] = $this->weight->format($this->cart->getWeight(), $this->config->get('config_weight_class_id'), $this->language->get('decimal_point'), $this->language->get('thousand_point')); } else { $data['weight'] = ''; } $this->load->model('tool/image'); $this->load->model('tool/upload'); $frequencies = [ 'day' => $this->language->get('text_day'), 'week' => $this->language->get('text_week'), 'semi_month' => $this->language->get('text_semi_month'), 'month' => $this->language->get('text_month'), 'year' => $this->language->get('text_year') ]; $data['products'] = []; $products = $this->cart->getProducts(); foreach ($products as $product) { $product_total = 0; foreach ($products as $product_2) { if ($product_2['product_id'] == $product['product_id']) { $product_total += $product_2['quantity']; } } if ($product['minimum'] > $product_total) { $data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); } if ($product['image']) { $image = $this->model_tool_image->resize(html_entity_decode($product['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')); } else { $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')); } $option_data = []; foreach ($product['option'] as $option) { if ($option['type'] != 'file') { $value = $option['value']; } else { $upload_info = $this->model_tool_upload->getUploadByCode($option['value']); if ($upload_info) { $value = $upload_info['name']; } else { $value = ''; } } $option_data[] = [ 'name' => $option['name'], 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) ]; } // Display prices if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { $unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')); $price = $this->currency->format($unit_price, $this->session->data['currency']); $total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']); } else { $price = false; $total = false; } $recurring = ''; if ($product['recurring']) { if ($product['recurring']['trial']) { $recurring = sprintf($this->language->get('text_trial_description'), $this->currency->format($this->tax->calculate($product['recurring']['trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['trial_cycle'], $frequencies[$product['recurring']['trial_frequency']], $product['recurring']['trial_duration']) . ' '; } if ($product['recurring']['duration']) { $recurring .= sprintf($this->language->get('text_payment_description'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']); } else { $recurring .= sprintf($this->language->get('text_payment_cancel'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']); } } $data['products'][] = [ 'cart_id' => $product['cart_id'], 'thumb' => $image, 'name' => $product['name'], 'model' => $product['model'], 'option' => $option_data, 'recurring' => $recurring, 'quantity' => $product['quantity'], 'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')), 'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''), 'price' => $price, 'total' => $total, 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product['product_id']) ]; } // Gift Voucher $data['vouchers'] = []; if (!empty($this->session->data['vouchers'])) { foreach ($this->session->data['vouchers'] as $key => $voucher) { $data['vouchers'][] = [ 'key' => $key, 'description' => $voucher['description'], 'amount' => $this->currency->format($voucher['amount'], $this->session->data['currency']), 'remove' => $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language') . '&remove=' . $key) ]; } } // Totals $this->load->model('setting/extension'); $totals = []; $taxes = $this->cart->getTaxes(); $total = 0; // Display prices if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { $sort_order = []; $results = $this->model_setting_extension->getExtensionsByType('total'); foreach ($results as $key => $value) { $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); } array_multisort($sort_order, SORT_ASC, $results); foreach ($results as $result) { if ($this->config->get('total_' . $result['code'] . '_status')) { $this->load->model('extension/' . $result['extension'] . '/total/' . $result['code']); // __call can not pass-by-reference so we get PHP to call it as an anonymous function. ($this->{'model_extension_' . $result['extension'] . '_total_' . $result['code']}->getTotal)($totals, $taxes, $total); } } $sort_order = []; foreach ($totals as $key => $value) { $sort_order[$key] = $value['sort_order']; } array_multisort($sort_order, SORT_ASC, $totals); } $data['totals'] = []; foreach ($totals as $total) { $data['totals'][] = [ 'title' => $total['title'], 'text' => $this->currency->format($total['value'], $this->session->data['currency']) ]; } $data['continue'] = $this->url->link('common/home', 'language=' . $this->config->get('config_language')); $data['checkout'] = $this->url->link('checkout/checkout', 'language=' . $this->config->get('config_language')); $data['modules'] = []; foreach ($results as $result) { if ($this->config->get('total_' . $result['code'] . '_status')) { $result = $this->load->controller('extension/' . $result['extension'] . '/total/' . $result['code']); if (!$result instanceof \Exception) { $data['modules'][] = $result; } } } $data['column_left'] = $this->load->controller('common/column_left'); $data['column_right'] = $this->load->controller('common/column_right'); $data['content_top'] = $this->load->controller('common/content_top'); $data['content_bottom'] = $this->load->controller('common/content_bottom'); $data['footer'] = $this->load->controller('common/footer'); $data['header'] = $this->load->controller('common/header'); $this->response->setOutput($this->load->view('checkout/cart', $data)); } else { $data['text_error'] = $this->language->get('text_no_results'); $data['continue'] = $this->url->link('common/home', 'language=' . $this->config->get('config_language')); unset($this->session->data['success']); $data['column_left'] = $this->load->controller('common/column_left'); $data['column_right'] = $this->load->controller('common/column_right'); $data['content_top'] = $this->load->controller('common/content_top'); $data['content_bottom'] = $this->load->controller('common/content_bottom'); $data['footer'] = $this->load->controller('common/footer'); $data['header'] = $this->load->controller('common/header'); $this->response->setOutput($this->load->view('error/not_found', $data)); } } public function add(): void { $this->load->language('checkout/cart'); $json = []; if (isset($this->request->post['product_id'])) { $product_id = (int)$this->request->post['product_id']; } else { $product_id = 0; } $this->load->model('catalog/product'); $product_info = $this->model_catalog_product->getProduct($product_id); if ($product_info) { if (isset($this->request->post['quantity'])) { $quantity = (int)$this->request->post['quantity']; } else { $quantity = 1; } if (isset($this->request->post['option'])) { $option = array_filter($this->request->post['option']); } else { $option = []; } // If variant get master product if ($product_info['master_id']) { $product_id = $product_info['master_id']; } // Merge variant code with options foreach ($product_info['variant'] as $key => $value) { $option[$key] = $value; } $product_options = $this->model_catalog_product->getOptions($product_id); foreach ($product_options as $product_option) { if ($product_option['required'] && empty($option[$product_option['product_option_id']])) { $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']); } } if (isset($this->request->post['recurring_id'])) { $recurring_id = $this->request->post['recurring_id']; } else { $recurring_id = 0; } $recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']); if ($recurrings) { $recurring_ids = []; foreach ($recurrings as $recurring) { $recurring_ids[] = $recurring['recurring_id']; } if (!in_array($recurring_id, $recurring_ids)) { $json['error']['recurring'] = $this->language->get('error_recurring_required'); } } if (!$json) { $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id); $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('checkout/cart', 'language=' . $this->config->get('config_language'))); // Unset all shipping and payment methods unset($this->session->data['shipping_method']); unset($this->session->data['shipping_methods']); unset($this->session->data['payment_method']); unset($this->session->data['payment_methods']); } else { $json['redirect'] = $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $this->request->post['product_id'], true); } } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } public function edit(): void { $this->load->language('checkout/cart'); $json = []; // Update if (!empty($this->request->post['quantity'])) { // Handles single item update if (!empty($this->request->post['key'])) { $key = $this->request->post['key']; $quantity = (int)$this->request->post['quantity']; $this->cart->update($key, $quantity); $json['success'] = $this->language->get('text_remove'); } else { foreach ($this->request->post['quantity'] as $key => $value) { $this->cart->update($key, $value); } $this->session->data['success'] = $this->language->get('text_remove'); unset($this->session->data['shipping_method']); unset($this->session->data['shipping_methods']); unset($this->session->data['payment_method']); unset($this->session->data['payment_methods']); unset($this->session->data['reward']); $this->response->redirect($this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'))); } } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } public function remove(): void { $this->load->language('checkout/cart'); $json = []; // Remove if (isset($this->request->post['key'])) { $this->cart->remove($this->request->post['key']); unset($this->session->data['vouchers'][$this->request->post['key']]); $json['success'] = $this->language->get('text_remove'); unset($this->session->data['shipping_method']); unset($this->session->data['shipping_methods']); unset($this->session->data['payment_method']); unset($this->session->data['payment_methods']); unset($this->session->data['reward']); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } }