【问题标题】:Is there any way to save cart data when a user logs out?用户注销时有什么方法可以保存购物车数据?
【发布时间】:2017-04-04 23:25:43
【问题描述】:
我在 Codeigniter 中使用购物车会话。我的问题是当买家注销时,购物车会话被破坏。如何为再次登录的客户保留尚未处理的购物车数据?
【问题讨论】:
标签:
codeigniter
shopping-cart
【解决方案1】:
您必须在注销时添加一个过程才能将数据保存到数据库中。
并且,当登录时...从数据库中取出并添加到购物车。
是唯一的解决方案,否则您必须重写整个购物车代码。
我的解决方案:数据库购物车和 cookie :) 所以无论您是否登录。您可以以管理员身份查看购物车中的内容。
当您注销时...您必须添加如下内容:
foreach ($this->cart->contents() as $items){
$this->db->from('temp_cart');
$this->db->set('id_user', LOGGED USER ID);
$this->db->set('cart_row', json_encode($items));
$this->db->insert();
}
登录时
$this->db->select('*');
$this->db->from('temp_cart');
$this->db->where('id_user', LOGGED USER ID);
$res=$this->db->get();
foreach($res->result_array() as $row{
$row=json_decode($row, TRUE);
$this->cart->insert($row);
}
$this->db->from('temp_cart');
$this->db->where('id_user', LOGGED USER ID);
$this->db->delete();
【解决方案2】:
当您注销时...您必须添加如下内容:
foreach ($this->cart->contents() as $items)
{
$this->db->from('temp_cart');
$this->db->set('id_user', $this->session->userdata('customer_id'));
$this->db->set('cart_row', json_encode($items));
$this->db->insert();
}
当您登录时:
$this->db->select('*');
$this->db->from('temp_cart');
$this->db->where('id_user', $this->session->userdata('customer_id'));
$res=$this->db->get();
if($res)
{
foreach($res->result_array() as $row)
{
$row=json_decode($row['cart_row'], TRUE);
$productData = array(
'id' => $row['id'],
'qty' => $row['qty'],
'price' => $row['price'],
'name' => $row['name'],
'options' => array('img_path' => $row['options']['img_path'])
);
$rowId = $this->cart->insert($productData);
}
$this->db->from('temp_cart');
$this->db->where('id_user', $this->session->userdata('customer_id'));
$this->db->delete();
}