instead of doing a traditional shopping cart checkout because of what we sell we need the checkout button to email the cart back to the web moderator for him to give approval what I have now it grabs the product out of the database but when it sends the email it sends a separate email for every line item in the shopping cart I want one email with the entire shopping cart its most likely a looping issue but can't seem to figure it out any help would be much appreciated`
foreach($_SESSION['shoppingCart'] as $key => $product)
{
if (isset($_POST['checkout']))
{
$result = $this->qry("SELECT * FROM product WHERE Item_num=?", array($product['ID']));
foreach ($result as $row)
{
echo '<tr>';
echo '<td>'.$row['Name'].'</td>';
echo '<td>'.$product['Quantity'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>$'.number_format($product['Quantity'] * $row['Price'], 2).'</td>';
echo '<td><form method="POST">
<button type="submit" name="removeFromCart" value="'.$product['ID'].'">
Remove</button></form></td>';
echo '</tr>';
$total = $total + ($product['Quantity'] * $row['Price']);
$price= $row['Price'];
$lprice= $product['Quantity'] * $row['Price'];
$quan= $product['Quantity'];
$name = $row['Name'];
$quantity = $product['Quantity'];
$to = '[email protected]';
$subject = 'order from fatfish aquatic website';
$message = "<html><body>";
$message .= "<table>";
$message .= "<th>Name</th><th>Quantity</th><th>Price</th> <th> line total</th>";
$message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quan.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";
$message .="123";
$message .= "</table>";
$message .= $total;
$message .= $row['Name'];
$message .= 'shipping address <br>';
$message .= 'phone number <br>';
}
$message .= 'email<br>';
$message .= "</body></html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: [email protected]\r\n"."X-Mailer: php";
mail($to, $subject, $message, $headers);
}
}`
Try this, I have refactored the code so that your mail
call is made after you have looped through your cart and assembled the line item details. I am not clear why you would echo the "remove product" forms at this juncture in your code - if you are making and sending the final email of the order details, the order must already have been placed, right? You will need to rethink this aspect of it, however I have demonstrated how you can refactor your email message building code to get what you have asked for.
if (!empty($_POST['checkout'])) {
// initialise message
$message = "<html><body>";
$message .= "<table>";
$message .= "<th>Name</th><th>Quantity</th><th>Price</th><th>line total</th>";
foreach ($_SESSION['shoppingCart'] as $key => $product) {
$result = $this->qry("SELECT * FROM product WHERE Item_num = ?", array($product['ID']));
foreach ($result as $row) {
// line item details added to message
$name = $row['Name'];
$quantity = $product['Quantity'];
$price = $row['Price'];
$lprice = $quantity * $price;
$total += $lprice;
$totalFormatted = number_format($total, 2);
$message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quantity.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";
$message .= "123";
$message .= $name;
}
}
// finalise message
$message .= "</table>";
$message .= "<p id='checkout-summary'>";
$message .= "total: ${$totalFormatted} <br>";
$message .= 'shipping address <br>';
$message .= 'phone number <br>';
$message .= 'email<br>';
$message .= "</p>";
$message .= "</body></html>";
$to = '[email protected]';
$subject = 'order from fatfish aquatic website';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: [email protected]\r\n" . "X-Mailer: php";
mail($to, $subject, $message, $headers);
}