Having problems sending and email containg the order details from cart

Hi , i have found some code for a cart and tweeked it to suit my needs but i cant work out how to send the order details in an email , so price , quantity and total price.

Thanks in advance Ian

Below is the cart code
[php]

<?php session_start(); $b_session =(int)$_SESSION['business_id']; if($b_session){ echo " B SESSION SET "; } ?> Cart <?php include("scripts/connection.php"); //connect to your database here foreach($product_id as $pid){ } ?> <?php $product_id = $_GET[product]; //the product id from the URL $action = $_GET[action]; //the action from the URL //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id header("location: cart.php"); break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. header("location: cart.php"); break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo ""; //format the cart using a HTML table //iterate through the cart, the $product_id is the key and $quantity is the value foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = mysql_query("SELECT * FROM products WHERE id ='$product_id'"); $num_rows = mysql_num_rows($sql); while($row = mysql_fetch_assoc($sql)){ $product_name = $row['product_name']; $price_1_6 = $row['price_1_6']; $price_1_6_b = $row['price_1_6_b']; $price_6_plus = $row['price_6_plus']; $price_6_plus_b = $row['price_6_plus_b']; $type = $row['type']; $price = $row['price']; } $product_name .= $new_product; //Only display the row if there is a product (though there should always be as we have already checked) if($num_rows > 0) { // SET THE PRICE IF THE PRODUCT IS WINE if($type == "Wine") { if($b_session){ // check if the session is set $cart_price = $price_1_6_b; // if the bus session is set , set the new price if($quantity >6){// if the quantity is greater than 6 use the discounted price ( bus) $cart_price = $price_6_plus_b; } } if(!$b_session) { // if the session isnt set use the guest price $cart_price = $price_1_6; if($quantity >6){// if the quantity is greater than 6 use the discounted price $cart_price = $price_6_plus; } } } // SET THE PRICE IF THE PRODUCT IS FOOD if($type == "Food") { $cart_price = $price; } $line_cost = $cart_price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo ""; //show this information in table cells echo ""; echo ""; echo ""; //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product echo ""; } } //show the total echo ""; echo ""; echo ""; echo ""; ?>
        <?php
		
		//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
		echo "<tr>";
			echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
		echo "</tr>";		
	echo "</table>";
	
	

}else{
	//otherwise tell the user they have no items in their cart
	echo "You have no items in your shopping cart.";
	
}

//function to check if a product exists
function productExists($product_id) {
		//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
		$sql = sprintf("SELECT * FROM products WHERE id = %d;",
						$product_id); 
			
		return mysql_num_rows(mysql_query($sql)) > 0;
}

?>

Continue Shopping

[/php]

$product_name $type$line_cost
$quantity Add X
Total$total

This tutorial explains the details of sending a simple email. You will just have to insert your data into the message in the form you want. Hope that helps…

http://www.w3schools.com/php/php_mail.asp

Hi thanks the reply , I know how to send an email but its getting the products to list in the emaik when I put $product_id in the body of the email it doesn’t show anything , so I’m stuck at that point .

Any more help will be great thanks

Well, since you didn’t show any email code, I thought you meant you wanted to know how to send one.

So, once your cart is complete and you are ready to email it, you will have to loop thru each item and create your message from it. Usually done after the payment has been processed. Something like this in general:

$message = "This message is from " . $userID . " containing list of purchases made on " . date() . ": \r\n";
 for-loop thru cart...
   {
     $message .= "Product : " . $product_name . "   Price : " . $price . "\r\n";
   }

Then send your email...    

Something like that should work. Give it a try and let us know if you need more help with it…

Thanks a lot ill have a go , yea sorry I didn’t copy the new code over , I’m not using a payment gateway , just sending an email order , I still find foreach loops confusing , so do I loop throu the session cart as tgats holding the data ?

Thanks in advance

Well, a quick look at your code and it looks like you store the product stuff inside a session variable called “cart” and you are using $_SESSION[‘cart’][$product_id] format of an array… Also, you loop thru this in the code you showed. So, all you have to do is create a message inside that loop and that should do it for you. So, inside this:

while($row = mysql_fetch_assoc($sql)){
$product_name = $row[‘product_name’];
$price_1_6 = $row[‘price_1_6’];
$price_1_6_b = $row[‘price_1_6_b’];
$price_6_plus = $row[‘price_6_plus’];
$price_6_plus_b = $row[‘price_6_plus_b’];
$type = $row[‘type’];
$price = $row[‘price’];
}

Add your code… Something like this:
[php]
while($row = mysql_fetch_assoc($sql)){
$product_name = $row[‘product_name’];
$price_1_6 = $row[‘price_1_6’];
$price_1_6_b = $row[‘price_1_6_b’];
$price_6_plus = $row[‘price_6_plus’];
$price_6_plus_b = $row[‘price_6_plus_b’];
$type = $row[‘type’];
$price = $row[‘price’];
$message .= "Product : " . $product_name . " - type : " . $type . " - price : " . $price . “\r\n”;
}
[/php]
Not sure of your code, but, this should add the products to the $message variable…
Then, just before you email it, you add your notes to it… Something like this:
$message = “Here is another order from the website!\r\n” . $message;
Or, you could add the first part earlier in your code…

Not sure if that is what you wanted, but, I think it might work for you…

Thanks alot thats worked nicely i had to add it at the end of the loop because there are a few if statments that change the variables ( so there not in the loop ).

1 more thing if you can help, the product name etc is coming out in a line , is there a way to put each product list into a line so e.g.

Product name Quantity price

Product name Quantity price

would explode work ?

explode(" ", $message);
echo $message;// just using this to display the results for testing

Thanks Again

Ive sorted it this code worked for me

[php]
$message .= “Product Name -” . $product_name . "Quantity ". $quantity . “Price “. $line_cost .”
”;

[/php]

Again thanks alot for the help youve help me out alot

Great! Glad we could solve it for you… Sometimes a programming puzzle just takes a new view at it…

CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service