2022-11-03 14:42:22 - 米境通跨境電商
Opencart首頁最新產(chǎn)品(LatestProducts)隨機(jī)顯示方法
Opencart首頁LatestProducts產(chǎn)品是不可控的,如果一直不上傳新產(chǎn)品,LatestProducts顯示固定不變,不像zen-cart每次刷新都有變化。我?guī)退鼊右幌滦∈中g(shù)。
文件位置: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;
}
去前臺刷新一下,看一下效果