2022-09-19 10:04:50 - 米境通跨境電商
Opencart首頁最新產(chǎn)品(LatestProducts)隨機顯示方法
Opencart首頁LatestProducts產(chǎn)品是不可控的,如果一直不上傳新產(chǎn)品,LatestProducts顯示固定不變,不像zen-cart每次刷新都有變化。我?guī)退鼊右幌滦∈中g。
文件位置:catalogmodelcatalogproduct.php.
方法一
把getLatestProducts修改成以下代碼:
publicfunctiongetLatestProducts($limit){
$query=$this->db->query("SELECT*,pd.nameASname,p.image,m.nameASmanufacturer,ss.nameASstock,(SELECTAVG(r.rating)FROM".DB_PREFIX."reviewrWHEREp.product_id=r.product_idGROUPBYr.product_id)ASratingFROM".DB_PREFIX."productpLEFTJOIN".DB_PREFIX."product_descriptionpdON(p.product_id=pd.product_id)LEFTJOIN".DB_PREFIX."product_to_storep2sON(p.product_id=p2s.product_id)LEFTJOIN".DB_PREFIX."manufacturermON(p.manufacturer_id=m.manufacturer_id)LEFTJOIN".DB_PREFIX."stock_statusssON(p.stock_status_id=ss.stock_status_id)WHEREp.status='1'ANDp.date_available<=NOW()ANDpd.language_id='".(int)$this->config->get('config_language_id')."'ANDp2s.store_id='".(int)$this->config->get('config_store_id')."'ANDss.language_id='".(int)$this->config->get('config_language_id')."'ORDERBYrand()DESCLIMIT".(int)$limit);
$product_data=$query->rows;
return$product_data;
}
方法二
把getLatestProducts修改成以下代碼:
publicfunctiongetLatestProducts($limit){
$product_data=$this->cache->get('product.latest.'.$this->config->get('config_language_id').'.'.(int)$this->config->get('config_store_id').'.'.$limit);
if(!$product_data){
$query=$this->db->query("SELECT*,pd.nameASname,p.image,m.nameASmanufacturer,ss.nameASstock,(SELECTAVG(r.rating)FROM".DB_PREFIX."reviewrWHEREp.product_id=r.product_idGROUPBYr.product_id)ASratingFROM".DB_PREFIX."productpLEFTJOIN".DB_PREFIX."product_descriptionpdON(p.product_id=pd.product_id)LEFTJOIN".DB_PREFIX."product_to_storep2sON(p.product_id=p2s.product_id)LEFTJOIN".DB_PREFIX."manufacturermON(p.manufacturer_id=m.manufacturer_id)LEFTJOIN".DB_PREFIX."stock_statusssON(p.stock_status_id=ss.stock_status_id)WHEREp.status='1'ANDp.date_available<=NOW()ANDpd.language_id='".(int)$this->config->get('config_language_id')."'ANDp2s.store_id='".(int)$this->config->get('config_store_id')."'ANDss.language_id='".(int)$this->config->get('config_language_id')."'ORDERBYrand()DESCLIMIT".(int)$limit);
$product_data=$query->rows;
$this->cache->set('product.latest.'.$this->config->get('config_language_id').'.'.(int)$this->config->get('config_store_id').'.'.$limit,$product_data);
}
return$product_data;
}
去前臺刷新一下,看一下效果
方法一和方法二區(qū)別,方法二執(zhí)行速度比方法一快。但是每次都要刪除systemcachecache.product.latest.*這個文件才有更新。如果產(chǎn)品很多最好選用方法二