【问题标题】:how can i email the contents of a shopping cart back to the web moderator我如何将购物车的内容通过电子邮件发送回网络版主
【发布时间】:2018-06-05 14:04:10
【问题描述】:

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

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() 函数在循环内...
  • @mitchell-secaur 我已经为您提供了答案,如果您认为这可以解决您的问题,请考虑将其标记为已接受的答案,谢谢 :)

标签: php html-email shopping-cart


【解决方案1】:

试试这个,我已经重构了代码,以便在您循环浏览购物车并组装订单项详细信息后进行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);

}

【讨论】:

  • 表格标题、行和表格在正文中为您的代码中的每个行项目重复,而送货地址、电话和电子邮件位于正文中的表格之外?
  • @Syfer 表格标题不应该在那里,真的,我的疏忽。但是,我们当然希望每个订单项都有一个新的&lt;tr&gt;?至于正文中杂乱无章的摘要信息,这是根据他的源代码,虽然我已经包裹在&lt;p&gt;中,但我无法决定。这是如此,不是代码编写服务,我只是说明他如何可以重构以解决他的实际问题“它为每个行项目发送单独的电子邮件购物车 - 我想要一封包含整个购物车的电子邮件”
  • 我了解,但您可以指出错误(不要修复只是指出),这可能是现在的样子。
  • 我做到了 - 请参阅我的帖子顶部 “我已经展示了如何重构您的电子邮件构建代码以获得您所要求的内容” 这并不声称是一个修复。您无法“解决”这样的问题 - 只需查看源代码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多