Welcome Guest, Not a member yet? Register   Sign In
Cart Class Not Working For Me
#1

[eluser]Bus Pants[/eluser]
So I am using the cart library, both the cart and session libraries are autoloaded in the config file. My session data is stored in a db table and it is there when I look.

When I add a new item to the cart I am redirected to the cart and the items show up. If I print_r($this->cart->contents()) at this point I get the correct output.

I click the Checkout button which goes to a form you fill out with your credit card info...

if I print_r($this->cart->contents()) at the bottom of the form it still shows up.

When I submit that form it goes to my checkout controller and for testing I just echo the cart info, and at this point my cart is empty but the info is still in the db...

print_r($this->cart->contents()) just echos Array( ) and $this->cart->total() and $this->cart->total_items() both echo as 0.

What could be causing this?

This is my cart controller which successfully sends the cart data to the view:

Code:
class Cart extends CI_Controller {

public function index() {
  
  // load the asset helper access css. scripts and images
  $this->load->helper('asset');
  
  // load gloabal css file into $css variable
  $data['css'] = css_asset('style.css');
  
  // load jQuery library into $jquery variable
  $data['jquery_main'] = js_asset('jquery-1.7.1.min.js');
  
  // set page value for navigation background image toggle
  $data['page'] = "shop";
  
  $data['cart_items'] = $this->cart->contents();
  $data['total'] = $this->cart->total();
  
  // load the head, body and footer view templates
  $this->load->view('header', $data);
  $this->load->view('cart');
         $this->load->view('footer');
  
}
}


on the view I just loop through the cart and it works:

Code:
<?php foreach ($cart_items as $item) {
    echo $item['id'];
    etc.
    etc.
} ?>


But in my Checkout controller if I just echo out the cart data, either with print_r, var_dump or looping through it like above, I get nothing...

Code:
echo "$" . $this->cart->total();
exit;

// just echos $0

Where is my cart data?

What's in the db looks like this:

Code:
a:2:{s:9:"user_data";s:0:"";s:13:"cart_contents";a:3:{s:32:"c4ca4238a0b923820dcc509a6f75849b";a:6:{s:5:"rowid";s:32:"c4ca4238a0b923820dcc509a6f75849b";s:2:"id";s:1:"1";s:3:"qty";s:1:"1";s:5:"price";s:6:"120.00";s:4:"name";s:23:"Calypso Sunnies - Black";s:8:"subtotal";d:120;}s:11:"total_items";i:1;s:10:"cart_total";d:120;}}
#2

[eluser]Bus Pants[/eluser]
I just noticed that when I add something to the cart, two new session rows are created in the database, each with their own session_id and only one of them has cart data...

WTH?

Also my config:

Code:
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;

is the last one, time to update set too low?
#3

[eluser]Bus Pants[/eluser]
And sorry, here's my add to cart script:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Update_cart extends CI_Controller {

public function index() {

  
  // if cart is empty there's nothing to loop
  // through so just add the item to the cart
  if ($this->cart->total_items() == 0) {
  
   $this->add();
  
  }
  else {
  
   $type = $this->input->post('type');
   $id = $this->input->post('id');
  
   foreach($this->cart->contents() as $item) {
    
    if ($item['id'] == $id) {
     $this->update($type, $item['rowid'], $item['qty']);
    }
    else {
     $this->add();
    }
   }
  }
  
}


private function add() {
  
  // capture post variables
  $id = $this->input->post('id');
  $qty = 1;
  $name = $this->input->post('name');
  $price = $this->input->post('price');
  
  $data = array(
   'id' => $id,
   'qty' => $qty,
   'price' => $price,
    'name' => $name
  );
  
  $this->cart->insert($data);
  
}


private function update($type, $rowid, $qty) {
  
  if ($type == "add") {
   $qty = $qty + 1;
  }
  else if ($type = "minus") {
   $qty = $qty - 1;
  }
  else {
   $qty = 0;
  }
  
  $data = array(
   'rowid'      => $rowid,
   'qty'     => $qty
  );
  
  $this->cart->update($data);
  
  echo "success";
}
  
}
#4

[eluser]WanWizard[/eluser]
If you get new session records, it's not a cart issue, it's a session issue.

Testing with IE? Remove the underscore from the session cookie name.
#5

[eluser]Bus Pants[/eluser]
Removed underscore but no change...

Testing in Firefox 12.0 and Chrome whatever is newest.

The timestamp difference on the two sessions is 7 seconds, about the time it takes to fill out the form
so it is making one session var when you add to cart and another when you click checkout....

do I need some sort of if(isset(session)) type check on each page?
#6

[eluser]WanWizard[/eluser]
Also check your cookie sessions.

After you've done a page request, check the HTTP response header to see if the cookie was send, and check if the cookie was saved by the browser. When you request a new page, check the HTTP header again, to see if your browser sends the cookie back to the server.

Due to increased security browsers are less and less flexible when it comes to errors in cookie definitions.

For example, 'localhost' is NOT a valid host for a cookie, and will be rejected by modern browsers.
#7

[eluser]cartalot[/eluser]
* are you changing the base URL for filling out the payment form -- and/or going from http to https ?
#8

[eluser]Bus Pants[/eluser]
[quote author="cartalot" date="1336339716"]* are you changing the base URL for filling out the payment form -- and/or going from http to https ?
[/quote]

Dammit... yes, thank you.

I should have known that as I have been down this road before...

One of my ajax calls was going to http:// thus breaking the chain and starting a new session.

Solved, thanks bro.




Theme © iAndrew 2016 - Forum software by © MyBB