Question

I use simplecartjs to make a shopping cart on my website where you can select element and send it to your cart... next step, will be the checkout process, but for business reason, no checkout process will append, and a simple form with name and email and date for order pickup will be ask. Now the order must be send to an email address (at the company) that will fullfill the order.

The question : how to send the content of the cart to an email body or as attachement ?

Was it helpful?

Solution

You should add new checkout method to simplecartjs:

me.emailCheckout = function() {    

    itemsString = "";
    for( var current in me.items ){ 
        var item = me.items[current];
        itemsString += item.name + " " + item.quantity + " " + item.price + "\n";
    }   

    var form = document.createElement("form");
    form.style.display = "none";
    form.method = "POST";
    form.action = "sendjs.php";
    form.acceptCharset = "utf-8";
    form.appendChild(me.createHiddenElement("jcitems", itemsString));
    form.appendChild(me.createHiddenElement("jctotal", me.total));
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

This will create new form element and submit cart data to sendjs.php. Enable this checkout method by setting me.checkoutTo = 'Email' in simplecart options.

Now create a new sendjs.php file:

<?php
    $to      = 'you@example.com';
    $subject = 'the subject';
    $jcitems = $_POST['jcitems'];
    $headers = 'From: webmaster@example.com' . "\r\n" .
               'Reply-To: webmaster@example.com' . "\r\n" .
               'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $jcitems, $headers);
    Header('Location: thankyou.html');
?>

This will send the email message and redirect to thankyou.html page you should also create.

OTHER TIPS

This Will add the email order, suplimentary fields to the user "Phone and Adress",

Check during the CheckOut the the user is registered if not will redirect to registration.

WILL CLEAR Shopping cart only after succesful email order sent.

Will send 2 e-mail to the shop owner "shop@domain.com" and to the users email so he sees the order

Will need to make a new page for the Thank You part after succesful order is made

simplecartjs: around line 288 is in mine

me.emailCheckout = function() {    

    itemsString = "";
    for( var current in me.items ){ 
        var item = me.items[current];
        itemsString += item.name + " " + item.quantity + " " + item.price + "\n";
    }   

    var form = document.createElement("form");
    form.style.display = "none";
    form.method = "POST";
    form.action = "sendjs.php";
    form.acceptCharset = "utf-8";
    form.appendChild(me.createHiddenElement("jcitems", itemsString));
    form.appendChild(me.createHiddenElement("jctotal", me.total));
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form); 
}

sendjs.php

 <?php   require( dirname(__FILE__) . '/wp-load.php' );   
   /* cheking is user is logged in*/ 
   if ( is_user_logged_in() ) { 
    get_currentuserinfo(); /* getting user details*/

/* sending e-mail to the shop email */
        $to      = 'shop@domain.com';
        $subject = 'New Order';
        $jcitems =  " Name: " . $current_user->user_lastname .
                    " \n First Name: " . $current_user->user_firstname .
                    " \n Email: " . $current_user->user_email .
                    " \n Phone: " . $current_user->phone .
                    " \n Adress: " . $current_user->adress ;
        $headers = 'From: shop@domain.com' . "\r\n" .
                   'Reply-To: shop@domain.com' . "\r\n" .
                   'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $jcitems, $headers);

/* sending e-mail with the order to the users email*/        

        $to      = $current_user->user_email;
        $subject = 'Order copy from Domain';
        $jcitems =  "Thank you for you order. Below you have your ordered products".
                    " \n ORDER: \n\n " . $_POST['jcitems'] . "Total: " . $_POST['jctotal'] . " USD" .
                    "\n\n http://www.domain.com \nshop@domain.com";
        $headers = 'From: shop@domain.com' . "\r\n" .
                   'Reply-To: shop@domain.com' . "\r\n" .
                   'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $jcitems, $headers);
        /*Clearing the cart info after succesfull order is made*/
        setcookie ("simpleCart", "", time() - 3600);
        /*redirecting user to Thank you page from Wordpress*/
    Header('Location: http://www.domain.com/thank_you/');  } 

    else { /*sending user to register*/
        header( 'Location: http://www.domain.com/wp-login.php?action=register' ) ; exit; }   ?>

You will need Register Plus plugin for wordpress to add the extra 2 fiels to the user "phone and address"
be sure to check
Add Registration Field
Add Profile Field
Required

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top