Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. public function Products($data = array()) {
  2. $sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special";
  3.  
  4. if (!empty($data['filter_category_id'])) {
  5. if (!empty($data['filter_sub_category'])) {
  6. $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";
  7. } else {
  8. $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
  9. }
  10.  
  11. if (!empty($data['filter_filter'])) {
  12. $sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
  13. } else {
  14. $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
  15. }
  16. } else {
  17. $sql .= " FROM " . DB_PREFIX . "product p";
  18. }
  19.  
  20. $sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
  21.  
  22. if (!empty($data['filter_category_id'])) {
  23. if (!empty($data['filter_sub_category'])) {
  24. $sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
  25. } else {
  26. $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  27. }
  28.  
  29. if (!empty($data['filter_filter'])) {
  30. $implode = array();
  31.  
  32. $filters = explode(',', $data['filter_filter']);
  33.  
  34. foreach ($filters as $filter_id) {
  35. $implode[] = (int)$filter_id;
  36. }
  37.  
  38. $sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
  39. }
  40. }
  41.  
  42. if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
  43. $sql .= " AND (";
  44.  
  45. if (!empty($data['filter_name'])) {
  46. $implode = array();
  47.  
  48. $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));
  49.  
  50. foreach ($words as $word) {
  51. $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
  52. }
  53.  
  54. if ($implode) {
  55. $sql .= " " . implode(" AND ", $implode) . "";
  56. }
  57.  
  58. if (!empty($data['filter_description'])) {
  59. $sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
  60. }
  61. }
  62.  
  63. if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
  64. $sql .= " OR ";
  65. }
  66.  
  67. if (!empty($data['filter_tag'])) {
  68. $implode = array();
  69.  
  70. $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_tag'])));
  71.  
  72. foreach ($words as $word) {
  73. $implode[] = "pd.tag LIKE '%" . $this->db->escape($word) . "%'";
  74. }
  75.  
  76. if ($implode) {
  77. $sql .= " " . implode(" AND ", $implode) . "";
  78. }
  79. }
  80.  
  81. if (!empty($data['filter_name'])) {
  82. $sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  83. $sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  84. $sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  85. $sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  86. $sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  87. $sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  88. $sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  89. }
  90.  
  91. $sql .= ")";
  92. }
  93.  
  94. if (!empty($data['filter_manufacturer_id'])) {
  95. $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
  96. }
  97.  
  98. $sql .= " GROUP BY p.product_id";
  99.  
  100. $sort_data = array(
  101. 'pd.name',
  102. 'p.model',
  103. 'p.quantity',
  104. 'p.price',
  105. 'rating',
  106. 'p.sort_order',
  107. 'p.date_added'
  108. );
  109.  
  110. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  111. if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
  112. $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
  113. } elseif ($data['sort'] == 'p.price') {
  114. $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
  115. } else {
  116. $sql .= " ORDER BY " . $data['sort'];
  117. }
  118. } else {
  119. $sql .= " ORDER BY p.sort_order";
  120. }
  121.  
  122. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  123. $sql .= " DESC, LCASE(pd.name) DESC";
  124. } else {
  125. $sql .= " ASC, LCASE(pd.name) ASC";
  126. }
  127.  
  128. if (isset($data['start']) || isset($data['limit'])) {
  129. if ($data['start'] < 0) {
  130. $data['start'] = 0;
  131. }
  132.  
  133. if ($data['limit'] < 1) {
  134. $data['limit'] = 20;
  135. }
  136.  
  137. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  138. }
  139.  
  140. $product_data = array();
  141.  
  142. $query = $this->db->query($sql);
  143.  
  144. foreach ($query->rows as $result) {
  145. $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
  146. }
  147.  
  148. return $product_data;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement