如何将购物车中的所有产品回显到paypal
我刚刚学习 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 "£" . $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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只回答你的问题。我不确定您所有代码的有效性。
我对您粘贴的一些代码进行了一些更改:
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:
问题是如何为每个用户单独创建一个查看购物车,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