[SOLVED] shopping cart cookies

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
amanw
Forum Newbie
Posts: 3
Joined: Mon Oct 18, 2004 10:12 pm

[SOLVED] shopping cart cookies

Post by amanw »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Can someone please help me?

I have written the code for a very basic shopping cart. It was working fine but as soon as I deleted the cookies of my browser and tried to run the code in a new browser window my shopping cart stopped working properly. I could only add one item at a time and it wouldn't display the items I added previously and also I wasn't able to update the quantity.
This is the code for how I set the session cookies:

Code: Select all

if(!isset($HTTP_COOKIE_VARS['cart_id'])) { 
     $cart_id = md5(uniqid(rand())); 
     setcookie("cart_id", $cart_id, time() + 14400); 
 } else { 
      $cart_id = $HTTP_COOKIE_VARS['cart_id']; 
 }
This section of the code is located at the top of my cart.php file where rest of the cart operations (add, delete, update) happen.

I think I set the cookies before any html tags but it still doesn't work.

Any suggestions?


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think I need to see more code.
amanw
Forum Newbie
Posts: 3
Joined: Mon Oct 18, 2004 10:12 pm

Post by amanw »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


This is the cart file

Code: Select all

<? 
  include ('alpha_include.php');
  do_html_header("Welcome to Flea Foundation");

// Cart Cookie Creator //////////////////////////
if(!isset($HTTP_COOKIE_VARS['cart_id'])) { 
     $cart_id = md5(uniqid(rand())); 
     setcookie("cart_id", $cart_id, time() + 14400); 
 } else { 
      $cart_id = $HTTP_COOKIE_VARS['cart_id']; 
 }
/////////////////////////////////////////////////

 
// Make the Cart Object /////////////////////////
require_once("shoppingcart.php");
$cart = new cart($cart_id);
/////////////////////////////////////////////////


// Select Action ////////////////////////////////
switch($_GET['a']){
	
	case "add":
		$cart->add_item($_GET["id"], $_GET["qty"]);
		break;
		
	case "update":
		$cart->modify_quantity($_POST["p"],$_POST["q"]);
		break;
	
	case "delete":
		$cart->delete_item($_POST['d']);
		break;
		
	case "clear":
		$cart->clear_cart();
		break;
}
///////////////////////////////////////////////


// Begin Cart Display /////////////////////////
$c = $cart->get_contents();
$cart_tbl = "
	<!-- Begin Cart Display -->
	
	<table summary='Your shopping cart' id='cart' align = 'center'>
		<tr>
			<th>Item Name</th><th>Price</th><th>Quantity</th><th>Total</th><th>Operations</th>
		</tr>";

if(count($c) <= 0){ //nothing in cart

	$cart_tbl .= "
		<tr>
			<td class='items' colspan='5'>Your cart is empty</td>
		</tr>";
				  
}else{ //stuff in the cart so show it

	for($i=0; $i < count($c); $i++){
		$cart_tbl .= "
		<tr>
			<td class='items'>" . $c[$i][0] . "</td>
			<td class='items'>" . $c[$i][2] . "</td>
			<td class='items'>
			<form action="showcart.php?a=update" method="POST">
				<input type='hidden' name='p' value='". $c[$i][3] ."' />
				<input type='text' name='q' size='3' value='" . $c[$i][1] . "' /> 
				<input type='submit' value='update' />
			</form>
			</td>
			<td class='items'>$". $c[$i][2]*$c[$i][1] ."</td>
			<td class='items'>
			<form action="showcart.php?a=delete" method="POST">
				<input type='hidden' name='d' value='". $c[$i][3] ."' />
				<input type='submit' value='remove item' class='delitem_btn' />
			</form>
			</td>
		</tr>";
	}

}	
	$cart_tbl .= "
		<tr>
			<td colspan='3' class='empty'>&nbsp;</td><td class='total'>". $cart->cart_total() ."</td><td class='items'><form action="showcart.php?a=clear" method="post"><input type="submit" value="clear cart" /></form></td>
		</tr>";
	$cart_tbl .= "
		</tr>
	</table>
	<!-- End Cart Display -->\n\n\n";
/////////////////////////////////////////////////							
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Your Shopping Cart</title>
	
	<style type="text/css" media="all">
		/* Some Clunky Style Rules To Be Highly Modified */
		#cart 
				 {width:auto;}
		
		#cart th 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#A8B3AB;
				  color:#A15733;
				  border:1px solid #ccc;
				  text-transform:uppercase;}
		
		#cart .items 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#ccc;
				  color:#333;
				  border:1px solid #999;
				  vertical-align:middle;}
				  
		#cart .empty 
				 {background-color:#fff;}
		
		#cart .total 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#999;
				  color:#333;
				  border:1px solid #fff;}
				  
		#minicart 
				  {width:auto; 
				   border:1px solid #333;
				   padding:0px;
				   background-color:#ccc;}		
				   
		.minihead 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#999;
				  color:#000;
				  border:1px solid #ccc;
				  text-transform:uppercase;
				  text-align:center;}
		
		.minicontent 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#ccc;
				  color:#333;
				  border:1px solid #999;
				  text-align:center;}  
	</style>

