Advertisement
Guest User

product.php

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