Retrieve data from cart and send using mail

1.8k views Asked by At

This project is with E-Commerce. There is option to Add to Cart. When user click on add, product will be added to his cart as well and working fine.

But finally when user click place order button Admin will receive Mail with products. That E-mail body should content should display like below.

Image

I tried with using foreach and echo data row by row. But its useless .

Question is - I want retrieve data from cart and add to Above table Format and then it will end with mailing. How to archive this with CI?

Html Table Format

$to = '[email protected]';
                    $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>';

Note : there is no special code to this(inserting and updating). all the codes are same with ellislab.com

CART VIEW

Cart view

If i use

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

it shows

Array

Controller

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

There are 2 answers

0
KBK On BEST ANSWER

use jquery and capture the html table that you want to send as email. and pass the variable to the email function using ajax, try this,

    <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

also you can use jquery serialize method to get all form inputs.

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

CI ocontroller

public function testmail(){

            $to = '[email protected]';
            $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))
            {

            }
        }
2
Bruce On

For view cart and receive it as mail use this...

In mail use this code...

$tpl_file = 'cartmail.php';
if (!file_exists($tpl_file)) {
die('Mail could not be sent');
exit;
}
$msg_tmpl = file_get_contents($tpl_file);
$to = '[email protected]';
$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);

And in the cartmail.php file use this code

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