【问题标题】:Is there any way to save cart data when a user logs out?用户注销时有什么方法可以保存购物车数据?
【发布时间】:2017-04-04 23:25:43
【问题描述】:

我在 Codeigniter 中使用购物车会话。我的问题是当买家注销时,购物车会话被破坏。如何为再次登录的客户保留尚未处理的购物车数据?

【问题讨论】:

  • 我已经删除了我的答案,因为它不是正确的答案对不起我认为ShadowElf 是正确的。
  • 来自 CI 文档:购物车库已弃用,不应使用。目前仅保留它是为了向后兼容。 codeigniter.com/userguide3/libraries/…

标签: 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();

【讨论】:

  • hmm 不太明白,能给我一些参考链接或者教程吗?
【解决方案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();
} 

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多