wojodesign / simplecart-js

A simple javascript shopping cart that easily integrates with your current website.
simplecartjs.org
1.79k stars 489 forks source link

e-commerce cart to checkout via email instead of paypal #529

Open DiabloSV opened 7 years ago

DiabloSV commented 7 years ago

Good day,

I am currently working on a site for a client that wants to have products that is purchased from a website to be sent to his email address and then have his eft details sent with a invoice number back to the client's email.

The cart page is working perfectly, I am just struggling to get the content onto the checkout.php/send-email.php page and have all information sent to the owner of the site's email address. I have tried setting up the mailing code but no email gets sent and when it get's sent it doesn't display any information.

Code is as follows: ///////////////////////////// add-to-cart.php ///////////////////////////// <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // Connect to the MySQL database
include("db.php"); ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (if user attempts to add something to the cart from the product page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } header("location: cart.php"); exit(); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = 0; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "

Your shopping cart is empty

"; ?> <?php
} else { //Checkout button $pp_checkout_btn .= '

<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="donovan@sector36technologies.co.za">';
// Start the For Each loop
$i = 0; 
foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id'];
    $sql = mysqli_query($con,"SELECT * FROM products WHERE id='$item_id' LIMIT 1");
    while ($row = mysqli_fetch_array($sql)) {
        $product_name = $row["name"];
        $price = $row["price"];
        $details = $row["description"];
        $img = $row['img1'];
        $amount = $row['amount'];
        $rgb = $row['rgb'];
        $ing = $row['ingredients'];
    }
    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;
    setlocale(LC_MONETARY, "en_US");
    // Dynamic Checkout Btn Assembly
    $x = $i + 1;
    $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">

    <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
    <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
    // Create the product array variable
    $product_id_array .= "$item_id-".$each_item['quantity'].","; 
    // Dynamic table row assembly
    $cartOutput .= "<tr>";
    $cartOutput .= '<td><a style="color:rgb(255,255,255)" href="product.php?id=' . $item_id . '">' . $product_name . '<br /><img class="img-thumbnail img-responsive center-block" src="assets/img/' . $img . '" alt="' . $product_name. '" width="75" height="75" border="1" /></a></td>';
    $cartOutput .= '<td style="color:rgb(255,255,255)">' . $details . '</td>';
    $cartOutput .= '<td style="color:rgb(255,255,255)">R' . $price . '.00</td>';
    $cartOutput .= '<td><form action="cart.php" method="post">
    <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
    <button class="btn btn-danger"  name="adjustBtn' . $item_id . '" type="submit" value="change" />Change
    <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
    </form></td>';
    //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
    $cartOutput .= '<td style="color:rgb(255,255,255)">' . $pricetotal . '</td>';
    $cartOutput .= '<td><form action="cart.php" method="post"><button class="btn btn-danger"  name="deleteBtn' . $item_id . '" type="submit" value="X" />X<input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
    $cartOutput .= '</tr>';
    $i++; 
} 
setlocale(LC_MONETARY, "en_US");
$cartTotal = "<div style='font-size:18px; margin-top:12px; color:rgb(255,255,255);' align='right'>Cart Total : ".$cartTotal.".00 Rand</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<button class="btn btn-danger" type="submit">Checkout </button>
</form>';

} ?>

///////////////////////////// cart.php ///////////////////////////// <?php
include("add-to-cart.php"); ?> <!DOCTYPE html>

Your Cart
Product Product Description Unit Price Quantity Total Remove

////////////// send-email.php ///////////// <?php // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['lastname']) || !isset($_POST['email']) || !isset($_POST['phone'])){ died('Please ensure that all fields with * is inserted correctly');
} $first_name = $_POST['name']; // required $last_name = $_POST['lastname']; // required $email_from = $_POST['email']; // required $phone = $_POST['phone']; // required $messageform = $_POST['message']; // not required

// loop through cart foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysqli_query($con,"SELECT FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysqli_fetch_array($sql)) { $product_name = $row["name"]; $price = $row["price"]; $details = $row["description"]; $img = $row['img1']; $amount = $row['amount']; $rgb = $row['rgb']; $ing = $row['ingredients']; } $pricetotal = $price $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal;

$to = 'sales@vape-elicious.co.za'; $subject = 'Sale';

$message = 'Client Name :' .$first_name. ' ' .$last_name. ."\r\n". 'Phone: ' .$phone "\r\n". 'Message: ' .$messageform "\r\n". 'Item ID:' .$item_id "\r\n". 'Product name:'.$product_name "\r\n". 'Amount:' .$amount "\r\n". 'Price:' .$price "\r\n".; 'Total:' .$cartTotal "\r\n".; $headers = 'From: ' .$email_from "\r\n" . 'Reply-To: sales@vape-elicious.co.za' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers); ?>

//////////////// checkout.php ////////////////

<!DOCTYPE html>

Vape elicious

Checkout

Please fill out the following information in order to complete your order.

*Required fields