CodeIgniter Forums
Unable to add more than 10 items to cart - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Unable to add more than 10 items to cart (/showthread.php?tid=53398)



Unable to add more than 10 items to cart - El Forum - 07-22-2012

[eluser]xbonez[/eluser]
I'm testing adding items to the shopping cart like this:

Code:
$item = $this->model->getSingleItem();

for($i = 0; $i < 11; $i++) {
    $this->cart->insert(array(
            'id' => $item->id++,
            'qty' => 1,
            'price' => 1,
            'name' => $item->title
        ));
}

However, the above loop only adds 10 items to the shopping cart. Even if I modify the loop above to run 20 times, I still get only 10 items in the cart. Is this a known bug, or am I doing something wrong?

Doing

Code:
echo count($this->cart->contents());

always shows a count of 10 or less. Never more.

I'm using CI 2.1.2. If someone can confirm they don't face this issue in a previous version, perhaps I can use the cart class from an older version of CI.


Unable to add more than 10 items to cart - El Forum - 07-22-2012

[eluser]skunkbad[/eluser]
If it were me, I'd store the cart in a secondary cookie that is dedicated to the cart, and nothing else. The problem with CI's cookie based session is that it is potentially serializing and encrypting a lot of unrelated data. You can easily run out of space. Using the database is another option, but then you are adding one more query for every pageload, which I think is a waste of a query. If CI was really smart, it would create more cookies to handle the session when the first is too full.


Unable to add more than 10 items to cart - El Forum - 07-22-2012

[eluser]xbonez[/eluser]
Skunkbad, you are right. The issue was being caused because of the cookie size limit. For now, I have switched to storing sessions in DBs but I am not too excited about the additional DB query.

Can you elaborate on the secondary cookie for cart? How can I do that while still using the Cart library?


Unable to add more than 10 items to cart - El Forum - 07-23-2012

[eluser]skunkbad[/eluser]
You would need to extend the cart class to use regular cookies instead of the CI session. Basically, anytime the cookie is created, updated, etc, then you change the code to use a regular cookie. Have you ever extended a class before?