如何将购物车中的所有产品回显到paypal

发布于 2024-12-25 11:28:43 字数 3235 浏览 1 评论 0原文

我刚刚学习 php 的诀窍,正在为一个大学项目构建一个电子商务网站,我已经构建了整个网站并通过 paypal 设置了付款,这是我通过一些教程完成的。

当我转账到贝宝时我想显示;订单摘要中购物车中每件商品的商品名称、价格和数量。

在当前状态 http://pastie.org/3127790 中,代码仅显示购物车中的顶部商品在 paypal 的订单摘要中,我认为我需要实现一个 foreach 循环来告诉代码回显购物车中的所有行,但我不确定到底要循环什么以及如何循环。

任何帮助将不胜感激!

预先感谢,迈克尔

代码片段

      <?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>

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 "£" . $row['price'];?></td>
                <td><?php echo"£" .  $_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"£" .  $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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2025-01-01 11:28:43

我只回答你的问题。我不确定您所有代码的有效性。
我对您粘贴的一些代码进行了一些更改:

<?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 "£" . $ppprice; ?></td>
            <td><?php echo"£" . $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>

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 "£" . $ppprice; ?></td>
            <td><?php echo"£" . $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>
爱,才寂寞 2025-01-01 11:28:43

问题是如何为每个用户单独创建一个查看购物车,PayPal 链接为我网站上的所有用户创建一个全局购物车

The thing is how to create a view cart for each user seperately that paypal link creates a global cart for all users on my site

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文