Shopping Cart Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrzebra
Forum Commoner
Posts: 37
Joined: Tue Feb 22, 2011 10:30 pm

Shopping Cart Help

Post by mrzebra »

Hello, I'm new to PHP and I followed a tutorial to create a shopping cart that I can pass to paypal. I have it working fine....it's adding items, removing items, updating quantities....except I can't adjust the quantity or remove the very first item in the cart. For example, if the cart has the following items:

10027 Elite Hand Cream 120mL Pomegranate 4 $20.00 $80.00
20016 Bath Soak 150ml Mulberry 1 $10.00 $10.00
50024 Bath Salts 12oz Honeysuckle 1 $8.00 $8.00


I can remove and adjust the quantities of the bath soak and bath salts but I can't do either with the elite hand cream. I'm not sure why this is so. I think my code may be kind of confusing so I apologize. Hopefully someone can shed some light on how I can fix this.

Here is my 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($_POST['id'])) {
		$found=false;
		$i=0;
		$id=$_POST['id'];
		$product_id=$_POST['pid'];
		$product_scent=$_POST['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) {
				$i++;
				while(list($key, $value)=each($each_item)) {
					// if the item is already in the cart, up the quantity by 1
					if ($key == "id" && $value == $id) {	
						
						$_SESSION["cart_array"][$i-1]['quantity']++;
						
						// sort the array by item ID
						sort($_SESSION["cart_array"],$product_id);
						$found=true;
					} // close if
				} // close while
			} // 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");
	exit();

}
?>

<?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['item_to_adjust'] != "") {
		 $adjust['id']= $_POST['item_to_adjust'];        
		 $adjust['product_id']= $_POST['name'];        
		 $adjust['quantity']= $_POST['quantity'];        
		 $adjust['scent']= $_POST['scent'];
		 
		 $i=0;
		
		
		
	
		foreach($_SESSION["cart_array"] as $each_item) {
				$i++;
				
			
				
				while(list($key, $value)=each($each_item)) {
										
					if ($key == "id" && $value == $adjust['id']) {
					
						//  if the quantity is set to 0, remove the item from the cart
						if ($adjust['quantity'] == 0) {
							
							// remove the item from the cart
							unset($_SESSION["cart_array"][$i-1]);
						} else {
							
						// change the quantity of the item if not 0	
						$_SESSION["cart_array"][$i-1]['quantity'] = $adjust['quantity'];
						}
					
					}  // close if 
										
				
		} // close foreach
	
					
		} // close else
		sort($_SESSION["cart_array"],$adjust['id']);
		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=6 align=center><i><font face=arial color=#999999>Your shopping cart is empty</font></i></td></tr>";
} else {
	
	// start Paypal Checkout Button
	$pp_checkout_btn .='<form target="_self" action="" method="post">
	<input type="hidden" name="shopping_url" value="">
	<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="myemail@mycompany.com">';
	
	
	// start the foreach loop
	$i=0;
	foreach($_SESSION["cart_array"] as $each_item) {
		
		$item_id=$each_item['id'];
		$prod_id=$each_item['product_id'];
		$scent=$each_item['scent'];
		
		
		// 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
		$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="'.$product_name.'">
							   <input type=hidden name="item_scent_'.$x.'" value="'.$scent.'">	
							   
							   <input type=hidden name="amount_'.$x.'" value="'.$price.'">
							   <input type=hidden name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
			
			// dynamic cart item dispaly
			
		
			
			$cartOutput.="<tr align=center>
								<td width=5%><font color='#999999'>".$each_item['id']."</font></td>
								<td width=30%><font color='#999999'>".$product_name."</font></td>
								<td width=35%><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><font color='#999999' size='-1'><a href=# onClick='submit()'>Update</a><br>
									
									</font></td>
								<td width=5%><font color='#999999'>$".$price.".00</font></td>
								<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>
								
							</tr></form>";			
			$i++;
			
			
			
		} // end if 
	} // end foreach
	
	
	// 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="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>';
	
}


?>
<body background="#1d1d1d">	
	
					
						<table border=1 width=100% 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=30%><b><font color="#cbb659">Product Name</font></b></td>
								<td width=35%><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>
			
		</body>	
		

Any help would be greatly appreciated.
Post Reply