</head>

<body>
<? 
echo $cart_tbl; 
$t = $cart->cart_total();
$qty = $cart->quant_items();
do_html_footer();
?>
<th><blockquote>
    <p>Checkout</p>
  </blockquote></th>
</body>
</html>

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read the posting code thread

please make sure error_reporting is set to E_ALL and display_errors is on/true/1. Consider switching to $_COOKIE instead of $HTTP_COOKIE_VARS.

what does do_html_header() do? I'm thinking your cookie isn't getting set properly, or you aren't retrieving/storing the cart correctly. :?:
amanw
Forum Newbie
Posts: 3
Joined: Mon Oct 18, 2004 10:12 pm

Post by amanw »

Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

feyd, thanks for you help. It works now.
what I had to do was move the two lines of code above where I set the cookies to below it.
so what I have now is this:

Code: Select all

// Cart Cookie Creator //////////////////////////

if(!isset($HTTP_COOKIE_VARS['cart_id'])) 
{      $cart_id = md5(uniqid(rand()));      
setcookie("cart_id", $cart_id, time() + 14400);  
}
 else {       
$cart_id = $HTTP_COOKIE_VARS['cart_id'];  
}

include ('alpha_include.php');  
do_html_header("Welcome to Flea Foundation");
do_html_header just posts the html tag and the title passed to it.
and alpha_include.php has other include files which call upon this do_html_header function. so i think what was happening is that the html tags were being posted before my cookies were getting set.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post the code to do_html_header()..

AND make sure to use the

Code: Select all

tags!
DroopyPawn
Forum Newbie
Posts: 4
Joined: Sun Nov 07, 2004 6:06 pm

Create SIMPLE shopping cart from scratch

Post by DroopyPawn »

I know this has been posted over and over but I'd like to go at it again.

I have commerce-cgi installed on my web site at http://www.16tracks.com and it works ok. But I'm in the proccess of redoing my entire site and I'd like to redo the shopping cart along with it. I don't know Perl and I don't know much PHP either but I am familiar with programming in general.

I'd just like to get some ideas on how to start this project. What I'd like to do is add a form for each item I sell, similar to what I already use.

Here's an example from a guitar page. It shows the Item number (BR-140), the price ($295), shipping cost ($18) and a description. It also has an optional row wich can be selected to included a case with an added cost of $65.

Code: Select all

<FORM METHOD="POST" ACTION="http://www.16tracks.com/cgi-bin/store/commerce.cgi">
			    <INPUT TYPE="text" NAME="item-1||295.00|BR-40 Blueridge Acoustic Guitar||18.00" SIZE="3" MAXLENGTH="3" VALUE="1">
			    <SELECT NAME="option|1|1">
			    	<OPTION VALUE="BR-40|0">BR-40
			    	<OPTION VALUE="BR-40 & Case|65">BR-40 & Case +$65
			    </SELECT>    
			   <INPUT TYPE="submit" NAME="add_to_cart_button.x" VALUE="Add To Cart">
			</FORM>
When the "add to cart buton" is pressed, the script generates a new HTML page with a table of items that have been "added to cart". The cart also shows the customer's current total with shipping.

Now I realize that I'm going to have to use cookies or sessions or something (which I know nothing about) but I assume that this will not be needed until the user actually presses one of the "add to cart" buttons. I assume that at this time, a cookie or session will be started. I further realize that cookies may be a bad option because some users may have cookies turned off. So how do I track the user (and his cart) after the first item is added?

I have read enough PHP posts and chapters in books that I think I can do all the echos to print out the HTML code for the cart. I'm just not sure how to get started.

Also, with my current setup, I get an email when a new order is placed. I get all of the customer's shipping and payment info (except that I have to ftp to my site to get half of the credit card number for security reasons.) I'd rather have the entire order (including the credit card number) emailed to me OR maybe more secure - have a page to access to see the lates order info on my web site.

Anyway, can someone help me with the very first part for now? I mean, when an "add to cart" button is clicked for the first time, what do I do? I don't want to use any database. All of the item info will be included in the form.
DroopyPawn
Forum Newbie
Posts: 4
Joined: Sun Nov 07, 2004 6:06 pm

Post by DroopyPawn »

Sorry, I meant to post this as a separate post.
Post Reply