I'm using CI 2.1.3 for my shopping cart project. This is how add-to-cart works : call add_to_cart function to insert item into cart, then redirect to view_cart page. But the cart wasn't updated immediately. It is only updated when the page reloaded. Why?
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);
}
}