/catalog/controller/account/wishlist.php

https://gitlab.com/shapcy/opencart · PHP · 174 lines · 128 code · 44 blank · 2 comment · 21 complexity · 946f8ef524a9ff953c2429cd7ad7ea52 MD5 · raw file

  1. <?php
  2. class ControllerAccountWishList extends Controller {
  3. public function index() {
  4. if (!$this->customer->isLogged()) {
  5. $this->session->data['redirect'] = $this->url->link('account/wishlist', '', true);
  6. $this->response->redirect($this->url->link('account/login', '', true));
  7. }
  8. $this->load->language('account/wishlist');
  9. $this->load->model('account/wishlist');
  10. $this->load->model('catalog/product');
  11. $this->load->model('tool/image');
  12. if (isset($this->request->get['remove'])) {
  13. // Remove Wishlist
  14. $this->model_account_wishlist->deleteWishlist($this->request->get['remove']);
  15. $this->session->data['success'] = $this->language->get('text_remove');
  16. $this->response->redirect($this->url->link('account/wishlist'));
  17. }
  18. $this->document->setTitle($this->language->get('heading_title'));
  19. $data['breadcrumbs'] = array();
  20. $data['breadcrumbs'][] = array(
  21. 'text' => $this->language->get('text_home'),
  22. 'href' => $this->url->link('common/home')
  23. );
  24. $data['breadcrumbs'][] = array(
  25. 'text' => $this->language->get('text_account'),
  26. 'href' => $this->url->link('account/account', '', true)
  27. );
  28. $data['breadcrumbs'][] = array(
  29. 'text' => $this->language->get('heading_title'),
  30. 'href' => $this->url->link('account/wishlist')
  31. );
  32. $data['heading_title'] = $this->language->get('heading_title');
  33. $data['text_empty'] = $this->language->get('text_empty');
  34. $data['column_image'] = $this->language->get('column_image');
  35. $data['column_name'] = $this->language->get('column_name');
  36. $data['column_model'] = $this->language->get('column_model');
  37. $data['column_stock'] = $this->language->get('column_stock');
  38. $data['column_price'] = $this->language->get('column_price');
  39. $data['column_action'] = $this->language->get('column_action');
  40. $data['button_continue'] = $this->language->get('button_continue');
  41. $data['button_cart'] = $this->language->get('button_cart');
  42. $data['button_remove'] = $this->language->get('button_remove');
  43. if (isset($this->session->data['success'])) {
  44. $data['success'] = $this->session->data['success'];
  45. unset($this->session->data['success']);
  46. } else {
  47. $data['success'] = '';
  48. }
  49. $data['products'] = array();
  50. $results = $this->model_account_wishlist->getWishlist();
  51. foreach ($results as $result) {
  52. $product_info = $this->model_catalog_product->getProduct($result['product_id']);
  53. if ($product_info) {
  54. if ($product_info['image']) {
  55. $image = $this->model_tool_image->resize($product_info['image'], $this->config->get($this->config->get('config_theme') . '_image_wishlist_width'), $this->config->get($this->config->get('config_theme') . '_image_wishlist_height'));
  56. } else {
  57. $image = false;
  58. }
  59. if ($product_info['quantity'] <= 0) {
  60. $stock = $product_info['stock_status'];
  61. } elseif ($this->config->get('config_stock_display')) {
  62. $stock = $product_info['quantity'];
  63. } else {
  64. $stock = $this->language->get('text_instock');
  65. }
  66. if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
  67. $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  68. } else {
  69. $price = false;
  70. }
  71. if ((float)$product_info['special']) {
  72. $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  73. } else {
  74. $special = false;
  75. }
  76. $data['products'][] = array(
  77. 'product_id' => $product_info['product_id'],
  78. 'thumb' => $image,
  79. 'name' => $product_info['name'],
  80. 'model' => $product_info['model'],
  81. 'stock' => $stock,
  82. 'price' => $price,
  83. 'special' => $special,
  84. 'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
  85. 'remove' => $this->url->link('account/wishlist', 'remove=' . $product_info['product_id'])
  86. );
  87. } else {
  88. $this->model_account_wishlist->deleteWishlist($result['product_id']);
  89. }
  90. }
  91. $data['continue'] = $this->url->link('account/account', '', true);
  92. $data['column_left'] = $this->load->controller('common/column_left');
  93. $data['column_right'] = $this->load->controller('common/column_right');
  94. $data['content_top'] = $this->load->controller('common/content_top');
  95. $data['content_bottom'] = $this->load->controller('common/content_bottom');
  96. $data['footer'] = $this->load->controller('common/footer');
  97. $data['header'] = $this->load->controller('common/header');
  98. $this->response->setOutput($this->load->view('account/wishlist', $data));
  99. }
  100. public function add() {
  101. $this->load->language('account/wishlist');
  102. $json = array();
  103. if (isset($this->request->post['product_id'])) {
  104. $product_id = $this->request->post['product_id'];
  105. } else {
  106. $product_id = 0;
  107. }
  108. $this->load->model('catalog/product');
  109. $product_info = $this->model_catalog_product->getProduct($product_id);
  110. if ($product_info) {
  111. if ($this->customer->isLogged()) {
  112. // Edit customers cart
  113. $this->load->model('account/wishlist');
  114. $this->model_account_wishlist->addWishlist($this->request->post['product_id']);
  115. $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . (int)$this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
  116. $json['total'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
  117. } else {
  118. if (!isset($this->session->data['wishlist'])) {
  119. $this->session->data['wishlist'] = array();
  120. }
  121. $this->session->data['wishlist'][] = $this->request->post['product_id'];
  122. $this->session->data['wishlist'] = array_unique($this->session->data['wishlist']);
  123. $json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', '', true), $this->url->link('account/register', '', true), $this->url->link('product/product', 'product_id=' . (int)$this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
  124. $json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
  125. }
  126. }
  127. $this->response->addHeader('Content-Type: application/json');
  128. $this->response->setOutput(json_encode($json));
  129. }
  130. }