TShopping

 找回密碼
 註冊
搜索
查看: 370|回復: 0

[教學] opencart首頁最新產品(Latest Products)隨機顯示方法

[複製鏈接]
發表於 2014-10-15 17:42:57 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 

opencart首頁Latest Products產品是不可控的,如果一直不上傳新產品, Latest Products顯示固定不變,不像zen-cart每次刷新都有變化。我幫牠動一下小手術。

文件位置:catalog\model\catalog\product.php.

方法一

把getLatestProducts修改成以下代碼:
  1. public function getLatestProducts($limit) {

  2. $query = $this->db->query("SELECT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.p​​roduct_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.p​​roduct_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.status = '1' AND p.date_available <= NOW() AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s .store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY rand() DESC LIMIT " . (int)$limit);

  3. $product_data = $query->rows;

  4. return $product_data;
  5. }
複製代碼
方法二

把getLatestProducts修改成以下代碼:
  1. public function getLatestProducts($limit) {
  2.         $product_data = $this->cache->get('product.latest.' . $this->config->get('config_language_id') . '. ' . (int)$this->config->get('config_store_id') . '.' . $limit);

  3.         if (!$product_data) {

  4.             $query = $this->db->query("SELECT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.p​​roduct_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.p​​roduct_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.status = '1' AND p.date_available <= NOW() AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s .store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY rand() DESC LIMIT " . (int)$limit);


  5.             $product_data = $query->rows;

  6.             $this->cache->set('product.latest.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit, $product_data);
  7.         }         return $product_data;     }
複製代碼
去前台刷新一下,看一下效果

方法一和方法二區別,方法二執行速度比方法一快。但是每次都要刪除system\cache\cache.product.latest.*這個文件才有更新。如果產品很多最好選用方法二

 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-9 15:59 , Processed in 0.059565 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表