Welcome Guest, Not a member yet? Register   Sign In
Basic Cart Example

[eluser]Jesse Schutt[/eluser]

Thanks so much for putting this sample together! It has really helped me get moving with my project.

One question I had for you... Is there a way to remove multiple items from the cart in the same update? Right now, all it will remove is one at a time. If you put zeros in two or more of the QTY fields, only one item will be removed from the cart.

Can you point me in the right direction?

Thanks again for your time!


I believe the problem is with the update cart function. We need to get the total items for the cart outside the for loop function as the total_items() is being called each loop. If you set it to a variable first the total will not change during the loop (as you remove products).

function update_cart()
                $total = $this->cart->total_items();
                for ($i = 1; $i <= $total; $i++)
                        $item = $this->input->post($i);
                        $data = array(
                                'rowid' => $item['rowid'],
                                'qty' => $item['qty']

[eluser]Jesse Schutt[/eluser]
Hey, thanks for jumping in with this! It appears to have worked.

[eluser]Jesse Schutt[/eluser]
Next question...

How would I begin to calculate shipping from the cart? I have the weight for each item loaded into the cart. What I would like to do is be able to calculate the total weight of the order, then multiply it by a specific amount.

I'd appreciate some direction!



Thank You very much.. this is extremely helpful, especially for CI newbies like me Big Grin

Really handy tutorial, thanks Joe!

Some may prefer it if the quantity in the cart is increased when someone adds an item to their cart and that exact same item is already in the cart. I extended the cart class and changed the _insert() method like this:

// Now that we have our unique "row ID", we'll add our cart items to the master array

        // check if a identical item already exists, if so, increment its quantity
        if(array_key_exists($rowid, $this->contents()))
            $this->_cart_contents[$rowid]['qty'] += $items['qty'];
        } else {
            // let's unset this first, just to make sure our index contains only the data from this submission

            // Create a new index with our new row ID
            $this->_cart_contents[$rowid]['rowid'] = $rowid;

            // And add the new items to the cart array
            foreach ($items as $key => $val)
                $this->_cart_contents[$rowid][$key] = $val;

Thanks alot for posting so useful example Smile

hi joe, i wanna ask , how many items are listed in shopping cart? i got problem about that amounts,

when i insert into cart, just 2 items are listed to cart

although my inserted id in cart are different with others, all the data in my database are different , include its ID number (primary key)

its name also not contains special character, just a-Z space A-Z,
its ID just number start from 1 ...( primary key, different with another)

why the third item are not listed to my show cart?

I wish I could quickly give you an answer, but you already guessed the non-alphanumeric character problem as that screws up the cart. This is something that should be fixed in the cart class.

If you have a URL you can post I can look at the cart page and try to figure it out for you.

sorry joe, i have no URL, located at my local
eh, my problem has been solved, but i have a new problem again

latest problem about why listed number were not increase, i got the solution, because, when i was login, i put a lot of session->userdata into CI, in user_guide, its explained that session hold 4KB of data ,
so i have no space to put a new items to shopping cart

now, i have decrease my session from login, but it is same, can hold 4KB of data, just 9-10 items are listed into cart,

do you have solutions how put as many as items into cart ? may be we can use database when booking or do you have right solutions?

thanks you joe Smile

Theme © iAndrew 2016 - Forum software by © MyBB