php - 我如何将购物车的内容通过电子邮件发送回网络版主
问题描述
而不是因为我们销售的产品而进行传统的购物车结账,我们需要结账按钮将购物车通过电子邮件发送回网络版主,让他批准我现在拥有的东西,它会从数据库中抓取产品,但是当它发送电子邮件时它为购物车中的每个行项目发送一封单独的电子邮件我想要一封包含整个购物车的电子邮件这很可能是一个循环问题,但似乎无法弄清楚任何帮助将不胜感激`
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 = 'ms245@zips.uakron.edu';
$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: ms245@zips.uakron.edu\r\n"."X-Mailer: php";
mail($to, $subject, $message, $headers);
}
}`
解决方案
试试这个,我已经重构了代码,以便mail
在您循环浏览购物车并组装行项目详细信息后进行调用。我不清楚为什么您会在代码中在此关键时刻呼应“删除产品”表格 - 如果您正在制作并发送订单详细信息的最终电子邮件,那么订单必须已经下达,对吗?您将需要重新考虑它的这一方面,但是我已经演示了如何重构您的电子邮件消息构建代码以获得您所要求的内容。
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 = 'ms245@zips.uakron.edu';
$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: ms245@zips.uakron.edu\r\n" . "X-Mailer: php";
mail($to, $subject, $message, $headers);
}
推荐阅读
- reactjs - 下载 zip,解压缩,将内容转换为 srt,最后将 str 转换为数组
- r - 如何减少叶子长度以在 R 树状图中拟合标签?
- github - Composer 在 Travis 中通过 SSH 协议而不是 HTTPS 进行解析
- excel - 选择与另一个 Excel 工作表中的项目列表匹配的所有数据透视项目
- drupal-8 - 在 DKAN 网站中为图像添加自定义 CSS / 将类名称附加到 DKAN 中的 HTML 元素
- json - 创建嵌套对象的数据库实体
- java - 使用 Java 在 DynamoDB 中更改时间点的值
- clion - 如何在clion中排除项目根目录之外的目录?
- html - 无法在 Firefox 中使用使用 CSS 网格 fr 单元计算的 % 高度
- spring-boot - 启动 Spring Cloud CLI 的问题