Becky
Becky

Reputation: 2275

Only one product is listing in php email even with multiple products in order

I created a php email to send out after an order has been placed. I'm not sure what I am doing wrong with this because if I have more than one different type of product, the email will only send the first one added. The transaction receipts that are sending from Authorize.net include all of the products that are with the order, but not my email. I am not getting any errors and the email is sending fine.

$to = $email;
$subject = 'Your Example order';
$message = '<img src="'.$logoImage.'">';
$message = '
<html>
<head>
  <title>Example Order</title>
</head>
<body>
  <p>Thank you for ordering with us!</p>
      <p>Your order was successfully sent to us and we will start processing it immediately.</p><br><br>
      <p>A charge of $'.$total_price.' was taken from '.$billToName.'\'s account that was sent with the order.
      <p>Your order contained:</p>
      <p> '.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>
      <p>'.  $AuthorrizeResponseText.';</p><br><br>
      <p>We really appreciate your business!</p>
</body>
</html>
';
$from = "[email protected]";
$cc = "[email protected]";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";

// Send the email
mail($to,$subject,$message,$headers);

The line I am having issues with is this...

<p> '.$prdqty.' - '.$prdsize.' - '.$prdname. $post_values.'</p>

I need it to include all of the different products that were in the order and not just the one. I grab the products in my site the same way. Output them from my db and then carry them through a session throughout the site. I'm also sending the email carrying the same session. This is how the variables are structured...

 foreach($_SESSION['shopping_cart'] as $id => $product) {
        $product_id = $product['product_id'];
        $prdname =  $products[$product_id]['product_name'];

Is there a way I can do this or something I am missing doing?

UPDATE: I wrote this code, but it is only including the part after

<p>'.  $AuthorrizeResponseText.';</p><br><br>
      <p>We really appreciate your business!</p>


$to = $email;
$subject = 'Your Example order';
$message = '<img src="'.$logoImage.'">';
$message = '
<html>
<head>
  <title>Example Order</title>
</head>
<body>
  <p>Thank you for ordering with us!</p>
      <p>Your order was successfully sent to us and we will start processing it immediately.</p><br><br>
      <p>A charge of $'.$total_price.' was taken from '.$billToName.'\'s account that was sent with the order.
      <p>Your order contained:</p><br><ul>';
 foreach($_SESSION['shopping_cart'] as $id => $product) {
        $product_id = $product['product_id'];
        $prdname =  $products[$product_id]['product_name'];
        $prdprice =  $products[$product_id]['price'];
        $prdqty = $product['quantity'];
        $prdsize = $product['size'];
$message .= '<li>' .$prdqty.' - '.$prdsize.' - '.$prdname.'</li><br>';              
                    }
$message .= '</ul>';
$message ='

      <p>'.  $AuthorrizeResponseText.';</p><br><br>
      <p>We really appreciate your business!</p>
</body>
</html>
';
$from = "[email protected]";
$cc = "[email protected]";

Any ideas on what is wrong with that??

Upvotes: 0

Views: 124

Answers (1)

Aleksandar
Aleksandar

Reputation: 654

You can include foreach loop between parted $message variable.I don't know your db structure and what your session contains, but try something like this

$message = '<img src="'.$logoImage.'">';
$message .= '
<html>
<head>
  <title>Example Order</title>
</head>
<body>
  <p>Thank you for ordering with us!</p>
      <p>Your order was successfully sent to us and we will start processing it immediately.</p><br><br>
      <p>A charge of $'.$total_price.' was taken from '.$billToName.'\'s account that was sent with the order.
      <p>Your order contained:</p>';
foreach( $_SESSION['shopping_cart'] as $product ) {
     $product_id = $product['product_id'];
     $prdname =  $product[$product_id]['product_name'];
     // And so on for each field you need
     echo '<p> '.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';
}

$message .= '<p>'.  $AuthorrizeResponseText.'</p><br><br>
      <p>We really appreciate your business!</p>
</body>
</html>
';

Don't forget $var = "value" is for giving value to variable, but $var .= "add more" is for adding new value and keep old. If you just put = again, old value will be replaced by new one.

Upvotes: 1

Related Questions