product.php (28504B)
1 <?php 2 class ModelCatalogProduct extends Model { 3 public function updateViewed($product_id) { 4 $this->db->query("UPDATE " . DB_PREFIX . "product SET viewed = (viewed + 1) WHERE product_id = '" . (int)$product_id . "'"); 5 } 6 7 public function getProduct($product_id) { 8 $query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (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, (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND pr.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (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 COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews, p.sort_order FROM " . DB_PREFIX . "product p 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) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND 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') . "'"); 9 10 if ($query->num_rows) { 11 return array( 12 'product_id' => $query->row['product_id'], 13 'name' => $query->row['name'], 14 'description' => $query->row['description'], 15 'meta_title' => $query->row['meta_title'], 16 'meta_description' => $query->row['meta_description'], 17 'meta_keyword' => $query->row['meta_keyword'], 18 'tag' => $query->row['tag'], 19 'model' => $query->row['model'], 20 'sku' => $query->row['sku'], 21 'upc' => $query->row['upc'], 22 'ean' => $query->row['ean'], 23 'jan' => $query->row['jan'], 24 'isbn' => $query->row['isbn'], 25 'mpn' => $query->row['mpn'], 26 'location' => $query->row['location'], 27 'quantity' => $query->row['quantity'], 28 'stock_status' => $query->row['stock_status'], 29 'image' => $query->row['image'], 30 'manufacturer_id' => $query->row['manufacturer_id'], 31 'manufacturer' => $query->row['manufacturer'], 32 'price' => ($query->row['discount'] ? $query->row['discount'] : $query->row['price']), 33 'special' => $query->row['special'], 34 'reward' => $query->row['reward'], 35 'points' => $query->row['points'], 36 'tax_class_id' => $query->row['tax_class_id'], 37 'date_available' => $query->row['date_available'], 38 'weight' => $query->row['weight'], 39 'weight_class_id' => $query->row['weight_class_id'], 40 'length' => $query->row['length'], 41 'width' => $query->row['width'], 42 'height' => $query->row['height'], 43 'length_class_id' => $query->row['length_class_id'], 44 'subtract' => $query->row['subtract'], 45 'rating' => round($query->row['rating']), 46 'reviews' => $query->row['reviews'] ? $query->row['reviews'] : 0, 47 'minimum' => $query->row['minimum'], 48 'sort_order' => $query->row['sort_order'], 49 'status' => $query->row['status'], 50 'date_added' => $query->row['date_added'], 51 'date_modified' => $query->row['date_modified'], 52 'viewed' => $query->row['viewed'] 53 ); 54 } else { 55 return false; 56 } 57 } 58 59 public function getProducts($data = array()) { 60 $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"; 61 62 if (!empty($data['filter_category_id'])) { 63 if (!empty($data['filter_sub_category'])) { 64 $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)"; 65 } else { 66 $sql .= " FROM " . DB_PREFIX . "product_to_category p2c"; 67 } 68 69 if (!empty($data['filter_filter'])) { 70 $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)"; 71 } else { 72 $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)"; 73 } 74 } else { 75 $sql .= " FROM " . DB_PREFIX . "product p"; 76 } 77 78 $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') . "'"; 79 80 if (!empty($data['filter_category_id'])) { 81 if (!empty($data['filter_sub_category'])) { 82 $sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'"; 83 } else { 84 $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'"; 85 } 86 87 if (!empty($data['filter_filter'])) { 88 $implode = array(); 89 90 $filters = explode(',', $data['filter_filter']); 91 92 foreach ($filters as $filter_id) { 93 $implode[] = (int)$filter_id; 94 } 95 96 $sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")"; 97 } 98 } 99 100 if (!empty($data['filter_name']) || !empty($data['filter_tag'])) { 101 $sql .= " AND ("; 102 103 if (!empty($data['filter_name'])) { 104 $implode = array(); 105 106 $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name']))); 107 108 foreach ($words as $word) { 109 $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'"; 110 } 111 112 if ($implode) { 113 $sql .= " " . implode(" AND ", $implode) . ""; 114 } 115 116 if (!empty($data['filter_description'])) { 117 $sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'"; 118 } 119 } 120 121 if (!empty($data['filter_name']) && !empty($data['filter_tag'])) { 122 $sql .= " OR "; 123 } 124 125 if (!empty($data['filter_tag'])) { 126 $implode = array(); 127 128 $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_tag']))); 129 130 foreach ($words as $word) { 131 $implode[] = "pd.tag LIKE '%" . $this->db->escape($word) . "%'"; 132 } 133 134 if ($implode) { 135 $sql .= " " . implode(" AND ", $implode) . ""; 136 } 137 } 138 139 if (!empty($data['filter_name'])) { 140 $sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 141 $sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 142 $sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 143 $sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 144 $sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 145 $sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 146 $sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 147 } 148 149 $sql .= ")"; 150 } 151 152 if (!empty($data['filter_manufacturer_id'])) { 153 $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'"; 154 } 155 156 $sql .= " GROUP BY p.product_id"; 157 158 $sort_data = array( 159 'pd.name', 160 'p.model', 161 'p.quantity', 162 'p.price', 163 'rating', 164 'p.sort_order', 165 'p.date_added' 166 ); 167 168 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 169 if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') { 170 $sql .= " ORDER BY LCASE(" . $data['sort'] . ")"; 171 } elseif ($data['sort'] == 'p.price') { 172 $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)"; 173 } else { 174 $sql .= " ORDER BY " . $data['sort']; 175 } 176 } else { 177 $sql .= " ORDER BY p.sort_order"; 178 } 179 180 if (isset($data['order']) && ($data['order'] == 'DESC')) { 181 $sql .= " DESC, LCASE(pd.name) DESC"; 182 } else { 183 $sql .= " ASC, LCASE(pd.name) ASC"; 184 } 185 186 if (isset($data['start']) || isset($data['limit'])) { 187 if ($data['start'] < 0) { 188 $data['start'] = 0; 189 } 190 191 if ($data['limit'] < 1) { 192 $data['limit'] = 20; 193 } 194 195 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 196 } 197 198 $product_data = array(); 199 200 $query = $this->db->query($sql); 201 202 foreach ($query->rows as $result) { 203 $product_data[$result['product_id']] = $this->getProduct($result['product_id']); 204 } 205 206 return $product_data; 207 } 208 209 public function getProductSpecials($data = array()) { 210 $sql = "SELECT DISTINCT ps.product_id, (SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = ps.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating FROM " . DB_PREFIX . "product_special ps LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) 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 p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_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())) GROUP BY ps.product_id"; 211 212 $sort_data = array( 213 'pd.name', 214 'p.model', 215 'ps.price', 216 'rating', 217 'p.sort_order' 218 ); 219 220 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 221 if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') { 222 $sql .= " ORDER BY LCASE(" . $data['sort'] . ")"; 223 } else { 224 $sql .= " ORDER BY " . $data['sort']; 225 } 226 } else { 227 $sql .= " ORDER BY p.sort_order"; 228 } 229 230 if (isset($data['order']) && ($data['order'] == 'DESC')) { 231 $sql .= " DESC, LCASE(pd.name) DESC"; 232 } else { 233 $sql .= " ASC, LCASE(pd.name) ASC"; 234 } 235 236 if (isset($data['start']) || isset($data['limit'])) { 237 if ($data['start'] < 0) { 238 $data['start'] = 0; 239 } 240 241 if ($data['limit'] < 1) { 242 $data['limit'] = 20; 243 } 244 245 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 246 } 247 248 $product_data = array(); 249 250 $query = $this->db->query($sql); 251 252 foreach ($query->rows as $result) { 253 $product_data[$result['product_id']] = $this->getProduct($result['product_id']); 254 } 255 256 return $product_data; 257 } 258 259 public function getLatestProducts($limit) { 260 $product_data = $this->cache->get('product.latest.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit); 261 262 if (!$product_data) { 263 $query = $this->db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.date_added DESC LIMIT " . (int)$limit); 264 265 foreach ($query->rows as $result) { 266 $product_data[$result['product_id']] = $this->getProduct($result['product_id']); 267 } 268 269 $this->cache->set('product.latest.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit, $product_data); 270 } 271 272 return $product_data; 273 } 274 275 public function getPopularProducts($limit) { 276 $product_data = $this->cache->get('product.popular.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit); 277 278 if (!$product_data) { 279 $query = $this->db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.viewed DESC, p.date_added DESC LIMIT " . (int)$limit); 280 281 foreach ($query->rows as $result) { 282 $product_data[$result['product_id']] = $this->getProduct($result['product_id']); 283 } 284 285 $this->cache->set('product.popular.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit, $product_data); 286 } 287 288 return $product_data; 289 } 290 291 public function getBestSellerProducts($limit) { 292 $product_data = $this->cache->get('product.bestseller.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit); 293 294 if (!$product_data) { 295 $product_data = array(); 296 297 $query = $this->db->query("SELECT op.product_id, SUM(op.quantity) AS total FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) LEFT JOIN `" . DB_PREFIX . "product` p ON (op.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE o.order_status_id > '0' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' GROUP BY op.product_id ORDER BY total DESC LIMIT " . (int)$limit); 298 299 foreach ($query->rows as $result) { 300 $product_data[$result['product_id']] = $this->getProduct($result['product_id']); 301 } 302 303 $this->cache->set('product.bestseller.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $this->config->get('config_customer_group_id') . '.' . (int)$limit, $product_data); 304 } 305 306 return $product_data; 307 } 308 309 public function getProductAttributes($product_id) { 310 $product_attribute_group_data = array(); 311 312 $product_attribute_group_query = $this->db->query("SELECT ag.attribute_group_id, agd.name FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_group ag ON (a.attribute_group_id = ag.attribute_group_id) LEFT JOIN " . DB_PREFIX . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id) WHERE pa.product_id = '" . (int)$product_id . "' AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY ag.attribute_group_id ORDER BY ag.sort_order, agd.name"); 313 314 foreach ($product_attribute_group_query->rows as $product_attribute_group) { 315 $product_attribute_data = array(); 316 317 $product_attribute_query = $this->db->query("SELECT a.attribute_id, ad.name, pa.text FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_id . "' AND a.attribute_group_id = '" . (int)$product_attribute_group['attribute_group_id'] . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY a.sort_order, ad.name"); 318 319 foreach ($product_attribute_query->rows as $product_attribute) { 320 $product_attribute_data[] = array( 321 'attribute_id' => $product_attribute['attribute_id'], 322 'name' => $product_attribute['name'], 323 'text' => $product_attribute['text'] 324 ); 325 } 326 327 $product_attribute_group_data[] = array( 328 'attribute_group_id' => $product_attribute_group['attribute_group_id'], 329 'name' => $product_attribute_group['name'], 330 'attribute' => $product_attribute_data 331 ); 332 } 333 334 return $product_attribute_group_data; 335 } 336 337 public function getProductOptions($product_id) { 338 $product_option_data = array(); 339 340 $product_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.product_id = '" . (int)$product_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY o.sort_order"); 341 342 foreach ($product_option_query->rows as $product_option) { 343 $product_option_value_data = array(); 344 345 $product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_id = '" . (int)$product_id . "' AND pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order"); 346 347 foreach ($product_option_value_query->rows as $product_option_value) { 348 $product_option_value_data[] = array( 349 'product_option_value_id' => $product_option_value['product_option_value_id'], 350 'option_value_id' => $product_option_value['option_value_id'], 351 'name' => $product_option_value['name'], 352 'image' => $product_option_value['image'], 353 'quantity' => $product_option_value['quantity'], 354 'subtract' => $product_option_value['subtract'], 355 'price' => $product_option_value['price'], 356 'price_prefix' => $product_option_value['price_prefix'], 357 'weight' => $product_option_value['weight'], 358 'weight_prefix' => $product_option_value['weight_prefix'] 359 ); 360 } 361 362 $product_option_data[] = array( 363 'product_option_id' => $product_option['product_option_id'], 364 'product_option_value' => $product_option_value_data, 365 'option_id' => $product_option['option_id'], 366 'name' => $product_option['name'], 367 'type' => $product_option['type'], 368 'value' => $product_option['value'], 369 'required' => $product_option['required'] 370 ); 371 } 372 373 return $product_option_data; 374 } 375 376 public function getProductDiscounts($product_id) { 377 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND quantity > 1 AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity ASC, priority ASC, price ASC"); 378 379 return $query->rows; 380 } 381 382 public function getProductImages($product_id) { 383 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order ASC"); 384 385 return $query->rows; 386 } 387 388 public function getProductRelated($product_id) { 389 $product_data = array(); 390 391 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related pr LEFT JOIN " . DB_PREFIX . "product p ON (pr.related_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pr.product_id = '" . (int)$product_id . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'"); 392 393 foreach ($query->rows as $result) { 394 $product_data[$result['related_id']] = $this->getProduct($result['related_id']); 395 } 396 397 return $product_data; 398 } 399 400 public function getProductLayoutId($product_id) { 401 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'"); 402 403 if ($query->num_rows) { 404 return (int)$query->row['layout_id']; 405 } else { 406 return 0; 407 } 408 } 409 410 public function getCategories($product_id) { 411 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'"); 412 413 return $query->rows; 414 } 415 416 public function getTotalProducts($data = array()) { 417 $sql = "SELECT COUNT(DISTINCT p.product_id) AS total"; 418 419 if (!empty($data['filter_category_id'])) { 420 if (!empty($data['filter_sub_category'])) { 421 $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)"; 422 } else { 423 $sql .= " FROM " . DB_PREFIX . "product_to_category p2c"; 424 } 425 426 if (!empty($data['filter_filter'])) { 427 $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)"; 428 } else { 429 $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)"; 430 } 431 } else { 432 $sql .= " FROM " . DB_PREFIX . "product p"; 433 } 434 435 $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') . "'"; 436 437 if (!empty($data['filter_category_id'])) { 438 if (!empty($data['filter_sub_category'])) { 439 $sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'"; 440 } else { 441 $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'"; 442 } 443 444 if (!empty($data['filter_filter'])) { 445 $implode = array(); 446 447 $filters = explode(',', $data['filter_filter']); 448 449 foreach ($filters as $filter_id) { 450 $implode[] = (int)$filter_id; 451 } 452 453 $sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")"; 454 } 455 } 456 457 if (!empty($data['filter_name']) || !empty($data['filter_tag'])) { 458 $sql .= " AND ("; 459 460 if (!empty($data['filter_name'])) { 461 $implode = array(); 462 463 $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name']))); 464 465 foreach ($words as $word) { 466 $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'"; 467 } 468 469 if ($implode) { 470 $sql .= " " . implode(" AND ", $implode) . ""; 471 } 472 473 if (!empty($data['filter_description'])) { 474 $sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'"; 475 } 476 } 477 478 if (!empty($data['filter_name']) && !empty($data['filter_tag'])) { 479 $sql .= " OR "; 480 } 481 482 if (!empty($data['filter_tag'])) { 483 $implode = array(); 484 485 $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_tag']))); 486 487 foreach ($words as $word) { 488 $implode[] = "pd.tag LIKE '%" . $this->db->escape($word) . "%'"; 489 } 490 491 if ($implode) { 492 $sql .= " " . implode(" AND ", $implode) . ""; 493 } 494 } 495 496 if (!empty($data['filter_name'])) { 497 $sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 498 $sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 499 $sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 500 $sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 501 $sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 502 $sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 503 $sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'"; 504 } 505 506 $sql .= ")"; 507 } 508 509 if (!empty($data['filter_manufacturer_id'])) { 510 $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'"; 511 } 512 513 $query = $this->db->query($sql); 514 515 return $query->row['total']; 516 } 517 518 public function getProfile($product_id, $recurring_id) { 519 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "recurring r JOIN " . DB_PREFIX . "product_recurring pr ON (pr.recurring_id = r.recurring_id AND pr.product_id = '" . (int)$product_id . "') WHERE pr.recurring_id = '" . (int)$recurring_id . "' AND status = '1' AND pr.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'"); 520 521 return $query->row; 522 } 523 524 public function getProfiles($product_id) { 525 $query = $this->db->query("SELECT rd.* FROM " . DB_PREFIX . "product_recurring pr JOIN " . DB_PREFIX . "recurring_description rd ON (rd.language_id = " . (int)$this->config->get('config_language_id') . " AND rd.recurring_id = pr.recurring_id) JOIN " . DB_PREFIX . "recurring r ON r.recurring_id = rd.recurring_id WHERE pr.product_id = " . (int)$product_id . " AND status = '1' AND pr.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' ORDER BY sort_order ASC"); 526 527 return $query->rows; 528 } 529 530 public function getTotalProductSpecials() { 531 $query = $this->db->query("SELECT COUNT(DISTINCT ps.product_id) AS total FROM " . DB_PREFIX . "product_special ps LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_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()))"); 532 533 if (isset($query->row['total'])) { 534 return $query->row['total']; 535 } else { 536 return 0; 537 } 538 } 539 }