4
votes

I'm trying out CodeIgniter's Shopping Cart Library for the first time. I've got all my sessions setup, and when I go to add an item to cart, it appears in $this->cart->contents(); So far so good. The add looks like this:

$data = array( 'id'      => 1,
               'qty'     => 1,
               'price'   => 20,
               'name'    => "Item1");

$rowid = $this->cart->insert($data);

But when I try to add that same item again, it doesn't increase the quantity of that item in the cart. I imagined that performing that same insert would add one more, increasing the quantity of that row to 2, but it doesn't.

If I add a different product, it appears alongside the first. But again, attempts to add another of that product fail to increase the count.

I am certainly missing something. Perhaps you know what it is.

Many thanks!

2

2 Answers

4
votes

I believe that just adding two of the item won't sum them together to get a new quantity.

You can actually update the cart with the new quantity [qty] value. See the "Updating the Cart" section of this page: http://codeigniter.com/user_guide/libraries/cart.html

Note: You will probably need to do the following:

  1. Get the product details using $this->cart->product_options($rowid); or by looping through all the items and finding the correct one with $this->cart->contents();
  2. Get the quantity from that array
  3. Increment the quantity by one
  4. Updating that $rowid with the new quantity value

Hope that helps!

1
votes

I needed something like that in my app a while ago so I edited some CI cart functions. Take a look at this thread.

Codeigniter Shopping Cart

I hope that you can use some of the code from it, at least it will point you in a right direction.