John
John

Reputation: 41

Adding to cart in Codeigniter shopping cart class

thanks for taking the time to read this. Ive been using the shopping cart class from code igniter for a basic cart, but im having one small issue. After adding an item to a cart I redirect the user to a checkout page, but when I click back on the browser the item gets removed. I know this because I have <?php echo anchor('cart','<strong>'.$this->cart->total_items(). '</strong> item(s)') ?> in the header, and it decrements when going back. This is really annoying and I would like to fix it.

this is the controller that process the form

public function process () {
if($this->input->post('submit')) {
    $product = $this->products_model->getProductRow($this->input->post('productid'));

    $data = array(
    'id'      => $product['id'],
    'qty'     => 1,
    'price'   => $this->product_helper->calcPrice($product['id']),
    'name'    => $product['name']
    );

    $this->cart->insert($data);
    redirect('cart');
    //have tried using redirect('cart', 303); but doest do anything
    //have also tried flusing the buffer
}           
else
    redirect('seatcovers');}

Is there something trivial I am missing here, or is this something that needs to be changed within the cart class of CI?

Many thanks

Upvotes: 4

Views: 3470

Answers (2)

stalin
stalin

Reputation: 3464

I know is a little old but i had the same problem, the problem is that the library have a regular expression for limit the item name

class CI_Cart {

    // These are the regular expression rules that we use to validate the product ID and    product name
    var $product_id_rules   = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
    var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods

change that or create your own custom cart library

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Cart extends CI_Cart
{
    function __construct()
    {
        parent::__construct();
        $this->product_name_rules = '\d\D';
    }
}

i found the solution in here http://darrenonthe.net/2011/05/03/cant-add-products-to-codeigniter-shop-cart-class/?

Upvotes: 1

Jason Lewis
Jason Lewis

Reputation: 18665

Important: The Cart class utilizes CodeIgniter's Session Class to save the cart information to a database, so before using the Cart class you must set up a database table as indicated in the Session Documentation , and set the session preferences in your application/config/config.php file to utilize a database.

I assume that you've done that as well? The only suggestion I have for you is remove that redirect, attempt to navigate to another page then navigate back to see if it maintains the correct number.

Also, you say you're using the browsers back button. Have you tried refreshing the page to see if it's not using a browser cached copy?

Upvotes: 0

Related Questions