Try Tuts+ Premium, Get Cash Back!
CodeIgniter from Scratch: Shopping Cart
videos

CodeIgniter from Scratch: Shopping Cart

Tutorial Details
  • Technology: CodeIgniter
  • Difficulty: Intermediate - Advanced
  • Length: 45 Minutes
This entry is part 12 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

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 12: Shopping Cart

Final Project

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • hQuse

    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/

  • sucheta

    Pls anyone tell me where is the mysql product table details?

    • Madalin

      Sucheta, is right in the beginning of this tutorial. Just watch from 11:40.

  • http://www.urbanvideos.tv alan

    Great tutorial ,thanks a lot…

    I’m just getting in to the CI.

  • http://gravityfx.org/ emcgfx
    • Video Guy

      Nice !

  • Roee

    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

  • iamVincent

    hey jeff i really apreciate your posts here in nettuts you really helped me enjoyed learning php and other web stuff.thanks more power:)

  • siryu

    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!!

  • Mustif

    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,

    • Mustif

      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?

      • Ile

        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.

      • Ile

        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.

      • Ile

        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.

  • PeterGreffen

    Thank you so much for this!
    Kind Regards,
    P. Greffen.

  • anthony

    what of adding products to the db??

  • Myung Ki

    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

    • http://www.phpandstuff.com/ Burak
      Author

      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.

  • http://www.rebatesense.com RebateSense

    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.

  • starchildno1

    I wish cakePHP had it’s own cart lib. Anyway nice tutorial.

  • Dogan

    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.

  • Mageshwar

    quantity should be editable, so that user have facility to change number items and total price updates
    automaticaly.

  • Tim

    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?

    • Tim

      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.

  • mike

    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.

  • Steffen

    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

  • http://webmarmun.com Sasa

    Yeah I also have the same problem I cant add more than 9 products to cart!

    • Loud

      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

  • fatkhur

    no database??

  • Jubic

    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?

    • Jubic

      Ahh just realized, it’s because my products table is empty.

  • jai

    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

  • jai

    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

  • jai

    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

    • Alex

      That happened because you used quotes in your database table.

  • junaid

    hi, i am trying ur tutorial but the product images r not displayed..other product fields are correctly displayed…what could be wrong?

  • http://www.maquinariamadrid.com/p_privado.html maquinaria para hierro

    Hello, Neat post. There is a problem along with your site in web explorer, could check this? IE nonetheless is the market leader and a huge portion of folks will pass over your excellent writing due to this problem.

  • http://www.mandoparking.com/0218eb97e70a75802/index.html puertas automaticas de garaje

    I was suggested this website via my cousin. I am not sure whether this submit is written by him as no one else know such special approximately my trouble. You are wonderful! Thank you!

  • http://www.all-info4you.com Idham

    Thanks, this i need. how can i download the video?

  • Alvaro

    I stopped to drop a comment when I saw how you had built your products table. That way is illegal, you cannot put different values in a cell, that doesn’t even pass 1st Normal Form. You need to create different tables and link them by foreign keys. that goes for colors and sizes.

  • peter

    hello,

    i don’t have access permission to the add function and other functions. When i want to add a product to the cart, the following error message comes:

    Object not found!

    The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

    If you think this is a server error, please contact the webmaster.
    Error 404
    localhost

    What should I do?

  • HarDick

    Video not working :(

  • paul quinn

    the video doesnt work. anywhere for us to download?

  • glenes

    This error occurs. Is there anyone also encounter this problem?

    any help will be appreciated.

    thanks

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.More information about this error may be available in the server error log.