我正在使用CI 2.1.3进行购物车项目。这就是add-to-cart的工作原理:调用add_to_cart函数将项目插入购物车,然后重定向到view_cart页面。但购物车没有立即更新。它仅在重新加载页面时更新。为什么呢?
public function add_to_cart($product_id){
if (isset($product_id)){
$product = $this->_product_model->get_record_by_id((int)$product_id);
if (!is_null($product)){
if (count($this->cart->contents())>0){
foreach ($this->cart->contents() as $item){
if ($item['id']==$product->id){
$data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
$this->cart->update($data);
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
}
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
//$this->layout->load('cart/view_cart', $this->data);
redirect(base_url('view_cart.html'),'location');
}
}else{
$this->layout->load('/product/product_not_found', $this->data);
}
}
答案 0 :(得分:0)
试试这个:
redirect(base_url('view_cart.html'),'refresh');