Advertisement
tacheshun

OpenCart_Product_Model

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