craigj1303 Posted July 9, 2010 Share Posted July 9, 2010 Hi All I am in desperate need of help with a shopping cart script i've been working on. Here is the background. Basically, I have been working on a site for a furniture company. I have added some products to a database and am now at the stage where I want to add the product to a shopping basket. Here is the script which is called when I add a product to the basket or view the cart: <?php include ('functions.php'); session_start(); @ $new = $_GET['new']; if($new) { //new item selected if(!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); $_SESSION['items'] = 0; $_SESSION['total_price'] ='0.00'; } if(isset($_SESSION['cart'][$new])) $_SESSION['cart'][$new]++; else $_SESSION['cart'][$new] = 1; $_SESSION['total_price'] = calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); } if(isset($_POST['save'])) { foreach ($_SESSION['cart'] as $prodcode => $qty) { if($_POST[$prodcode]=='0') unset($_SESSION['cart'][$prodcode]); else $_SESSION['cart'][$prodcode] = $_POST[$prodcode]; } $_SESSION['total_price'] = calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); } if($_SESSION['cart']&&array_count_values($_SESSION['cart'])) display_cart($_SESSION['cart']); else { echo '<p>There are no items in your cart</p>'; } $target = 'index.php'; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_product_details($new); if($details['category_id']) $target = 'index.php?page=show_category&category_id='.$details['category_id'].'&sort='.$_GET['sort']; } ?> This script is working great with standard products. The variable $_GET['new'] is carried over to the script from the product page and the function "display_cart" displays each product with a little thumbnail of the item and some details in a nice table row where you can alter the qty, delete the item etc. Great.....no problem. Now this is the curve ball! Certain items have "optional finishes". i.e. product number 1234 is available in white, black and red. So I thought, OK, before the user adds that item to the cart they select a finish from a drop down menu and this information can be carried across to the cart script by way of $_POST['finish']. But for the life of me I cannot work out how to display this information in my cart table row alongside the product information. Obviously I can echo the $_POST['finish'] variable which is great for the first item but if I add another product to the cart in a different finish it changes the finish for the first item and every other item as well! Here is the code for the function display_cart: <?php function display_cart($cart, $change = true, $images = 1) { // display items in shopping cart // optionally allow changes (true or false) // optionally include images (1 - yes, 0 - no) echo '<table style="margin-top: 20px;" border = "0" width = "95%" cellspacing = 0> <form action = "index.php?page=show_cart" method = "post"> <tr><th colspan = '. (1+$images) .' bgcolor="#905f36">Item</th> <th bgcolor="#905f36">Finish</th><th bgcolor="#905f36">Price</th><th bgcolor="#905f36">Quantity</th> <th bgcolor="#905f36">Total</th></tr>'; //display each item as a table row foreach ($cart as $prodcode => $qty) { $item = get_product_details($prodcode); echo '<tr>'; if($images ==true) { echo '<td align = left>'; if (file_exists('images/products/small/'.$item['sm_image'])) { $size = GetImageSize('images/products/small/'.$item['sm_image']); if($size[0]>0 && $size[1]>0) { echo '<img src="images/products/small/'.$item['sm_image'].'" '; echo 'width ="'. $size[0]/3 .'" height = "' .$size[1]/3 . '" />'; } } else echo ' '; echo '</td>'; } echo '<td align = "left">'; echo '<a href = "index.php?page=show_product&product='.$prodcode.'">'.$item['description'].'</a>'; echo '</td>'; [u][i][b]echo '<td></td>';[/b][/i][/u] echo '<td align = "center">$'.number_format($item['price'], 2); echo '</td><td align = "center">'; // if we allow changes, quantities are in text boxes if ($change == true) echo "<input type = 'text' name = \"$prodcode\" value = \"$qty\" size = 3>"; else echo $qty; echo '</td><td align = "center">$'.number_format($item['price']*$qty,2)."</td></tr>\n"; } // display total row echo "<tr> <th colspan = ". (3+$images) ." bgcolor=\"#905f36\"> </td> <th align = \"center\" bgcolor=\"#905f36\"> ".$_SESSION['items']." </th> <th align = \"center\" bgcolor=\"#905f36\"> \$".number_format($_SESSION['total_price'], 2). '</th> </tr>'; // display save change button if($change == true) { echo '<tr> <td colspan = '. (2+$images) .'> </td> <td align = "center"> <input type = "hidden" name = "save" value = true> <input type = "image" src = "images/save.png" border = 0 alt = "Save Changes"> </td> <td> </td> </tr>'; } echo '</form></table>'; } ?> It is between the <td></td> cells I have underlined in the above function that I need to contain the optional finish if one has been selected by the client. Would really appreciate your help with this one guys. I've been trying to program a solution for a week now but after much deliberation with associative arrays, loops and $_SESSION variables i'm at the stage where it has taken over my life and I can think of nothing else until I get it resolved and I can't do it by myself! I think the answer lies with $_SESSION variables but i'm at that "staring at the computer screen with a blank expression on my face" stage and I can no longer see the wood for the trees! HELP!! Many thanks Craig Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/ Share on other sites More sharing options...
kumarkiranm Posted July 9, 2010 Share Posted July 9, 2010 did you try to save cart in this format?? [cart] -->[product1] ---->[finish]=>array(black, red) ---->[quantity]=>[2] -->[product2] ---->[finish]=>array() ---->[quantity]=>[1] Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083580 Share on other sites More sharing options...
Adam Posted July 9, 2010 Share Posted July 9, 2010 How do you store the variations in finishes within your DB? Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083582 Share on other sites More sharing options...
kumarkiranm Posted July 9, 2010 Share Posted July 9, 2010 How do you store the variations in finishes within your DB? Ofcrs, he should treat 'product' and 'finish' combination as seperate product with respective quantity of each combi. Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083586 Share on other sites More sharing options...
Adam Posted July 9, 2010 Share Posted July 9, 2010 Not necessarily as a separate product, otherwise you'd have to up-keep two products if any shared detail of it changed. If you've heard the term SKU before it partly relates to having a separate order-able SKU for variations in the same product. You have a main product ID that holds the product's details and then a SKU ID which holds the variation. If there's only 1 version of a product, there'd only be 1 SKU for it. You assign the varied attribute(s) to the SKUs, and then place the order based on the SKU as opposed to product ID. The queries would need to join up the required data to give you the product details. It's obviously a much more complicated architecture. It's up to you if you want to get down that route, you could just write a bit of a hacky work-around. It's for problems like the one you're having though that these designs are used. Edit: Just to add that may be a little over kill for your situation thinking about it. Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083594 Share on other sites More sharing options...
craigj1303 Posted July 9, 2010 Author Share Posted July 9, 2010 Hi MrAdam! As you said in your reply to kumarkiranm I can't store all the products available with different finishes in the products table because they would have to have a different product_id and I would be upkeeping hundreds of extra products in the database. The products are stored in a database table called "products" and the finishes are stored in a seperate table called "finishes". The "products" table contains all the information about the product e.g. part number, weight, width, associated images etc. It also contains a field called 'finish_id' which is integer. I use this field to identify what finishes are available for each product. The finishes available seem to be in groups. If the field is null I know that that item is available in only one finish. If finish_id is "1" the item page displays a drop down menu with 5 options. If finish_id is "2" the item page displays a menu with a different set of options still. The "finishes table" contains an ID for each finish available, the name of the finish, the finish_id field (as in the products table) Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083656 Share on other sites More sharing options...
Adam Posted July 9, 2010 Share Posted July 9, 2010 I see. So different products have a range of finishes available to them? In that case then I'd store the data like this: array( 'prod1' => array( 'fin1', 'fin1', 'fin2', ), 'prod2' => array( 'fin1', ), 'prod3' => array( null // remember you mentioning null values ), ); To get the quantity you can just count the values in the finish array for a particular product. To list the finishes for a particular order just format the finish array in a nice looking way. Quote Link to comment https://forums.phpfreaks.com/topic/207234-adding-item-to-a-cart-with-selected-options/#findComment-1083678 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.