SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Shopping Cart with user selectable options

    I'm trying to find/learn how to code a simple shopping cart that allows for the site visitor to select an item to add to the cart and configure additional options about that product that may or may not increase the price.

    For example, if it was a t-shirt, color and size could be dropdowns with choices and would have different values associated with them.

    I started by trying to modify the Watchmaker Project, but I'm getting all tangled up in how to store the option choices and reflect them in the cart. It relies on unique products, where I would have a product with many choice combinations.

    I've thought about coming up with a code system to feed all the data through the ID when it's added to the cart and then doing all the necessary database lookups to pull the size and color info, and I guess that's an option.

    I'd really like to see if there is someone out there who has tackled this and to learn how to code it. I'm certainly new to PHP, so it's a big learning curve.

    Oh, and I have no need for a checkout method at this time, just a cart that I can dump in an email or order database.

    Thanks for any help!

  2. #2
    SitePoint Wizard bronze trophy bluedreamer's Avatar
    Join Date
    Jul 2005
    Location
    Middle England
    Posts
    3,394
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Many carts do this already using product options... the store owner can set up as many options as needed for the product and assign price modifiers for certain options.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply. Yes, I know many offer this. They are, however, typically overkill for my needs and I would prefer code that is simple, as I have to integrate into what exists.

    I'm looking for specific examples and code that I can look at that easily handle product options in the cart. Or advice on methods, like an object in the session, etc.

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your cart is simple $_SESSION variable
    So, if you want more options - just add more variables.
    e.g.
    $_SESSION['cart'][0]=array(
    'id'=555;
    'color'='green';
    'size'='XXXXL';
    'qty'=2;
    );

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    Your cart is simple $_SESSION variable
    So, if you want more options - just add more variables.
    e.g.
    $_SESSION['cart'][0]=array(
    'id'=555;
    'color'='green';
    'size'='XXXXL';
    'qty'=2;
    );
    Two comments on your code
    1> no proper wrapping
    2> invalid code

    Corrected:
    PHP Code:
    $_SESSION['cart'][0]=array(
    'id'=>555,
    'color'=>'green',
    'size'=>'XXXXL',
    'qty'=>2
    ); 

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    PHP Code:
    $_SESSION['cart'][0]=array(
    'id'=>555,
    'color'=>'green',
    'size'=>'XXXXL',
    'qty'=>2
    ); 
    So I would just foreach through $_SESSION['cart'] and do whatever I needed to do, like display them, add, update, remove?

    So a simple display might look like this?

    PHP Code:
    foreach ($_SESSION['cart'] as $key => $value){
    echo 
    "$key is set to $value<br />";


  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matt1dmb View Post
    so I Would Just Foreach Through $_session['cart'] And Do Whatever I Needed To Do, Like Display Them, Add, Update, Remove?

    So A Simple Display Might Look Like This?

    PHP Code:
    foreach ($_session['cart'] As $key => $value){
    Echo 
    "$key Is Set To $value<br />";

    No

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope.
    because $value itself is array, you cannot just echo it
    access it's elements by key, like
    PHP Code:
    foreach ($_SESSION['cart'] as $key => $value){
      echo 
    "$key has choosen amount of $value[qty]<br />";


  9. #9
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    nope.
    because $value itself is array, you cannot just echo it
    access it's elements by key, like
    PHP Code:
    foreach ($_SESSION['cart'] as $key => $value){
      echo 
    "$key has choosen amount of $value[qty]<br />";

    Now you are a senior user
    Why are you always breaking the rule of thumb (wrap up code with proper highlighter)

    PHP Code:
    foreach ($_SESSION['cart'] as $key => $value){
      echo 
    "$key has choosen amount of $value[['qty']<br />";


  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Shrapnel. I thought it looked too simple and figured I would need another foreach.

    Forgive me as I don't have anything to test this with right now to prove, but what about

    PHP Code:
    foreach ($_SESSION['cart'] as $key => $value){
      echo 
    "Item $key has <br />";
      foreach (
    $value as $option => $val){
        echo 
    "$option is $val
      }


  11. #11
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes it can be this way.

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So using the following code

    PHP Code:
    $_SESSION['cart'][0]=array(

    'id'=>555,

    'color'=>'green',

    'size'=>'XXXXL',

    'qty'=>2

    ); 
    What if I wished to add $_POST fields to this array that matched a certain string as their key, and I did not know how many I would be adding?

    I'm trying to build a shopping cart that has a variable number of options that could be submitted in the form. Some of the fields will be standard though.

    Am I right that I would add to the cart session variable as follows for the standard fields:

    PHP Code:
    $_SESSION['cart'][]=array(
    'id'=>$_POST['ID'],
    'color'=>$_POST['color'],
    'size'=>$_POST['size'],
    'qty'=>$_POST['qty']
    ); 
    Now I have have $_POST['option1'], $_POST['option2'], and $_POST['option3'] that I need to insert into the same key that was created for id, size, etc. above.

    Something like this perhaps?
    PHP Code:
    // for each $_POST that's an option, insert into array
    foreach ($_POST as $key => $value) {
        if(
    strstr($key'option')){
            
    // add to established cart from above
            // $_SESSION['cart'][?][$key] = $value
        
    }


  13. #13
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can simply put whole $_POST array into session
    PHP Code:
    $_SESSION['cart'][]=$_POST
    but there are difficulties with another adding. dunno if it important

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There may be $_POST keys and values I don't want to go into the cart, but are there for other purposes.

    Perhaps an exclusionary approach? Like for each $_POST, if it is a field I care about, add it to $cart_update, and then $_SESSION['cart'][]=$cart_update?

  15. #15
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matt1dmb View Post
    There may be $_POST keys and values I don't want to go into the cart, but are there for other purposes.

    Perhaps an exclusionary approach? Like for each $_POST, if it is a field I care about, add it to $cart_update, and then $_SESSION['cart'][]=$cart_update?
    just do:
    PHP Code:
    unset($_POST['field_that_you_dont_want']); 

  16. #16
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    just do:
    PHP Code:
    unset($_POST['field_that_you_dont_want']); 
    But then I have to complete all my other processing using $_POST[] before removing them, which may or may not be possible at that point in the code.

    I guess what I'm truly asking for is if there is any way to return the current array key to use in a sub-routine to add additional key=>values, like this code below where I have the question mark... Is there any way to know what that question mark should be?

    PHP Code:
    // for each $_POST that's an option, insert into array

    foreach ($_POST as $key => $value) {

        if(
    strstr($key'option')){

            
    // add to established cart from above

            // $_SESSION['cart'][?][$key] = $value

        
    }



  17. #17
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are going just wrong way.
    don't put it into session directly.
    make it temporary array
    PHP Code:
    foreach ($_POST as $key => $value) {
        if(
    strstr($key'option')){
            
    $tmp[$key] = $value
        
    }

    $_SESSION['cart'][]=$tmp
    but i doubt it will help you anyway

  18. #18
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate the help. I got wrapped around the axle looking for something a bit more elegant. If I didn't have to use memory, I didn't want to. But it doesn't look like there is anything that would solve it the way I was looking to.

    The following would complete your code to meet my objective:

    PHP Code:
    foreach ($_POST as $key => $value) {
        if(
    strstr($key'option')){
            
    $tmp[$key] = $value
        
    }

    $tmp['ID'] = $_POST['ID'];
    $tmp['color'] = $_POST['color'];
    $tmp['size'] = $_POST['size'];
    $_SESSION['cart'][]=$tmp
    Thanks again!


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
  •