首页 > 解决方案 > 我如何将购物车的内容通过电子邮件发送回网络版主

问题描述

而不是因为我们销售的产品而进行传统的购物车结账,我们需要结账按钮将购物车通过电子邮件发送回网络版主,让他批准我现在拥有的东西,它会从数据库中抓取产品,但是当它发送电子邮件时它为购物车中的每个行项目发送一封单独​​的电子邮件我想要一封包含整个购物车的电子邮件这很可能是一个循环问题,但似乎无法弄清楚任何帮助将不胜感激`

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





                }

            }`

标签: phphtml-emailshopping-cart

解决方案


试试这个,我已经重构了代码,以便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: &#36;{$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);

}

推荐阅读