Search code examples
phpmysqlforeachpaypalshopping-cart

how to echo all products in shopping cart to paypal


I am just learning the ropes of php and am building an e-commerce site for a uni project, I have the whole thing built and setup payment via paypal, which I've done through a few tutorials.

When I am transferring to paypal I want to display; the item name, price and quantity for each item in the cart, within the order summary.

In its current state http://pastie.org/3127790 , the code is only displaying the top item in the cart in the order summary on paypal, I think I need to implement a foreach loop to tell the code to echo all rows in the cart, but am unsure what exactly to loop and how.

Any help would be greatly appreciated!

Thanks in advance, Michael

Code snippet

      <?php

        $sql = "SELECT * FROM podcasts WHERE id_podcasts IN (";
          foreach ($_SESSION['cart'] as $id => $value){
            $sql .= $id . ",";  
          }
          $sql = substr($sql,0,-1).") ORDER BY name ASC";
          $query = mysql_query($sql);
          $total_price = 0;
          if(!empty($query)){
          while($row = mysql_fetch_array($query)){
            $subtotal = $_SESSION['cart'][$row['id_podcasts']]['quantity']*$row['price'];
            $total_price += $subtotal;
            ?>

            <?php
            $ppname = $row['name'];
            $_SESSION['cart'][$row['id_podcasts']]['name'] = $row['name'];
            $ppquantity = $_SESSION['cart'][$row['id_podcasts']]['quantity'];
            $ppprice= $row['price'];
            ?>
              <tr>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $_SESSION['cart'][$row['id_podcasts']]['quantity'];?></td>
                <td><?php echo "&pound;" . $row['price'];?></td>
                <td><?php echo"&pound;" .  $_SESSION['cart'][$row['id_podcasts']]['quantity']*$row['price'];?></td>
              </tr>
            <?php
          } }
          ?>


  <tr>
    <td></td>
    <td></td>
    <td><span>Total Price:</td></span>
    <td><span><?php echo"&pound;" .  $total_price;?></td></span>
  </tr>
</table>
</div>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="[email protected]"> <!-- change to your paypal address -->
<input type="hidden" name="quantity" value="<?php echo $ppquantity;?>"> <!-- do not change, since you refer to it all via the database -->
<input type="hidden" name="item_name" value="<?php echo $ppname;?>">
<input type="hidden" name="amount" value="<?php echo $ppprice;?>"> <!-- change here -->
<input type="hidden" name="shipping" value="0.00">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="custom" value="<?php echo $_SESSION['emailaddress'];?>"> <!-- if you store their purchase in a database, reference the database number here -->
<input type="hidden" name="return" value="http://shop.residencyradio.com/success.php">
<span class="input_btn"><input type="submit" name="purchase" value="Purchase" ></span>
</form>

Solution

  • I only answer your question. I don't sure about the effectiveness of all your code.
    I have made some changes in some of your pasted code:

    <?php
    $output_paypal = ''; // container for output for paypal form
    $total_items = 0;
    
    if ( ! empty($query))
    {
        while ($row = mysql_fetch_array($query))
        {
            $subtotal = $_SESSION['cart'][$row['id_podcasts']]['quantity'] * $row['price'];
            $total_price += $subtotal;
    
            $ppname = htmlspecialchars($row['name']);
            $_SESSION['cart'][$row['id_podcasts']]['name'] = $row['name']; // this assigment seems not useful
            $ppquantity = $_SESSION['cart'][$row['id_podcasts']]['quantity'];
            $ppprice = $row['price'];
    
            $total_items ++;
    
            $output_paypal .= <<<EOM
    <input type="hidden" name="item_name_{$total_items}" value="$ppname">
    <input type="hidden" name="quantity_{$total_items}" value="$ppquantity">
    <input type="hidden" name="amount_{$total_items}" value="$ppprice">
    EOM; // heredoc syntax (may not be any spaces or tabs before or after the semicolon.)
            ?>
            <tr>
                <td><?php echo $ppname; ?></td>
                <td><?php echo $ppquantity; ?></td>
                <td><?php echo "&pound;" . $ppprice; ?></td>
                <td><?php echo"&pound;" . $ppquantity * $ppprice; ?></td>
            </tr>
            <?php
        }
    }
    ?>
    
    
    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="business" value="[email protected]">
    
        <?php echo $output_paypal; ?>
    
        <input type="hidden" name="shipping" value="0.00">
        <input type="hidden" name="currency_code" value="GBP">
        <input type="hidden" name="custom" value="<?php echo $_SESSION['emailaddress']; ?>">
        <input type="hidden" name="return" value="http://shop.residencyradio.com/success.php">
        <span class="input_btn"><input type="submit" name="purchase" value="Purchase" ></span>
    </form>