heartonsleeve Posted January 16, 2007 Share Posted January 16, 2007 I'm working on setting up a shopping cart. I've got a class that was in a book, but I'm trying to modify it to allow products to have attributes.For example, I've got a t-shirt with sizes small, medium, large, and x-large. If I add a size small, everything is fine and it says the item that I have in size small. However, if I go back and add a medium, it just says that I have 2 smalls.Here's the shopping cart class:[code]<?phpclass ShoppingCart{ // Get the cart id // If one is not available, make one. function get_cart_id(){ if(!isset($_COOKIE['cid'])){ $cart_id = FALSE; } else { $cart_id = $_COOKIE['cid']; $_SESSION['cid'] = $_COOKIE['cid']; } if($_SESSION['cid']){ $cart_id = $_SESSION['cid']; if($_SESSION['login']){ @mysql_query("UPDATE members SET cart_id='$cart_id' WHERE id='".$_SESSION['userid']."'"); } } else { $cart_id = FALSE; } if(!$cart_id){ return FALSE; } else { return $cart_id; } } function cart_add($product_id, $product_qty, $shop, $size){ $cart_id = $this->get_cart_id(); if(!$cart_id){ // if no cart id found, generate one $unique_cid = md5(uniqid(rand(),1)); // set cart id into the cookie setcookie('cid', $unique_cid, time()+24*3600*60); // Register session with cart id value $_SESSION['cid'] = $unique_cid; // if person is a member // modify their profile with // cart id in the database if($_SESSION['login']){ $_SESSION['cid'] = $unique_cid; @mysql_query("UPDATE members SET cart_id='$unique_cid' WHERE id='".$_SESSION['userid']."'"); } } $sql_get_product = mysql_query("SELECT * FROM shopping_products WHERE product_id='$product_id'"); $sql_check = mysql_query("SELECT * FROM shopping_carts WHERE cart_identifier='{$_SESSION['cid']}' AND product_id='$product_id'"); while($row = mysql_fetch_array($sql_check)){ $products = mysql_fetch_assoc($sql_get_product); if(($product_qty + $row[product_qty]) > $products[product_qty]){ $new_qty = $products[product_qty]; } else { $new_qty = ($product_qty + $row[product_qty]); } $sql = mysql_query("UPDATE shopping_carts SET product_qty = '$new_qty', date = now() WHERE id='{$row['id']}'"); $skip = TRUE; } if(!$skip){ $products = mysql_fetch_assoc($sql_get_product); if($products[product_qty] < $product_qty){ $product_qty = $products[product_qty]; } if($product_qty > 0){ $sql = mysql_query("INSERT INTO shopping_carts (cart_identifier, product_id, product_title, product_qty, product_price, shop, size, date) VALUES ('{$_SESSION['cid']}', '$product_id', '$product_title', '$product_qty', '{$products['product_price']}', '{$products['shop']}', '$size', now())"); } else { $sql = FALSE; } } if(!$sql){ return FALSE; } else { return TRUE; } } function empty_cart(){ $cart_identifier = $this->get_cart_id(); $sql = @mysql_query("DELETE FROM shopping_carts WHERE cart_identifier='$cart_identifier'"); if(!$sql){ return FALSE; } else { return TRUE; } }}?>[/code]I get the feeling that it's only doing this because it's reading the product_id but I don't know how to change it and I've been trying for way too long. Please help. Quote Link to comment Share on other sites More sharing options...
heartonsleeve Posted January 16, 2007 Author Share Posted January 16, 2007 Here's some other stuff, if that helps.The form for the product[code]<h3><?=$product_title?></h3><div align="center"><img style="padding: 10px; border: 1px solid #4d4d4d;" src="http://www.indie-threads.com/store_images/<?=$main_image?>" alt="<?=$product_title?> by <?=$shop?>" /></div><br /><div style="width: 100%; "><div style="float: right; width: 210px;"><? if ($brand == aa){?><img src="http://www.indie-threads.com/store_images/aasizing.gif" alt="American Apparel Sizing Chart" /><? }?></div><div style="float: left; width: 50%;"><h2><strong>Description</strong></h2><?=$long_description?><p><font style="font-size: 10px;">Sold by Indie Threads | Shipped by <a href="http://www.indie-threads.com/users/<?=$shop?>"><?=$shop?></a></font></p></div></div><p><div align="center" style="clear: both;"> <form method="post" action="/cart.php"> <h3><strong>$<?=number_format($product_price,2)?></strong> <select name="size"> <? $attributes = mysql_query("SELECT * FROM product_attributes WHERE product_id = $product_id"); while($row = mysql_fetch_array($attributes)){ stripslashes(extract($row)); echo "<option>$attribute</option>"; } ?> </select> </h3> <input type="hidden" name="qty" value="1" /> <input type="hidden" name="req" value="add" /> <input type="hidden" name="product_id" value="<?=$product_id?>" /> <input type="hidden" name="shop" value="<?=$shop?>" /> <input type="image" src="store_images/add2cart.gif" value="buy me!" /> </form></div></p>[/code]And the cart.php which processes the form.[code]<?phpinclude $_SERVER['DOCUMENT_ROOT']. '/test_layout.php'; $cart = &new ShoppingCart;$cart_id = $cart->get_cart_id();switch($_REQUEST['req']){ case "add": $add2cart = $cart->cart_add($_REQUEST['product_id'], $_REQUEST['qty'], $_REQUEST['shop'], $_REQUEST['size']); myheader("Shopping Cart"); include('featured_shop.php'); ?> <div class="words"> <? if(!$add2cart){ echo "<h3>Woops!</h3> The product could not be ". "to your shopping cart. You may ". "have entered an invalid quantity</center>"; } else { header("Location: /cart.php"); } ?> </div> <? footer(); break; case "update": while(list($product_id, $qty) = each($_POST[qty])){ $sql = mysql_query("SELECT * FROM shopping_products WHERE product_id='$product_id'"); $row = mysql_fetch_assoc($sql); if($qty == 0){ mysql_query("DELETE FROM shopping_carts WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); } if($qty > $row[product_qty]){ mysql_query("UPDATE shopping_carts SET product_qty='{$row[product_qty]}' WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); $error = TRUE; $products[$product_id] = stripslashes($row[product_title]); } else { mysql_query("UPDATE shopping_carts SET product_qty='$qty' WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); } } if($error){ myheader("Shopping Cart"); include('featured_shop.php'); ?> <div class="words"> <? echo "<center>You have selected more ". "than our current stock for the following ". "product(s): <br />"; while(list($product_id, $product_name) = each($products)){ echo "<a href=\"/products.php?req=view&product_id=$product_id\">". "$product_name</a><br />"; } echo "<br />"; echo "We have updated your quantity to the maximum ". "value that we have in stock.</center><br />"; echo "<center><a href=\"/cart.php\">Back to Cart</a></center>"; ?> </div> <? footer(); } else { header("Location: /cart.php"); } break; case "remove": $sql = mysql_query("DELETE FROM shopping_carts WHERE cart_identifier='$cart_id' AND product_id='{$_REQUEST['product_id']}'"); header("Location: /cart.php"); break; case "empty_confirm": myheader("Shopping Cart"); include('featured_shop.php'); ?> <div class="words"> <? echo "<center>Are you sure ". "you want to empty your cart?<br />". "<a href=\"/cart.php?req=empty\">Yes</a>". " | ". "<a href=\"/cart.php\">No</a></center>"; ?> </div> <? footer(); break; case "empty": myheader("Shopping Cart"); include('featured_shop.php'); ?> <div class="words"> <? $cart->empty_cart(); echo "<center>Your cart has been emptied!</center>"; ?> </div> <? footer(); break; default: myheader("Your Shopping Cart"); include('featured_shop.php'); ?> <div class="words"> <? if($cart_id){ $num_items = mysql_result(mysql_query("SELECT COUNT(*) as items FROM shopping_carts WHERE cart_identifier='$cart_id'"),0); if($num_items == 0){ echo "<center>Your Shopping Cart is Empty!</center>"; footer(); exit(); } } else { echo "<center>Your Shopping Cart is Empty!</center>"; footer(); exit; } ?> <h3>Your Shopping Cart</h3> <div align="center" style="font-size: 11px; margin-bottom: 5px;"> This page allows you to modify or empty your shopping cart contents. Simply change the number of each product you wish to purchase and select the "Update Cart" link at the bottom.</div> <form name="update" method="post" action="/cart.php"> <input type="hidden" name="req" value="update"> <table width="90%" border="0" cellspacing="0" cellpadding="4" align="center"> <tr> <td><h2>Qty</h2></td> <td><h2>Product</h2></td> <td align="right"><h2>Price</h2></td> <td align="right"><h2>Product Total</h2></td> </tr> <?php $total = mysql_result(mysql_query("SELECT sum(product_qty * product_price) AS subtotal FROM shopping_carts WHERE cart_identifier='$cart_id'"),0); $total = number_format($total, 2); $sql = mysql_query("SELECT * FROM shopping_carts WHERE cart_identifier='$cart_id'") or die (mysql_error()); while($row = mysql_fetch_array($sql)){ $product_total = number_format(($row[product_qty] * $row[product_price]),2); echo "<tr>". "<td>". "<input type=\"text\" name=\"qty[$row[product_id]]\" size=\"2\" value=\"$row[product_qty]\">". "<br /><font size=\"2\">". "<a href=\"/cart.php?req=remove&product_id=$row[product_id]\">Remove</a>". "</td>". "<td><a href=\"/products.php?req=view&product_id=$row[product_id]\">" .stripslashes($row[product_title]). "</a> by ".$row['shop']." - ".$row['size']."</td>". "<td align=\"right\">\$".number_format($row[product_price], 2)."</td>". "<td align=\"right\">\$$product_total</td>". "</tr>"; } ?> <tr> <td colspan="2"> </td> <td align="right">Total:</td> <td align="right">$<?=$total?></td> </tr> <tr> <td colspan="4" align="center"> <a href="javascript:void(document.update.submit())">Update Cart</a> | <a href="/cart.php?req=empty_confirm">Empty Cart</a> | <a href="/products.php">Continue Shopping</a> | <a href="/checkout.php">Checkout</a> </td> </tr> </table> </form> </div> <?php footer(); break;}?>[/code]...anything? 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.