Search code examples
phpsessioncartshopping-cart

List shopping cart in phpmailer


I try to send an email with phpmailer after "Place Order". The problem is that I cannot list the shopping cart as an email (product, quantity, price of each product and total price).

echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);

This shows me that everything seems to work.

enter image description here

cart.php:

<tr>
    <?php
        $total = 0;

        if(!empty($_SESSION['cart'])){
            include 'config.php';

            $index = 0;

        if(!isset($_SESSION['qty_array'])){
            $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }

        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
    ?>
</tr>
<tr>
    <td>
        <img src="<?= $row['photo'] ?>" width="150px"><br />
        <?= $row['name'] ?>
    </td>
        <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
    <td>
        <?php echo $_SESSION['qty_array'][$index]; ?>
    </td>
    <td>
        <b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
    </td>
        <?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
    $index ++;
    }
    }
?>

phpmailer.php:

    echo var_dump($_SESSION['cart']);
    echo var_dump($_SESSION['qty_array']);

    foreach($_SESSION['cart'] as $key => $product) {
        $name = $product['name'];
        $price = $product['price'];
        $qty = $product['qty'];
        $tprice = $product['totalPrice'];
    }

    $mail->Body  = nl2br("$name\r\n$\r\n$qty\r\n$tprice"); 

This is not working at all. I tried a few things, and most of what worked was just the listing of the last product on the shopping cart list. But only the ID.

Edit:

I have tried another way, but still only the last item is listed.

phpmailer.php:

    $total = 0;
    if(!empty($_SESSION['cart'])){
    include 'config.php';
    $index = 0;
    if(!isset($_SESSION['qty_array'])){
    $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
    }
    $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
    $query = $conn->query($sql);
    while($row = $query->fetch_assoc()){
        $service = $row['name'];
        $qty = $_SESSION['qty_array'][$index];
        $qtyPrice = number_format($row['price'], 2);
        $qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
        $total += $_SESSION['qty_array'][$index]*$row['price'];

    $mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
    $index ++;
    }
    }

Solution

  • I found a solution:

    phpmailer.php

        ...
    
        $mail->Body  = nl2br("Hi {$_POST['name']} \r\n");
    
        /// List Cart Item(s) Start
        $total = 0;
        if(!empty($_SESSION['cart'])){
            include 'config.php';
            $index = 0;
            if(!isset($_SESSION['cart'])){
                $_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
            }
            $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
            $query = $conn->query($sql);
            while($row = $query->fetch_assoc()){
                $index;
                $service = $row['name'];
                $qty = $_SESSION['cart'][$index];
                $qtyPrice = $row['price'];
                $qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
                $total += $_SESSION['cart'][$index]*$row['price'];
            
            $mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
            
            $index ++;
            }
        }
        $mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
        /// Cart Item(s) End
    
        $mail->Body .= nl2br("Kind regards \r\n");
    
        ...