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

Add Comment

Discussion 100 Comments

Comment Page 2 of 2 1 2
  1. hQuse says:

    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/

  2. sucheta says:

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

  3. alan says:

    Great tutorial ,thanks a lot…

    I’m just getting in to the CI.

  4. Roee says:

    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

  5. iamVincent says:

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

  6. siryu says:

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

  7. Mustif says:

    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 says:

      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 says:

        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 says:

        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 says:

        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.

  8. PeterGreffen says:

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

  9. anthony says:

    what of adding products to the db??

  10. Myung Ki says:

    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

    • Burak says:
      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.

  11. RebateSense says:

    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.

  12. starchildno1 says:

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

  13. Dogan says:

    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.

  14. Mageshwar says:

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

  15. Tim says:

    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 says:

      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.

  16. mike says:

    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.

  17. Steffen says:

    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

  18. Sasa says:

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

    • Loud says:

      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

  19. fatkhur says:

    no database??

  20. Jubic says:

    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?

  21. jai says:

    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

  22. jai says:

    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

  23. jai says:

    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

  24. junaid says:

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

Comment Page 2 of 2 1 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.