我面临的问题是如何在数组中添加多个项目以将数据发送到paypal我使用以下脚本进行购物车和paypal集成。当我在我的购物车中添加一个项目时,它工作正常,但是当我尝试添加多个项目时,它会在购物车中添加商品,但它只会将最后一个项目发送到PayPal进行交易。以下是我试图解释http://anushn.hostingsiteforfree.com/ecommerce/index.php的实例。任何形式的帮助将不胜感激。
<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
$total_quantity = count($_SESSION["cart_array"]);
}
header("location: cart.php");
exit();
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 (If user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['item_to_remove']) && ($_POST['item_to_remove'] != ""))
{
$key_to_remove = $_POST['item_to_remove'];
if(count($_SESSION['cart_array']) <= 1)
{
unset($_SESSION['cart_array']);
}
else
{
unset($_SESSION['cart_array'][$key_to_remove]);
sort($_SESSION['cart_array']);
}
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 (render the cart for the user to view)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cart_output="";
$cart_total = "";
$pp_checkout_btn = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
$cart_output = "<h2 align='center'>Your Shopping Cart Is Empty</h2>";
}
else
{
// Start the For Each loop
$i=0;
foreach($_SESSION["cart_array"] as $each_item)
{
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql))
{
$product_name = $row['product_name'];
$price = $row['price'];
$detail = $row['details'];
$cat = $row['category'];
$sub_cat = $row['subcategory'];
$cover = $row['cover_location'];
}
$total_item_price = $price * $each_item['quantity'];
$total_item_price2 = number_format($total_item_price, 2);
$cart_total = $total_item_price + $cart_total;
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
$cart_output .= "<div class='cart_output'>";
$cart_output .= "<h2>Cart Item $i</h2>";
$cart_output .= "<img src=$cover width='100' height='100'><br/><br/>";
$cart_output .= "Item Name : " . $product_name . "<br/>";
$cart_output .= "Unit Price : $" . $price . "<br/>";
$cart_output .= "Total : $" . $total_item_price2 . "<br/>";
$cart_output .= "<br /><form action='cart.php' method='POST' name='' id =''>
<input type='submit' name='deleteBtn"."$item_id' id='' value='Remove Item'>
<input type='hidden' name='item_to_remove' id='' value='$i'>
</form>
";
$cart_output .= "</div>";
$i++;
}
$cart_total = number_format($cart_total,2);
//Start PayPal Checkout Button
$POST_DATA = array(
"METHOD" => "BMCreateButton",
"VERSION" => "65.2",
"PWD" => "090078601",
"USER" => "mystore.yahoo.com",
"SIGNATURE" => "adadaadadada",
"BUTTONCODE"=> "ENCRYPTED",
"BUTTONTYPE"=> "BUYNOW",
"BUTTONSUBTYPE" => "SERVICES",
"BUTTONCOUNTRY" => "US",
"BUYNOWTEXT" => "PAYNOW",
"BUTTONIMAGE" => "REG",
"BUTTONIMAGEURL" => "https://www.paypalobjects.com/webstatic/en_US/btn/btn_pponly_142x27.png",
"L_BUTTONVAR0" => "bussiness=mustafazahid43@yahoo.com",
"L_BUTTONVAR1" => "item_name=$product_name",
"L_BUTTONVAR2" => "amount=$price",
"L_BUTTONVAR3" => "custom=$product_id_array",
"L_BUTTONVAR4" => "notify_url=http://yoursite.com/ecommerce/storescripts/my_ipn.php",
"L_BUTTONVAR5" => "cancel_return=http://yoursite.com/ecommerce/paypal_cancel.php",
"L_BUTTONVAR6" => "return=http://yoursite.com/ecommerce/checkout_complete.php",
"L_BUTTONVAR7" => "quantity=$each_item[quantity]",
"L_BUTTONVAR8" => "upload=0",
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($POST_DATA),
'timeout' => 10,
),
));
$response = file_get_contents("https://api-3t.sandbox.paypal.com/nvp/",true,$context);
parse_str($response, $output);
$button = str_replace("\\","",$output["WEBSITECODE"]);
}
?>
<html>
<body>
<div class="container">
<div class="sub_container">
<?php echo $cart_output; ?>
<br/>
<br>
<?php echo $button;?>
<div style="margin-top:20px; color:#CCC; float:right;"><b><?php if($cart_total != "") {echo "Total Shopping : $".$cart_total." USD";} ?></b></div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
BMCreateButton适用于单个项目。如果您想使用PayPal的购物车,则需要创建购物车按钮而不是“立即购买”按钮,并为每个商品设置一个按钮。然后你还有一个单独的View Cart按钮,但是那个购物车都将由PayPal驱动。
这真的不是我推荐的。由于您已经拥有自己的购物车,所以您需要做的就是将数据发送到PayPal。您可以使用“cart upload command”方法轻松完成此操作,这正是它的目的。
您将完成您在此处所做的工作,但您只需构建一个包含隐藏输入变量的HTML表单,而不是构建NVP字符串并进行API调用。您可以参考PayPal's standard variable reference了解可以包含的内容的详细信息。
更好的方法是使用Express Checkout API,因为您熟悉API调用。由于您正在使用PHP,我建议您查看我的class library for PayPal。它可以使Express Checkout非常快速,轻松地集成。