如何将购物车会话变量发送到电子邮件

时间:2019-02-18 14:58:18

标签: php e-commerce

我有一个电子商务网站,我希望将购物车中的物品发送到邮件中,以便可以完成订购。我在结帐页面上有我的购物车会话,一旦单击“下单”按钮,它将转到另一个执行php进程的页面。我没有错误,但是它仅将购物车中的最后一个项目发送到邮件,而不是购物车中的所有项目。以下是我用来将购物车商品带到结帐页面的代码

<?php
session_start();
?>
<?php
$product_ids = array();
if(isset($_POST['checkout'])){  
if(isset($_SESSION['shopping_cart'])){
$count = count($_SESSION['shopping_cart']); 
$product_ids = array_column($_SESSION['shopping_cart'], 'id');
if (!in_array(filter_input(INPUT_GET, 'id'), $product_ids)){
$_SESSION['shopping_cart'][$count] = array
(       
'id' => filter_input(INPUT_GET, 'id'),
'name' => filter_input(INPUT_POST, 'name'),
'price' => filter_input(INPUT_POST, 'price'),
'qty' => filter_input(INPUT_POST, 'qty'),
'img' => filter_input(INPUT_POST, 'img'),   
);
echo "Product Added to cart";
}else{
echo "Product Already added to cart";       
}   
}else{
$_SESSION['shopping_cart'][0] = array
(
'id' => filter_input(INPUT_GET, 'id'),
'name' => filter_input(INPUT_POST, 'name'),
'price' => filter_input(INPUT_POST, 'price'),
'qty' => filter_input(INPUT_POST, 'qty'),
'img' => filter_input(INPUT_POST, 'img'),
);
}
}
?>

下面是从会话中获取每个项目的代码

  <?php
$total =0;  
foreach($_SESSION['shopping_cart'] as $key => $r):
?>

<?php
 $prodname = $r['name'];
 $prodprice = $r['price'];
 $prodqty = $r['qty'];

?>

<?php
$total = ($r['qty'] * $r['price']);
?>

<?php
endforeach;
?>


<?php
$totalall=0;
foreach($_SESSION['shopping_cart'] as $key => $r):
$totalall = $totalall +  ($r['qty'] * $r['price']);
?>

<?php
endforeach;
?>

这是将项目发送到邮件的代码

<?php
include('Mail.php');

$username = 'username';
$password = 'password'; 

$from = "";
$to = "";
$subject = "$subject";
$body= "
$prodname;
$prodprice;
$prodqty;
$prodtotal;
";

$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' =>'localhost', 'auth' => true, 
'username' => $username, 'password' => $password, 'port' => '25'));
$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)){
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("Order Completed Successfully");
}
?>

我不介意更改代码,我只需要这样做即可。谢谢

0 个答案:

没有答案