I have 2 controllers:
cart/
cart/buy
In both show the contents of the library Cart
<tbody>
<?php foreach($this->cart->contents() as $items): ?>
<tr>
<td><?php echo $items['name'] ?></td>
<td>$ <?php echo $this->cart->format_number($items['price']); ?></td>
<td><?php echo $items['qty'] ?></td>
<td>$ <?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
My problem is that the controller buy remains in cache when I add the first item to the cart. I mean, the controller cart/ have 5 items and the controller cart/buy have 1 item. I have to press Ctrl + F5 to see all items
I was able to solve this part to:
function buy()
{
$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
if($this->cart->contents())
{
$this->load->view('web/products/buy_view');
}
else
{
redirect('cart');
}
}
But see, I want to know if there is data in the cart and redirect to another page if the cart is empty.
Apparently if($ this->cart->contents()), "remains in cache", as for example the cart can be filled in cart/ but empty in cart/buy and until I press Ctrl F5 condition was still failing.
Is there any way to fix this, or maybe I'm doing something wrong?
p.d. My add method in cart controller:
function add_item()
{
if($this->cart_model->validate_add_item() == TRUE)
{
redirect('cart');
}
}
My add method in cart model:
function validate_add_item()
{
$id = $this->input->post('producto_id');
$cantidad = $this->input->post('cantidad');
$this->db->select('vNombre, dPrecio');
$this->db->where('iIdProducto', $id);
$query = $this->db->get('product', 1);
if($query->num_rows > 0)
{
foreach ($query->result() as $row)
{
$data = array(
'id' => $id,
'qty'=> $cantidad,
'price' => $row->dPrecio,
'name' => $row->vNombre
);
$this->cart->insert($data);
return TRUE;
}
}
else
{
return FALSE;
}
}
I didnt find any problem in your code. I think its a cache issue as you mentioned so you can try this to clear your cache. This will clear the all previous caches created by codeigniter and also prevent to create cache in future:
$this->load->driver('cache');
$this->cache->clean();
$this->output->cache(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With