我有一个篮子 cookie,在将产品添加到篮子之前,该函数会检查是否存在 cookie 或是否需要新的 cookie。这在一次添加一个项目时效果很好,但有时会同时添加多个项目。同样,如果存在旧的篮子 cookie,则此方法可以正常工作。问题是当不存在篮子 cookie 并且函数必须创建一个时。
第一次通过时,会创建 cookie,并添加数据库记录等。
第二次我们检查 cookie 的函数没有找到 cookie 并创建了另一个 cookie 等等。
$this->db->select('basket_id');
$this->db->from($this->basket_table);
$this->db->where('basket_id', get_cookie('basket_id'));
$check_basket = $this->db->get();
if($check_basket->num_rows > 0){
$basket_exists = 1;
}else{
$basket_exists = 0;
}
if($basket_exists == 0){
delete_cookie('basket_id');
$basket = array(
'lang_id' => $lang_id,
'currency_id' => $currency_id,
'customer_id' => $customer_id,
);
$this->db->insert($this->basket_table, $basket);
$basket_id = $this->db->insert_id();;
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 60*60*24*30,
'domain' => 'REMOVED'
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
}else{
$basket_id = get_cookie('basket_id');
}