从购物车中检索数据并使用邮件发送

时间:2015-06-12 12:05:34

标签: php codeigniter cart

此项目与电子商务有关。可以选择 添加到购物车 。当用户点击添加时,产品也会被添加到他的购物车中并且工作正常。

但最后当用户点击下订单按钮后,管理员将收到包含产品的邮件。电子邮件正文内容应该如下所示。

Image

我尝试逐行使用foreachecho数据。但它没用。

问题 - 我想从购物车中检索数据并添加到上表格式,然后它将以邮件结束。如何使用CI存档?

Html 表格格式

$to = 'mail@gmail.com';
                    $msg .= '
                        <table id="table" border="0" cellpadding="5px" cellspacing="1px"  style="border: 1px solid #eee">
                            <tr id= "main_heading">
                                <td>Name</td>
                                <td>Price</td>
                                <td>Qty</td>
                                <td>Amount</td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        </table>';

注意:此处没有特殊代码(插入和更新)。所有代码与ellislab.com

相同

CART VIEW

Cart view

如果我使用

$as = $this->cart->contents();
print_r($as);

显示

Array

控制器

public function index()
        {
            $data['category'] = $this->Product_Model->get_category();
            $data['category2'] = $this->Product_Model->get_category2();
            $data['product'] = $this->Product_Model->get_product();
            $data['right_brand'] = $this->Product_Model->get_right_brand();

            $data['right_prod'] = $this->Product_Model->get_right_product();
            $data['brand'] = $this->Product_Model->get_brand_names();

            $data['products'] = $this->Product_Model->get_cart_items();
            $data['head'] = $this->Product_Model->check_cart();

            $this->load->view('template/header', $data);
            $this->load->view('template/right_sidebar', $data);
            $this->load->view('pages/cart', $data);
            $this->load->view('template/foot');


        }

        public function insert_cart()
        {

            $data = array(
                'id'    => $this->input->post('pid'),
                'qty'   => $this->input->post('qty'),
                'price' => $this->input->post('price'),
                'name'  => $this->input->post('head'),
            );

            $this->cart->insert($data);    
        }

        function remove($rowid)
        {
            if ($rowid==="all")
            {
                $this->cart->destroy();
                redirect('index.php/main');
            }
            else
            {
                $data = array(
                    'rowid'   => $rowid,
                    'qty'     => 0
                );
                $this->cart->update($data);
            }

            // This will show cancle data in cart.
            redirect('index.php/cart');
        }

        function update_cart()
        {

            $cart_info =  $_POST['cart'] ;
            foreach( $cart_info as $id => $cart)
            {
                $rowid = $cart['rowid'];
                $qty = $cart['qty'];

                $data = array(
                    'rowid'   => $rowid,
                    'qty'     => $qty
                );

                $this->cart->update($data);
            }
            redirect('index.php/cart');
        }

2 个答案:

答案 0 :(得分:2)

使用jquery并捕获您要作为电子邮件发送的html table。并使用ajax将变量传递给电子邮件功能,试试这个,

    <script>
                    $(document).ready(function() {
                        var htmlstring = $("#mail_table").html();
                        $( "#send" ).click(function() {

                            var cn_name = $("#cn_name").val();
                            var cn_mail = $("#cn_mail").val();

                            $.ajax({
                                method : 'post',
                                dataType : 'html',
                                url :'<?php echo base_url(); ?>index.php/cart/testmail',
                                data :{
                                    name:cn_name,
                                    mail:cn_mail
                                    table:htmlstring
                                }
                            });
                        });
                    });
                </script>

jquery veriable = htmlstring

您也可以使用jquery serialize方法获取所有表单输入。

https://api.jquery.com/serialize/

CI ocontroller

public function testmail(){

            $to = 'mail@gmail.com';
            $subject = 'Enquiry from ';
            $message = $_POST['table'];
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

            if(mail($to, $subject, $message, $headers))
            {

            }
        }

答案 1 :(得分:0)

对于查看购物车并以邮件方式接收,请使用此...

在邮件中使用此代码......

$tpl_file = 'cartmail.php';
if (!file_exists($tpl_file)) {
die('Mail could not be sent');
exit;
}
$msg_tmpl = file_get_contents($tpl_file);
$to = 'mail@gmail.com';
$subject = 'Enquiry from ' .$_REQUEST['guest_name'];
$message = $msg_tmpl;
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$_REQUEST['email'];
mail($to, $subject, $message, $headers);

cartmail.php文件中使用此代码

<div class="table-responsive">
<table class="shop_table cart" cellspacing="0">
    <thead>
        <tr>
            <th class="product-remove">&nbsp;</th>
            <th class="product-thumbnail">&nbsp;</th>
            <th class="product-name">Product</th>
            <th class="product-price">Price</th>
            <th class="product-quantity">Quantity</th>
            <th class="product-subtotal">Total</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
           $obj = $results->fetch_object();
    ?>

                        <tr class="cart_item">
                    <td class="product-name">
                    <span class="name"><?php echo $obj->product_name; ?> </span>                    </td>

                    <td class="product-price">
                        <span class="amount"><?php echo $currency,$obj->price;?></span>                 </td>

                    <td class="product-quantity">
                        <div class="quantity"><?php echo $cart_itm["qty"];?></div>
                    </td>

                    <td class="product-subtotal">
                        <span class="amount"><?php $ptotal=$obj->price*$cart_itm["qty"]; echo $ptotal; ?></span>                    </td>
                        <td class="product-remove">
                    <?php echo '<a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'" class="remove" title="Remove this item">&times;</a>';?> </td>
                </tr>
<?php
    $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            $cart_items ++;

        }
        ?>
            </tbody>
</table>

</div>