Advertisement
krot

ModelCatalogProduct opencart

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