Tutorial Details
- Technology: CodeIgniter
- Difficulty: Intermediate - Advanced
- Length: 45 Minutes
Today, we are going to take a look at the Shopping Cart library for CodeIgniter. This useful class allows us to add and remove items to a shopping cart, update them, and calculate prices. I will demonstrate how you can build a simple shopping cart system with the help of this library..
Catch Up
- Day 1: Getting Started With the Framework
- Day 2: Database Selecting Methods
- Day 3: Sending Emails
- Day 4: Newsletter Signup
- Day 5: CRUD
- Day 6: Login
- Day 7: Pagination
- Day 8: AJAX
- Day 9: File Uploading and Image Manipulation
- Day 10: The Calendar Library
- Day 11: File and Directory Operations
- Day 12: Shopping Cart
Day 12: Shopping Cart


RoyalSlider – Touch-Enable ... only $12.00 
For those who are interested in CI learning (like me) I recommend this [not video] tutorial too http://www.dijexi.com/2009/07/codeigniter-tutorial-creating-accounting-application-part-1-setting-up-the-environment/
Pls anyone tell me where is the mysql product table details?
Sucheta, is right in the beginning of this tutorial. Just watch from 11:40.
Great tutorial ,thanks a lot…
I’m just getting in to the CI.
http://a-mirror.video2.blip.tv/NETTUTS-CodeIgniterFromScratchShoppingCart141.flv
Nice !
Hi Guys.
Great tuts. I am becoming a fan of CodeIgniter.
I have some problems on this one when adding a lot of products to my cart (more than 4).
It stops adding to the cart list.
Any ideas?
Thanks,
Roee
hey jeff i really apreciate your posts here in nettuts you really helped me enjoyed learning php and other web stuff.thanks more power:)
Hi Jeff and Burak, Thx for the tutorial, but I have 1 question, I wonder how to get the cart contents and store it to my database.
can you or anybody tell me how??? Thx b4.
Nettuts Rocks!!
Thanks guys for the Tut!
I’m having an issue with add() function. It seems to override the object with corresponding values each time I submit, instead of creating a new object with a new ref number, if think?
Does anyone know why this would be happening and what I need to do to create a new object each time I try to add two of the exact same item.
Thanks,
Actually I would just like to increase the quantity value not overwrite it each time I want to add more of the same item to the cart.
Anyone have any suggestions?
I have a solution for adding two or more of the same pieces to your shopping cart by just pushing to Add to Cart button again. But this code to your controller before the first code line at the add function.
$this-load-model(products_model);
$product = $this-products_model-get($this-input-post(id));
$qty = 0;
if($cart = $this-cart-contents())
{
foreach($cart as $item)
{
if($item[id] == $this-input-post(id))
{
if(isset($item[options]))
{
if($this-input-post(Size) != NULL)
{
if($product-option_values[$this-input-post(Size)] == $item[options][Size])
{
$qty = $item[qty];
}
}
elseif($this-input-post(Color) != NULL)
{
if($product-option_values[$this-input-post(Color)] == $item[options][Color])
{
$qty = $item[qty];
}
}
else
{
$qty = 0;
}
}
else
{
$qty = $item[qty];
}
}
}
}
You can also delete the two code lines after this snippet because loading the model getting the products is already done at the beginning.
Its not beautiful solution but it does work. Things get complicated because of the options for products. Otherwise it would be a simple task. I guess it would be better to build the database differently, but I just wanted to make it work with this particular database structure.
Sorry, forgot one thing..Your $insert should look like this:
$insert = array(
id = $this-input-post(id),
qty = $qty + 1,
price = $product-price,
name = $product-name
);
So that it increases quantity by one every time you make an new insert.
Thank you so much for this!
Kind Regards,
P. Greffen.
what of adding products to the db??
Hi Burak – I’m a huge fan of CI and the Tutorials here on Nettuts…
I’m about to do my very first ci app – but got stuck, ’cause i can’t figure out, how to convert mysql date -> into eurodate….
Please help me out…..
Thanx – Myung Ki
You can convert a mysql date into a timestamp with either UNIX_TIMESTAMP() (in mysql) or strtotime() (in PHP). And then format it as you wish using the PHP’s date() function.
Burak – I would like to see a tutorial on how to take payments with CI especially PayPal. Given the querystring hack and bunch of other considerations I would think that would make up a good article. There aren’t many articles available on that topic.
I wish cakePHP had it’s own cart lib. Anyway nice tutorial.
Hi, Burak,
it’s very nice article, but it sometimes doesn’t work, if the cart contets, for example name has any turkish chars, it doesn’t work.
for your information.
quantity should be editable, so that user have facility to change number items and total price updates
automaticaly.
I have a question in regards to the cart output to the screen. My options are showing as :
0:black
0:brown
It lists the array key ($option in this case) rather than “Color”.
Any suggestions?
Nevermind. I had an error in my add() method in my shop controller.
I had:
$product->option_name = $product->option_values[…
Where it should be:
$product->option_name => $product->option_values[…
Note the => assignment operator.
thanks burak, brilliant tutorial. i’ve learnt a lot. easy to follow and melodious voice. i have built a codeigniter web site using ‘professional codeigniter’ by thomas myer, which is also a very good tutorial, but it was published before cart class was introduced. i have now redone it using the cart class from your tutorial and it works fine and i’ve put in some new additions, such as adding more of the same product (using the code from one of the comments on this site). however, i now want to have more than one attribute per product, for example a t-shirt with both colour and size options and i’m in difficulty writing the code. it makes me realise how complex things can be working in the dark, and without a good tutorial to light the way. thumbs up to you and thanks.
This tut is great, and easy to implement. – Thanks a lot.
I have a small problem never the less.
I can’t add more than 9 different products to my shopping cart.
Is there some setting than im missing somewhere in the cart-helper?
Hope you can help me.
Best regards,
Steffen
Yeah I also have the same problem I cant add more than 9 products to cart!
Hi!
I just realized this problem, but i figured out what’s coused this. This cart use the session to store the cart items, but the session basically uses cookie’s to store the data. But the cookies maximum size is 4KB. So it’s only 8-9 item. But if you set that the session saves the information to database, then the data size could up to 64KB /cause the data tag is TEXT type/. I hope this could help you to achive your goals.
This is the link to save session to database: http://codeigniter.com/user_guide/libraries/sessions.html
no database??
Hi Burak,
I seem to have a little problem over here:
In the Shop Controller, whenever I try to run the function add, it seems to always give me an Undefined offset: 0 in models/Products_model.php and other whole bunch of errors. Any solutions?
Ahh just realized, it’s because my products table is empty.
i am just learning codeigniter….i get whats wrong in this code
like when i click “add to cart ” it only passing the last element in database …..
Example :
id name qty price
1 jai 2 20,00
2 rdf 1 30.00
when i click on any add to cart button …only id =”2″ is posted .
this is my view file code
id;?>
name;?>
qty;?>
price;?>
id); ?>
please help me thank you
i am just learning codeigniter….i get whats wrong in this code
like when i click “add to cart ” it only passing the last element in database …..
Example :
id name qty price
1 jai 2 20,00 [add to cart]
2 rdf 1 30.00 [add to cart]
when i click on any add to cart button …only id =”2″ is posted .
this is my view file code
id;?>
name;?>
qty;?>
price;?>
id); ?>
please help me thank you
i am just learning codeigniter….i not getting whats wrong in this code
like when i click “add to cart ” it only passing the last element in database …..
Example :
id name qty price
1 jai 2 20,00 [add to cart]
2 rdf 1 30.00 [add to cart]
when i click on any add to cart button …only id =”2″ is posted .
this is my view file code
?php foreach($records as $row):?
li
?php echo form_open(‘index.php/dataex/add’);?
?php echo $row->id;?>
?php echo $row->name;?
?php echo $row->qty;?>
?php echo $row->price;?
?php echo form_hidden(‘id’, $row->id); ?
?php echo form_submit(‘action’, ‘Add to Cart’); ?
php form_close();?
/li
?php endforeach;?
please help me thank you
hi, i am trying ur tutorial but the product images r not displayed..other product fields are correctly displayed…what could be wrong?