Jump to content

Send customer order email


MovedBiscuit73

Recommended Posts

I need a fresh set of eyes. This is the link to the website: http://hermanshivehire.biz/index.php

Basically its purpose is to send an email to a customer summarizing their order.

 

The issue I'm having is with the foreach statement . When the email is sent to admin - an email is sent for each product item.

The figures and totals are all correct.

I have tried closing the foreach in different places and this results is one email but there is no loop for the product items.

 

Here is my code (you can copy it into notepad++):

<?php                $member_id = $_SESSION['memberID'];                     $query = "INSERT INTO tbl_order (orderDate, order_memberID_F) VALUES (NOW(), '$member_id')"; // $orderDate = datetime(now);                mysqli_query($link, $query);                // retrieve the order id from tbl_order                $orderID = mysqli_insert_id($link);                // decode product id from $_SESSION['cart']                $cart = $_SESSION['cart'];                $items = explode(',', $cart);  // $items is an array now                $content = array();  // declares $content as array                foreach ($items as $productID) {                    if (isset($content[$productID]))                        $content[$productID] = $content[$productID] +1;                    else                        $content[$productID] = 1;                        // $content[$productID] = (isset($content[$productID]))?$content[$productID] + 1:1; SHORTENED VERSION OF ABOVE IF                } // end of foreach                // store $content into tbl_order_product                foreach ($content as $product_id=>$qty) {                    // retrieve product price AND name from tbl_product                             $query = "SELECT * FROM tbl_product WHERE productID ='$product_id' ";                    $result = mysqli_query($link, $query);                    $row = mysqli_fetch_array($result);                    $price = $row['price']; //value from database                    $prodName = $row['name']; //value from database                    $total = $qty*$price;                    $grandTotal = $grandTotal + ($qty*$price);                    $query = "INSERT INTO tbl_order_product (orderID_F, productID_F, orderQuantity, orderPrice)  VALUES ('$orderID', '$product_id', '$qty', '$price')";                    mysqli_query($link, $query);                                            unset($_SESSION['cart']); // destroys the cart session                  } // end of foreach                                             //send email TO admin WITH ORDER DETAILS                    $email_to = "info@hermanshivehire.biz"; // your email address send TO                    $email_from = "info@hermanshivehire.biz"; // your email address send FROM                    $subject = "RE: New Website Order to Fulfill";                    // From                    $header = 'From: '.$email_from."\r\n".                    'Reply-To: '.$email."\r\n" ;                        // email message content                                                        $message="\n                     Hi Rebecca and Craig! \n                    There is a new order to fulfill.\n                    Please contact them to arrange payment.\n                           The order as follows:                _______________________________________________________________________                             Name: $custName                    Email: $email                    Address: $address                    Phone: $phone                       OrderID: $orderID                                   Product name: $prodName                     Order quantity: $qty                    Order price: $$price each                    Total: $$total.00                                   Grand Total: $$grandTotal.00                                            _______________________________________________________________________                                 Warm regards,\n                    Hermans Hive Hire                    ";                                          //send email                    $sentMailAdmin = mail($email_to, $subject, $message, $header); // mail() to admin email                             ?>              

Your assistance is much appreciated!

Link to comment
Share on other sites

You say an email is sent for each product, but from what you posted that's clearly not the case. So either the file itself is being executed for each product, or there's only one email.

 

I do see that you have all the product information in a loop but the email not in the loop. Your variables like $prodName used in the email will only be for the final product in the cart, since it gets overwritten each time through the loop.

 

If your problem is that you only receive one email containing one product then you need to build up the email content for each of the products as you go. In the loop.

Link to comment
Share on other sites

Here's the bit of the email that has the products:

OrderID: $orderID
Product name: $prodName
Order quantity: $qty
Order price: $$price each
Total: $$total.00
Grand Total: $$grandTotal.00
The order ID and grand total are for the whole order, but the rest in between are for an individual product. That's what needs to be repeated.

 

As really simple way of doing this would be to just build up a string with the email content - not doing it all at once at the end. Meaning something like

$message_products = "";

foreach ($content as $product_id=>$qty) {
	// ...

	$message_products .= "\n
Product name: $prodName
Order quantity: $qty
Order price: $$price each
Total: $$total.00
";
}
Then you put $message_products into the $message where the product information would go.
Link to comment
Share on other sites

Just in general, your code exemplifies why MVC is popular.

 

Your code seems to have a number of competing purposes:

 

1. It 'completes' an order by persisting the data to tbl_order (and in the process allocates an auto incremented order id#.

2. It then stores line item rows into tbl_order_product, and in the process, looks up pricing info and calculating a subtotal.

3. When this is complete it attempts to send an email summary to the customer.

 

There's quite a few things that aren't great about this mashup of code, not the least of which is that anything which might go wrong here will result in lost orders. What if the end user typo's their email address? What if the order gets spam filtered? What if your web server has a glitch and the script never completes correctly?

 

Certainly storing, updating and retrieving orders or order line items, are activities that your system will need to do in many places. This is why MVC is a popular solution, where your data persistence (model) is separated from display of all or portions of that data be it on a webpage, in a pdf document or sent in an email(views), and the actual url's that are provided in your site, are provided by the controller which orchestrates these activities.

 

What I would suggest at very least here, is begin by moving out all of your persistence (database insert/update/delete/select) code into separate script(s) that either are class based with methods, or at least in functions.

 

The same goes for your email code which should assume that it will be passed an order array that has all the order and line item information in it, for easy traversal and display.

 

When I create systems that involve emailing, I almost always use a queue system, where the sending of emails is based on a queued request. A "sending" script runs, most simply with command line php scripts being run by cron every minute, and they dequeue any requests and process them. This decouples the order saving process from the sending of the email. Most systems display the information to the end user in html form, or perhaps offer up a pdf for download.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.