在 phpmailer 中列出购物车

我尝试在“下订单”后使用 phpmailer 发送电子邮件。问题是我无法将购物车列为电子邮件(产品、数量、每种产品的价格和总价)。

echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);

这表明一切似乎都有效。

https://img3.mukewang.com/64c5f9e10001580712230033.jpg

购物车.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"); 

这根本不起作用。我尝试了一些方法,大部分有效的只是列出购物车列表中的最后一个产品。但只有身份证。


qq_笑_17
浏览 91回答 1
1回答

扬帆大鱼

我找到了一个解决方案:phpmailer.php&nbsp; &nbsp; ...&nbsp; &nbsp; $mail->Body&nbsp; = nl2br("Hi {$_POST['name']} \r\n");&nbsp; &nbsp; /// List Cart Item(s) Start&nbsp; &nbsp; $total = 0;&nbsp; &nbsp; if(!empty($_SESSION['cart'])){&nbsp; &nbsp; &nbsp; &nbsp; include 'config.php';&nbsp; &nbsp; &nbsp; &nbsp; $index = 0;&nbsp; &nbsp; &nbsp; &nbsp; if(!isset($_SESSION['cart'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";&nbsp; &nbsp; &nbsp; &nbsp; $query = $conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; while($row = $query->fetch_assoc()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $service = $row['name'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $qty = $_SESSION['cart'][$index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $qtyPrice = $row['price'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total += $_SESSION['cart'][$index]*$row['price'];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $index ++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");&nbsp; &nbsp; /// Cart Item(s) End&nbsp; &nbsp; $mail->Body .= nl2br("Kind regards \r\n");&nbsp; &nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP