亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 phpmailer 中列出購物車

在 phpmailer 中列出購物車

PHP
qq_笑_17 2023-07-30 13:46:06
我嘗試在“下訂單”后使用 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"); 這根本不起作用。我嘗試了一些方法,大部分有效的只是列出購物車列表中的最后一個產品。但只有身份證。
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

我找到了一個解決方案:


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");


    ...


查看完整回答
反對 回復 2023-07-30
  • 1 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號