SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: creating a cart

  1. #1
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating a cart

    I want to create a shopping cart for myself. In each step they can add products to the cart, apply coupon etc.. The items on cart were sessioned when taking each step of cart. Finally an array of cart info were sessioned. After login that array was inserted into db as an order then pay. I had problem that after adding items to my cart, on last step if I had not an account, first I had to create an account and when login, sometimes that array session was cleaned and I had to repeat ordering on cart again. So on last step of cart, I did serialize the array and save it to db, after login it checks if the user has a cart session in db, it generates an order for him. Now I want to improve it a better way, can you suggest how would you implement a cart?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    first I had to create an account and when login, sometimes that array session was cleaned and I had to repeat ordering on cart again
    You were destroying the session somehow, I guess, instead of updating the existing session with the fact this is now a registered user logged in. If you fix that you shouldn’t need to store the session data anywhere else temporarily.

  3. #3
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was not destroying the session by command, and actually on my installation I had not such problem, in fact some users of my cart reported to me they have such problem! Anyway what is the best way to handle items on cart?

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Anyway what is the best way to handle items on cart?
    Can you be more specific? If you mean how would I implement a shopping cart I’d probably say I’d use a table called Cart with fields id, userId, productId, and qty.

    I like the idea of using a database instead of sessions because I don’t think it is good to have carts magically empty themselves. I occasionally put things in an Amazon cart, don’t buy them, and then return many weeks later and find it rather delightful that they are still in there.

    In fact you could probably generalize the table I just suggested to handle both registered and unregistered users.

  5. #5
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain more? On each step you'd save them on cart table and on last step you create an order and delete tge info from cart table?

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    OK so, you have a tables that look like this:
    Code:
    User { id, name, dateRegistered ... }
    Product { id, name, price, ... }
    Cart { id, productId, userId, qty }
    1. Unregistered user starts using the site

    A session will be created by virtue of session_start() being called at the top of every page that could need to reference the cart.

    2. Unregistered user adds an product to cart

    Add the productId to the session.

    PHP Code:
    session_start();
    if (isSet(
    $_POST['product-id']) && $_POST['form-id'] === 'cart-add') {
       
    $productId = (int)$_POST['product-id'];
       
    // TODO: validate `$toAddToCart` exists in Product table
       
    if (!isSet($_SESSION['cart'])) {
          
    $_SESSION['cart'] = array(); }
       if (isSet(
    $_SESSION['cart'][$productId])) {
          
    $_SESSION['cart'][$productId]++; }
       else { 
    $_SESSION['cart'][$productId] = 1; } } 
    3. Unregistered user adds same product to cart a second time

    Above code handles that already

    4. Unregistered user attempt to pay

    Bounce the user to the registration page. Only registered users can submit an order.

    5. Unregistered user registers

    Do this however you would normally. Only at the end add this:

    PHP Code:
    if (isSet($_SESSION['cart'])) {
       
    $q 'INSERT INTO `Cart`(`userId`, `productId`, `qty`) VALUES ';
       
    $tuples = array();
       foreach (
    $_SESSION['cart'] as $productId => $qty) {
          
    $tuples[]=  '(' . (int)$userId ', '. (int)$productId ', '. (int)$qty ')'; }
       
    mysqli_query($db$q join(','$tuples)); 
    6. Now registered user adds another product to cart

    Now we can see that the code in 2 has to be expanded to handle the registered users as well.

    PHP Code:
    session_start();
    if (isSet(
    $_SESSION['user'])) { // user is logged in
       /* switching to psuedocode */
       
    If row with this productId already exists // select query required
          
    update rowqty++
       Else
          
    insert row with userIdproductId, and qty of 1
    }
    else { 
    /* code from 2 */ 
    6. Registered user completes order

    SELECT from Cart to see what to charge the user for

  7. #7
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. But what about order info in order table? In your ecample your cart table is yhe same as order table? Or you'd create it when payment is done?

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ah OK. You’ll need two additional tables for that:

    Code:
    Order { id, userId, ... }
    OrderItems { id, orderId, productId, qty }
    When the user places an order copy SELECT from Cart to INSERT INTO Order and OrderItems. Then 'DELETE FROM Cart WHERE userId = ' . (int)$userId


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •