Here is my cart.php page
Code: Select all
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','1');
require_once("connect.php");
?>
<?php
// code to add item to the cart
if(isset($_GET['id'])) {
$found=false;
$i=0;
$id=$_GET['id'];
$product_id=$_GET['pid'];
$product_scent=$_GET['scent'];
$cartOutput="";
// if the cart session is empty
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1) {
$_SESSION["cart_array"] = array();
array_push($_SESSION["cart_array"], array("id" =>$id, "product_id" => $product_id, "scent"=>$product_scent, "quantity" => 1 ));
sort($_SESSION["cart_array"],$id);
}else {
foreach($_SESSION["cart_array"] as $each_item) {
$i++;
// if the item is already in the cart, up the quantity by 1
if ($_SESSION['cart_array'][$i-1]['id']==$id) {
$_SESSION["cart_array"][$i-1]['quantity']++;
// sort the array by item ID
sort($_SESSION["cart_array"],$id);
$found=true;
} // close if
} // close foreach
if($found == false) {
array_push($_SESSION["cart_array"], array("id" =>$id, "product_id" => $product_id, "scent"=>$product_scent, "quantity" => 1 ));
sort($_SESSION["cart_array"],$id);
}
}
header("location: cart.php");
}
?>
<?php
// code to empty the cart
if(isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
/* code to adjust the item quantity in the cart. If the quantity is set to 0, the item will be removed from the cart */
if(isset($_POST['item_to_adjust'], $_POST['quantity']) && ctype_digit($_POST['quantity'])) {
$id = $_POST['item_to_adjust'];
$quantity = intval($_POST['quantity']);
for ($i = 0; $i < count($_SESSION['cart_array']); $i++) {
if ($_SESSION['cart_array'][$i]['id'] == $id) {
if ($quantity == 0) {
unset($_SESSION['cart_array'][$i]);
} else {
$_SESSION['cart_array'][$i]['quantity'] = $quantity;
}
break;
}
}
header("location: cart.php");
exit();
}
?>
<?php
/* code that outputs the cart contents and totals up the cart */
$cartOutput="";
$total="0";
$pp_checkout_btn="";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1) {
$cartOutput= "<tr><td colspan=3 align=center><i><font face=arial color=#999999>Your shopping cart is empty</font></i></td><td></td><td></td></tr>";
} else {
// start Paypal Checkout Button
$pp_checkout_btn .='<form target="_self" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="shopping_url" value="http://www.coppercreekbathandbody.ca/mainProducts.php">
<input type="hidden" name="page_style" value="copperCreek">
<input type="hidden" name="cs" value="0">
<input type=hidden name="cmd" value="_cart">
<input type=hidden name="upload" value="1">
<input type=hidden name="business" value="coppercreekbathandbody@yahoo.ca">';
// start the foreach loop to go through each item in the cart array
$i=0;
foreach($_SESSION["cart_array"] as $each_item) {
$item_id=$each_item['id'];
$prod_id=$each_item['product_id'];
// distiguish between a regular item and a gift box
switch ($prod_id) {
// if it is a $50 gift box
case "11":
$scent_id=substr($each_item['scent'],0,4);
$scent2_id=substr($each_item['scent'],4,4);
$scent3_id=substr($each_item['scent'],8,4);
$scent4_id=substr($each_item['scent'],12,4);
// Get product name and price
$sql=mysql_query("SELECT products.productPrice, products.productName FROM products WHERE productId='$prod_id' LIMIT 1");
while ($row=mysql_fetch_array($sql)) {
$product_name=$row["productName"];
$price=$row["productPrice"];
}
// get Bath Butter Scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent=$row2['scentName'];
}
// get Body Scrub scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent2_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent2=$row2['scentName'];
}
// get Body Mist scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent3_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent3=$row2['scentName'];
}
// get Elite Hand Cream 120mL scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent4_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent4=$row2['scentName'];
}
$subtotal=$price*$each_item['quantity'];
$total=$subtotal + $total;
if(isset ($_SESSION["cart_array"] )) {
// dynamic checkout button assembly
$x=$i+1;
$pp_checkout_btn.='<input type=hidden name="item_name_'.$x.'" value="'.$product_name.'<br>FBB 8oz: '.$scent_id.', Scrub 8oz: '.$scent2_id.', Mist 150mL: '.$scent3_id.', Cream 60mL: '.$scent4_id.'<br> ">
<input type=hidden name="amount_'.$x.'" value="'.$price.'">
<input type=hidden name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
// dynamic cart item display
$cartOutput.="<tr align=center>
<td width=5%><font color='#999999'>".substr($each_item['id'],0,7)."</font></td>
<td width=25%><font color='#999999'>".$product_name."</font></td>
<td width=40% valign=top><font color='#999999'>Foaming Bath Butter 8oz: ".$scent."<br> Body Scrub 8oz: " .$scent2."<br>Body Mist 150mL: ".$scent3."<br>Hand Cream 60mL: ".$scent4."</font></td>
<td valign=center width=5%><font color='#999999'><form action=cart.php method=post>
<input class=box name=quantity type=text size=1 width=3 maxlength=3 onKeyPress='return check_qty(event);' value=".$each_item['quantity'].">
<input type=hidden name='item_to_adjust' value='".$item_id."'/>
<input type=hidden name='name' value='".$product_name."'/>
<br><input class='updateButton' value='Update' type='submit' />
</td>
<td width=5%><font color='#999999'>$".$price.".00</font></td>
<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>
</tr></form>
<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";
$i++;
} // end if statement to see if cart is empty
break;
// if it is a $75 gift box
case "12":
$scent_id=substr($each_item['scent'],0,4);
$scent2_id=substr($each_item['scent'],4,4);
$scent3_id=substr($each_item['scent'],8,4);
$scent4_id=substr($each_item['scent'],12,4);
$scent5_id=substr($each_item['scent'],16,4);
$scent6_id=substr($each_item['scent'],20,4);
// Get product name and price
$sql=mysql_query("SELECT products.productPrice, products.productName FROM products WHERE productId='$prod_id' LIMIT 1");
while ($row=mysql_fetch_array($sql)) {
$product_name=$row["productName"];
$price=$row["productPrice"];
}
// get Bath Butter Scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent=$row2['scentName'];
}
// get Body Scrub scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent2_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent2=$row2['scentName'];
}
// get Body Mist scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent3_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent3=$row2['scentName'];
}
// get Elite Hand Cream 120mL scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent4_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent4=$row2['scentName'];
}
// get Bath Salt 8oz scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent5_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent5=$row2['scentName'];
}
// get Massage Oil 50mL scent
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent6_id' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent6=$row2['scentName'];
}
$subtotal=$price*$each_item['quantity'];
$total=$subtotal + $total;
if(isset ($_SESSION["cart_array"] )) {
// dynamic checkout button assembly
$x=$i+1;
$pp_checkout_btn.='<input type=hidden name="item_name_'.$x.'" value="'.$product_name.'<br>FBB 8oz: '.$scent_id.', Scrub 8oz: '.$scent2_id.', Mist 150mL: '.$scent3_id.', Elite 120mL: '.$scent4_id.', Salt 8oz: '.$scent5_id.', Massage 50mL: '.$scent6_id.'<br>">
<input type=hidden name="amount_'.$x.'" value="'.$price.'">
<input type=hidden name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
// dynamic cart item display
$cartOutput.="<tr align=center>
<td width=5%><font color='#999999'>".substr($each_item['id'],0,7)."</font></td>
<td width=25%><font color='#999999'>".$product_name."</font></td>
<td width=40% valign=top><font color='#999999'>Foaming Bath Butter 8oz: ".$scent."<br> Body Scrub 8oz: " .$scent2."<br>Body Mist 150mL: ".$scent3."<br>Elite Hand Cream 120mL: ".$scent4."<br>Bath Salt 8oz: " .$scent5."<br>Massage Oil 50mL: ".$scent6."</font></td>
<td valign=center width=5%><font color='#999999'><form action=cart.php method=post>
<input class=box name=quantity type=text size=1 width=3 maxlength=3 onKeyPress='return check_qty(event);' value=".$each_item['quantity'].">
<input type=hidden name='item_to_adjust' value='".$item_id."'/>
<input type=hidden name='name' value='".$product_name."'/>
<br><input class='updateButton' value='Update' type='submit' />
</td>
<td width=5%><font color='#999999'>$".$price.".00</font></td>
<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>
</tr></form>
<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";
$i++;
} // end if statement to see if cart is empty
break;
// if it is a clearance/sale item
case "13":
$scent=substr($each_item['scent'],0,4);
$clearId=substr($each_item['id'],2,4);
// Get product name and price
$sql=mysql_query("SELECT * FROM clearance WHERE productId='$clearId' LIMIT 1");
while ($row=mysql_fetch_array($sql)) {
$clearId=$row['productId'];
$product_name=$row["productName"];
$price=$row["price"];
$stock=$row["stock"];
}
# get product scent name
$sql2= "SELECT productscents.scentName FROM productscents WHERE productscents.scentId = '$scent'";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
$scent=$row2['scentName'];
if(isset ($_SESSION["cart_array"] )) {
if($each_item['quantity']>$stock) {
$each_item['quantity']=$stock;
}
$subtotal=$price*$each_item['quantity'];
$total=$subtotal + $total;
// dynamic checkout button assembly
$x=$i+1;
$pp_checkout_btn.='<input type=hidden name="item_name_'.$x.'" value="'.$scent.' '.$product_name.'">
<input type=hidden name="amount_'.$x.'" value="'.$price.'">
<input type=hidden name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
// dynamic cart item display
$cartOutput.="<tr align=center>
<td width=5%><font color='#999999'>".$each_item['id']."</font></td>
<td width=25%><font color='#999999'>".$product_name."<br>* CLEARANCE * </font></td>
<td width=40%><font color='#999999'>".$scent."</font></td>
<td valign=center width=5%><font color='#999999'><form action=cart.php method=post>
<input class=box name=quantity type=text size=1 width=3 maxlength=3 onKeyPress='return check_qty(event);' value=".$each_item['quantity'].">
<input type=hidden name='item_to_adjust' value='".$item_id."'/>
<input type=hidden name='name' value='".$product_name."'/>
<input type=hidden name='scent' value='".$scent."'/>
<br><input class='updateButton' value='Update' type='submit' />
</td>
<td width=5%><font color='#999999'>$".$price.".00</font></td>
<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>
</tr></form>
<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";
$i++;
} // end if statement to see if cart is empty
/*
// update database Stock Quantity
if ($each_item['quantity'] !=0) {
$updateQuantity=$stock-$each_item['quantity'];
mysql_select_db('CopperCreek');
mysql_query("UPDATE clearance SET stock = '$updateQuantity' WHERE productId = '$clearId'");
}
if ($each_item['quantity'] ==0) {
$updateQuantity=$each_item['quantity']+$stock;
mysql_select_db('CopperCreek');
mysql_query("UPDATE clearance SET stock = '$updateQuantity' WHERE productId = '$clearId'");
}
*/
break;
// if it is not a gift box or sale/clearance item
default :
$scent=substr($each_item['scent'],0,4);
// Get product name and price
$sql=mysql_query("SELECT products.productPrice, products.productName FROM products WHERE productId='$prod_id' LIMIT 1");
while ($row=mysql_fetch_array($sql)) {
$product_name=$row["productName"];
$price=$row["productPrice"];
}
// get scent for regular product
$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent' LIMIT 1");
while ($row2=mysql_fetch_array($sql2)) {
$scent=$row2['scentName'];
}
$subtotal=$price*$each_item['quantity'];
$total=$subtotal + $total;
if(isset ($_SESSION["cart_array"] )) {
// dynamic checkout button assembly
$x=$i+1;
$pp_checkout_btn.='<input type=hidden name="item_name_'.$x.'" value="'.$scent.' '.$product_name.'">
<input type=hidden name="amount_'.$x.'" value="'.$price.'">
<input type=hidden name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
// dynamic cart item display
$cartOutput.="<tr align=center>
<td width=5%><font color='#999999'>".substr($each_item['id'],0,7)."</font></td>
<td width=25%><font color='#999999'>".$product_name."</font></td>
<td width=40%><font color='#999999'>".$scent."</font></td>
<td valign=center width=5%><font color='#999999'><form action=cart.php method=post>
<input class=box name=quantity type=text size=1 width=3 maxlength=3 onKeyPress='return check_qty(event);' value=".$each_item['quantity'].">
<input type=hidden name='item_to_adjust' value='".$item_id."'/>
<input type=hidden name='name' value='".$product_name."'/>
<input type=hidden name='scent' value='".$scent."'/>
<br><input class='updateButton' value='Update' type='submit' />
</td>
<td width=5%><font color='#999999'>$".$price.".00</font></td>
<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>
</tr></form>
<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";
$i++;
} // end if statement to see if cart is empty
break;
} // end switch
} // end foreach that goes through each item in the cart
// Finish the Paypal Checkout Button
$pp_checkout_btn .='
<input type=hidden name="return" value="success.php">
<input type=hidden name="rm" value="2">
<input type=hidden name="cbt" value="Return to Store">
<input type=hidden name="cancel_return" value="http://www.coppercreekbathandbody.ca/cancel.php">
<input type=hidden name="currency_code" value="CAD">
<a href=# onclick=submit();><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="center" border=0;"></a></form>';
}
?>
<html>
<script type="text/javascript" src="./js/functions.js"></script>
<body background="#1d1d1d">
<div id="wrapper">
<div id="main">
<table border=0 width=70% cellspacing=0 cellpadding=5 id="carttable" ><form>
<tr>
<td colspan=6 align=center>
<img src="./images/copperCreekLogo.gif">
</td>
</tr>
<tr align=center>
<td width=5%><strong><font color="#cbb659">ID</font></strong></td>
<td width=25%><b><font color="#cbb659">Product Name</font></b></td>
<td width=40%><b><font color="#cbb659">Scent</font></b></td>
<td width=5%><b><font color="#cbb659">Quantity</font></b></td>
<td width=5%><b><font color="#cbb659">Price</font></b></td>
<td width=10%><b><font color="#cbb659">Total Cost</font></b></td>
</tr>
<tr>
<?php echo $cartOutput;?>
</tr>
<tr>
<td colspan=3 align=center><font size=-1 color=#999999>***Update quantity to 0, to remove the item from your cart***</font></td>
<td colspan=2 align=right><font face=arial color="#999999">Total Price:</font></td>
<td align=center><?php echo "$".$total.".00";?></td>
</tr>
<tr>
<td colspan=3>
<center>
<a href="cart.php?cmd=emptycart"><font face=arial color="#cbb659">Empty Cart</a>
</font></center>
</td>
<td colspan=3 valign=center><br>
<center><font face=arial color="#999999"><?php echo $pp_checkout_btn; ?></font></center>
</td>
</tr>
</form>
</table>
</div>
<div id="sidebar">
<?php require ("menu.html");?>
</div>
</div> <!-- End wrapper class -->
</body>
</html>
I also had this in there to adjust the first item in the cart, but took it out:
Code: Select all
<?php
/* code to adjust the first item in the cart. If the quantity is set to 0, the item will be removed from the cart */
if(isset($_GET['item_to_adjust'], $_GET['quantity']) && ctype_digit($_GET['quantity'])) {
$id = $_GET['item_to_adjust'];
$quantity = intval($_GET['quantity']);
for ($i = 0; $i < count($_SESSION['cart_array']); $i++) {
if ($_SESSION['cart_array'][$i]['id'] == $id) {
if ($quantity == 0) {
unset($_SESSION['cart_array'][$i]);
} else {
$_SESSION['cart_array'][$i]['quantity'] = $quantity;
}
break;
}
}
header("location: cart.php");
exit();
}
?>
Now when I add, for example, 4 items to the cart, I can delete 2 of them and then it won't let me delete the last 2 items in the cart.