Advertisement
Guest User

Untitled

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