php paypal沙盒动态列表

标签 php paypal sandbox

我正在尝试通过对多种产品使用沙箱来制作 Paypal 按钮。 当我在购物车中添加产品并按下 paypal 按钮时,它工作正常。 但是,如果我在购物车上制作两个或三个产品并按下按钮, 它出现消息,“您的购物车是空的”。 我认为问题出在动态列表中。 你能帮我点忙吗?动态列表使用 foreach 语句。老实说,在这个 foreach 语句中,其他代码工作正常。请帮我。谢谢。

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                    <input type="hidden" name="cmd" value="_cart"/>
                    <input type="hidden" name="upload" value="1"/>
                    <input type="hidden" name="business" value="wholee1@googlemail.com"/> 
                    <input type="hidden" name="notify_url" value="XXX">
                    <input type="hidden" name="return" value="http://localhost/eshopProject/home.php">
                    <input type="hidden" name="rm" value="2">
                    <input type="hidden" name="cbt" value="Return to The Store">
                    <input type="hidden" name="cancel_return" value="http://localhost/eshopProject/android.php">
                    <input type="hidden" name="lc" value="UK">
                    <input type="hidden" name="currency_code" value="GBP">
                    <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
                    <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
                    <?php  echo $pp_checkout_btn; ?>
                </form>


//added more php sources here
<?php
//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(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
    //if cart is empty or not set, run it
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
}
else{
    //if 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){
                //this item is in cart already, then it can be adjusted the quantity using array_splice()
                array_splice($_SESSION["cart_array"], $i-1, 1, 
                array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                $wasFound = true;
            }
        }
    }
    if($wasFound == false){
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    }
}
header("location: cart.php");
exit();
}
?>


//php code
    $pp_checkout_btn = '';
    $i = 0;
    foreach($_SESSION["cart_array"] as $each_item){   
    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM product WHERE productId='$item_id' LIMIT 1");
        while($row = mysql_fetch_array($sql)){
            $category = $row["product_category"];
            $product_name = $row["product_name"];
            $brand = $row["product_brand"];
            $price = $row["product_price"];
        }    
    $priceTotal = $price * $each_item['quantity'];
    $cartTotal = $priceTotal + $cartTotal;
    $x = $i + 1;
    //dynamic Checkout Button
    $pp_checkout_btn = '<input type="hidden" name="item_name_'.$x.'" value="'.$product_name.'">
        <input type="hidden" name="amount_'.$x.'" value="'.$price.'">
        <input type="hidden" name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
     }

最佳答案

您似乎忘记了在 foreach 中递增 $i。这就是为什么您一直只生成一个项目($x 始终等于 1,除第一个元素外的所有元素都将被忽略)。

我建议将 $x = $i + 1; 替换为 $x =++$i;。或者只是执行 $i++ 并在输入的形成代码中使用 $i 而不是 $x

关于php paypal沙盒动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491112/

相关文章:

php - 如何从另一个 div 中获得相同的、只是随机播放的内容

php - 如何不让用户在mysql中保存相同的项目两次

paypal - 为 PayPal Marketplace (API) 自定义 "PayPal"按钮

php - WordPress:函数声明 WC_Gateway_PayPal_Pro_PayFlow::get_post_data($order) 应该与 WC_Settings_API::get_post_data() 兼容

java - Paypal 自动充值

android - Android 中的静态变量真的是 "global"(系统范围)吗?

c++ - Visual Studio Code 看不到系统上安装的库

php - 在 PHP 中声明变量有问题吗?

Java-Sandbox 示例抛出 java.lang.NoClassDefFoundError

PHP MySQL 搜索标题和多个标签顺序