Page 3 of 3
Re: Can't adjust qty or remove first item in the cart
Posted: Thu Mar 31, 2011 6:19 pm
by mrzebra
Yeah I changed it to a button. I'm testing it on the localhost and haven't uploaded the changes to the site yet. I copied the code you gave for adjusting the item, and changed it from $_POST to $_GET, then added it to my cart page as another php chunk of code and it seems to be working. It changes all the items in the cart. I'm just not sure how I can combine the two chunks of code into 1.
Code: Select all
<?php
/* code to adjust the first item in 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();
}
?>
<?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();
}
?>
I also updated the webpage so it has the "update" submit button instead of the text link. I also took out the second block of code where I changed your code from $_POST to $_GET so you can see that the first item won't adjust or remove.
Re: Can't adjust qty or remove first item in the cart
Posted: Fri Apr 01, 2011 11:10 am
by Jonah Bron
That's not the solution we want. That's a hack, not a fix to the problem. This is strange indeed. Since you've made some changes, could you re-post the entire cart.php code?
Re: Can't adjust qty or remove first item in the cart
Posted: Fri Apr 01, 2011 11:17 pm
by mrzebra
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.
Re: Can't adjust qty or remove first item in the cart
Posted: Sat Apr 02, 2011 1:00 pm
by Jonah Bron
Delete this:
Code: Select all
header("location: cart.php");
exit();
And this:
And see what that does.
Re: Can't adjust qty or remove first item in the cart
Posted: Sat Apr 02, 2011 8:02 pm
by mrzebra
I removed them, but when I added items to my cart the quantities went up by 3 for all items except the item I just added. I tried cleaning up my "add item to cart code" to see if that helped but it never, so i had to put the header("location: cart.php") back in. Here is my updated add to cart code:
Code: Select all
<?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(1=>array("id" =>$id, "product_id" => $product_id, "scent"=>$product_scent, "quantity" => 1 ));
} else {
foreach($_SESSION["cart_array"] as $each_item) {
if ($each_item['id']==$id) {
$_SESSION['cart_array'][$i]['quantity']++;
$found=true;
}
$i++;
} // 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"],$product_id);
}
}
header("location: cart.php");
}
?>
Re: Can't adjust qty or remove first item in the cart
Posted: Thu Apr 07, 2011 11:08 pm
by Jonah Bron
I think I just figured out your problem. Try changing this:
Code: Select all
$_SESSION["cart_array"] = array();
array_push($_SESSION["cart_array"], array("id" =>$id, "product_id" => $product_id, "scent"=>$product_scent, "quantity" => 1 ));
Re: Can't adjust qty or remove first item in the cart
Posted: Wed Apr 13, 2011 6:50 pm
by mrzebra
Nope, that still won't allow me to adjust the first item in the cart. It is like the first item in the cart needs $_GET to get the data and all the other items in the cart need $_POST.
Re: Can't adjust qty or remove first item in the cart
Posted: Wed Apr 13, 2011 7:24 pm
by Jonah Bron
Is your add-to-cart code on a separate page than the cart itself? Could you post the cart.php code again in it's entirety? And please use the
Code: Select all
[ /syntax] tags instead of [code][ /code], because then it's syntax highlighted and expandable. Thanks.
Re: Can't adjust qty or remove first item in the cart
Posted: Wed Apr 13, 2011 8:57 pm
by mrzebra
cart.php code
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 ));
} else {
foreach($_SESSION["cart_array"] as $each_item) {
if ($each_item['id']==$id) {
$_SESSION['cart_array'][$i]['quantity']++;
sort($_SESSION["cart_array"],$id);
$found=true;
}
$i++;
} // 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 first item in the cart */
if(isset($_GET['item_to_adjust'], $_GET['quantity'])) {
$id = $_GET['item_to_adjust'];
$quantity = intval($_GET['quantity']);
if ($quantity == 0) {
// if the quantity is 0, remove the item from the cart
unset($_SESSION['cart_array'][0]);
}
else {
$_SESSION['cart_array'][0]['quantity'] = $quantity;
}
sort($_SESSION["cart_array"],$id);
header("location: cart.php");
}
?>
<?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'])) {
$id = $_POST['item_to_adjust'];
$quantity = intval($_POST['quantity']);
$i=0;
foreach($_SESSION["cart_array"] as $each_item) {
if ($each_item['id']==$id) {
if ($quantity == 0) {
// if the quantity is 0, remove the item from the cart
unset($_SESSION['cart_array'][$i]);
}
else {
$_SESSION['cart_array'][$i]['quantity'] = $quantity;
}
}
$i++;
} // close foreach
sort($_SESSION["cart_array"],$id);
header("location: cart.php");
}
?>
<?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.': 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.': 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":
$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'];
$scent=$row['productScent'];
}
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="*CLEARANCE* '.$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
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%><font color="#999999">ID #</font></td>
<td width=25%><font color="#999999">Product Name</font></td>
<td width=40%><font color="#999999">Scent</font></td>
<td width=5%><font color="#999999">Quantity</font></td>
<td width=5%><font color="#999999">Price</font></td>
<td width=10%><font color="#999999">Total Cost</font></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>
Re: Can't adjust qty or remove first item in the cart
Posted: Wed Apr 13, 2011 9:42 pm
by Jonah Bron
Remove all sort()s and header()s, and tell me what that does. If you can, upload it after you do that so I can look at it myself. I'm 99% positive that those redirects are not needed.
Re: Can't adjust qty or remove first item in the cart
Posted: Wed Apr 13, 2011 11:09 pm
by Christopher
It should be:
Code: Select all
header("location: cart.php");
exit;
Because you want to redirect after adding so refresh will not re-add. Same with delete or clear.
Hard to tell what else is the problem -- the fall-through logic makes things very unclear.