halosinfire Posted May 7, 2013 Share Posted May 7, 2013 I have an online store site with an "Add to Cart" button on each products page. There is no dropbox or input box to select the quantity, so if you wanted two of the item, you would have to click the Add to Cart button twice. Confusing this a little more is that each item has a color option, however, the item ID is the same, and each item has different color options. We'll call the products wingbats for example's sake. From what I've read online, I need to create a session array to store the quantity and the color of the item purchased. I don't have any code to post because none of what I have tried has worked, so it's pretty useless. Here's some pseudo-code though with an example: The user is browsing product #1, color green. So i have those saved in variables from query strings: $id = 1; $color = green; The user presses the Add to Cart button once, so now one green wingbat should be added to the cart. I've tried something like: $_SESSION[$id][$color] += 1; but that didn't work. Then the user wants to buy a blue wingbat, same item id #1. It should add $_SESSION[$id][$color] +=1; with $color now being blue. Can anyone help me? Sorry if this post doesn't make a lot of sense, I'm a beginner when it comes to PHP and I've been going at this problem now for about 16 hours now with no sleep. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
halosinfire Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) For some reason it won't let me edit my above post. Here's what I tried to write afterwards: I've done shopping carts before when each item didn't have any special attributes like color or size, but my current project has a color attribute. In the past I used Session variables named after the product ID to hold the quantity. Like so: /* initialize all quantities to 0 */ for ($i=0; $i <=$maxID; $i++) { if (!isset($_SESSION['product'.$i])) $_SESSION['product'.$i] = 0; } /* keeps a running total of the number of products to be purchased */ for ($j=0; $j<=$maxID; $j++) { $cartCount += $_SESSION['product'.$j]; } and on the products page I have: if (isset($_POST['addtocart'])) { $_SESSION['product'.$id] += 1; header ("Location: products.php?id=$id"); } Then on the checkout page I used a for loop to go through the cart by $id. However now I'm stuck because not only do I need to save the quantity of each product, but also the color, with the item # being the same. Can anyone help me transform the above code into something that will do the same thing, with the added feature of also keeping track of the color ordered? For example, I might purchase one green item #1, and one blue item #1. These have to be shown separately as the color is different, even though the item number is the same. Any help would be greatly appreciated. Sorry again if I didn't explain my problem well, going on 17 hours now! Edited May 7, 2013 by halosinfire Quote Link to comment Share on other sites More sharing options...
halosinfire Posted May 7, 2013 Author Share Posted May 7, 2013 These have to be shown separately as the color is different I'm sorry, the images don't load up for me. Quote Link to comment Share on other sites More sharing options...
ignace Posted May 7, 2013 Share Posted May 7, 2013 (edited) function cart_add($id, $quantity = 1, $color = null) { if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } if ($quantity === 0) { if (isset($_SESSION['cart'][$id])) { unset($_SESSION['cart'][$id]; } return; } if (!isset($_SESSION['cart'][$id])) { $_SESSION['cart'][$id] = compact('id', 'color', 'quantity'); } else { $_SESSION['cart'][$id]['quantity'] += $quantity; } }You can use this like: session_start(); cart_add(1, 5, 'green'); // 5x green cart_add(1, 2); // 7x green cart_add(1, 0); // removed Edited May 7, 2013 by ignace Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.