php - 在 phpmailer 中列出购物车
问题描述
我尝试在“下订单”之后使用 phpmailer 发送电子邮件。问题是我无法将购物车列为电子邮件(产品、数量、每种产品的价格和总价)。
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
这向我表明一切似乎都有效。
购物车.php:
<tr>
<?php
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
</tr>
<tr>
<td>
<img src="<?= $row['photo'] ?>" width="150px"><br />
<?= $row['name'] ?>
</td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td>
<?php echo $_SESSION['qty_array'][$index]; ?>
</td>
<td>
<b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
</td>
<?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
$index ++;
}
}
?>
phpmailer.php:
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
foreach($_SESSION['cart'] as $key => $product) {
$name = $product['name'];
$price = $product['price'];
$qty = $product['qty'];
$tprice = $product['totalPrice'];
}
$mail->Body = nl2br("$name\r\n$\r\n$qty\r\n$tprice");
这根本行不通。我尝试了一些东西,大部分的工作只是在购物车列表中列出最后一个产品。但只有身份证。
编辑:
我尝试了另一种方法,但仍然只列出最后一项。
phpmailer.php:
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$service = $row['name'];
$qty = $_SESSION['qty_array'][$index];
$qtyPrice = number_format($row['price'], 2);
$qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
$total += $_SESSION['qty_array'][$index]*$row['price'];
$mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
$index ++;
}
}
解决方案
我找到了一个解决方案:
phpmailer.php
...
$mail->Body = nl2br("Hi {$_POST['name']} \r\n");
/// List Cart Item(s) Start
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$index;
$service = $row['name'];
$qty = $_SESSION['cart'][$index];
$qtyPrice = $row['price'];
$qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
$total += $_SESSION['cart'][$index]*$row['price'];
$mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
$index ++;
}
}
$mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
/// Cart Item(s) End
$mail->Body .= nl2br("Kind regards \r\n");
...
推荐阅读
- javascript - ExceptionsManager.js:84 未处理的 JS 异常:ReferenceError:未定义响应
- postgresql - 如何在 PostgreSQL 中跳过 psql \COPY 中的空行
- sql - 将数据从列转换为行,仅当列的并集不为空时才选择,使用 where 子句检查不为空和 != ''
- node.js - 如何将 nodemon 用于 eslint 和 babel
- python-3.x - 将图像放在另一个图像之上的更有效方法 - OpenCV Python3
- asp.net-core - ASP.NET Core 静态文件返回为 Base64 编码
- react-native - jest-haste-map:观看错误,抱怨在 jest-haste-map node_modules 文件夹中找不到文件
- html - 如何在
- tfs - TFS 发布部署评论
- ios - HMHomeManager 家是空的