一、匿名用戶的購(gòu)物車和登錄會(huì)員購(gòu)物車
ecshop的購(gòu)物車機(jī)制,當(dāng)用戶打開(kāi)網(wǎng)站時(shí)會(huì)在session表產(chǎn)生一條seesion,登陸時(shí)更新這條seession。此時(shí)如果用戶將商品添加至購(gòu)物車,就會(huì)在cart表中添加以session_id為Id的商品數(shù)據(jù)。而用戶退出登錄時(shí)系統(tǒng)會(huì)調(diào)用destroy_session()函數(shù),清購(gòu)物車。函數(shù)部分如下:(cls_session.php文件中)
function destroy_session()
{ .此處省略部分代碼
$this->db->query('DELETEFROM ' . $GLOBALS['ecs']->table('cart') . " WHEREsession_id ='$this->session_id'“ );
此處省略部分代碼
}
所以此時(shí)退出購(gòu)物車就被清空了,下面是本人的解決方法:
說(shuō)說(shuō)思路:
Author:夢(mèng)中的期待*/
Author:夢(mèng)中的期待*/
Author:夢(mèng)中的期待*/
Author:夢(mèng)中的期待*/
具體實(shí)現(xiàn)代碼:
1.cls_session.php
function destroy_session()
{ .此處省略部分代碼
Author:夢(mèng)中的期待*/
$this->db->query('DELETEFROM ' . $GLOBALS['ecs']->table('cart') . " WHEREsession_id = '$this->session_id' AND user_id=''");
此處省略部分代碼}
2.lib.main.php
function update_user_info()
{ 些處省略函數(shù)原來(lái)代碼,直接跳到函數(shù)最后,加上如下代碼:
Author:夢(mèng)中的期待*/
$sql ="update ".$GLOBALS['ecs']->table('cart')." setuser_id =".$_SESSION['user_id']." where session_id ='".SESS_ID."'";
$GLOBALS['db'] -> query($sql);
Author:夢(mèng)中的期待*/
$sql1 ="update ".$GLOBALS['ecs']->table('cart')." setsession_id ='".SESS_ID."' where user_id ='".$_SESSION['user_id']."'";
$GLOBALS['db'] -> query($sql1);
Author:夢(mèng)中的期待*/
$sql2="select distinct(c.goods_id)from".$GLOBALS['ecs']->table('cart')."as c leftjoin"
.$GLOBALS['ecs']->table('goods')."as g onc.goods_id=g.goods_id where g.is_on_sale =0 AND c.user_id ='".$_SESSION['user_id']."'";
$data =$GLOBALS['db'] -> getAll($sql2);
if($data){
foreach($data as $k=>$v){
$sql="deletefrom".$GLOBALS['ecs']->table('cart')."where goods_id= '".$v['goods_id']."'";
$GLOBALS['db'] -> query($sql);
}}}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////分隔線
補(bǔ)充:發(fā)現(xiàn)一個(gè)BUG
在privilege.php中發(fā)現(xiàn)一個(gè)functionclear_cart()用于清理cart中無(wú)效的數(shù)據(jù)的,難怪購(gòu)物車的商品,session一過(guò)期就沒(méi)有了!~
既然我們不需要定期清cart中的無(wú)效數(shù)據(jù),這個(gè)function其實(shí)就沒(méi)有用了,但我們改一下,可以用來(lái)清理己下架的商品,這樣上面.lib.main.php文件中的清理下架商品的部分就可以刪除了!~
修改后的function如下:
function clear_cart()
{
//刪除cart中無(wú)效的數(shù)據(jù)
//$sql ="DELETE FROM " . $GLOBALS['ecs']->table('cart').
// " WHERE session_id NOT " .db_create_in($valid_sess);
//$GLOBALS['db']->query($sql);
$sql2="select distinct(c.goods_id)from".$GLOBALS['ecs']->table('cart')."as c leftjoin"
.$GLOBALS['ecs']->table('goods')."as g onc.goods_id=g.goods_id where g.is_on_sale =0 AND c.extension_code=''";
//.$GLOBALS['ecs']->table('goods')."as g onc.goods_id=g.goods_id ";
$data = $GLOBALS['db'] -> getAll($sql2);
print_r($data);
if($data){
foreach ($data as $k=>$v){
$sql="deletefrom".$GLOBALS['ecs']->table('cart')."where goods_id= '".$v['goods_id']."'";
$GLOBALS['db'] ->query($sql);
}
}
}