创建一个数组以在条带会话的数组内发送

时间:2019-06-19 03:09:47

标签: php arrays foreach stripe-payments

我正在尝试创建条纹会话,但希望将多个产品传递给line_items。下面有一个示例,显示了它的外观,但是我无法弄清楚如何发送多个产品。

    $session = \Stripe\Checkout\Session::create([
        'customer_email' => $this->customer_email,
        'payment_method_types' => ['card'],
        'line_items' => [[
        'name' => 'product1',
        'amount' => 1000,
        'currency' => 'usd',
        'quantity' => '1',
        ],
        [
        'name' => 'product2',
        'amount' => 1000,
        'currency' => 'usd',
        'quantity' => '2',
        ]],

        'success_url' => 'http://example.com/stripe-checkout/success.php',
        'cancel_url' => 'http://example.com/stripe-checkout/cancel.php',
        ], [
        'stripe_account' => $stripe_account,
    ]);

所以我想创建一个可以做到这一点的变量

'line_items' => [$line_items],

$ line_items等于

      [
        'name' => 'product1',
        'amount' => 1000,
        'currency' => 'usd',
        'quantity' => '1',
        ],
        [
        'name' => 'product2',
        'amount' => 1000,
        'currency' => 'usd',
        'quantity' => '2',
        ]

但是我不知道如何创建该变量,我从这样的事情开始,但是显然不起作用。

    $items = array();       
    foreach($_SESSION['cart_array'] as $item) {
        $item_id = $item['item_id'];
        $quantity = $item['quantity'];
        $qry = mysqli_query($this->con,"SELECT * FROM products WHERE products_id=$item_id");
        $row = mysqli_fetch_assoc($qry);
        $items[] = [
            'name' => $row['products_name'],
            'images' => [BASE.'images/products/viewed/'.$row['products_image']],
            'amount' => $row['products_price'],
            'currency' => 'gbp',
            'quantity' => $quantity,
        ];  
    }

$items_list = implode(',',$items)

在foreach循环中我需要更改什么?

1 个答案:

答案 0 :(得分:1)

我不能发表评论,信誉限制是50。

为什么不能使用'line_items' => $line_items

如果$_SESSION['cart_array']有您的商店商品,则foreach将为您返回二维数组。

也许您可以检查数据结构,添加var_dump($_SESSION['cart_array']);var_dump($line_items);exit;

所有代码:

    $items = array();       
    foreach($_SESSION['cart_array'] as $item) {
        $item_id = $item['item_id'];
        $quantity = $item['quantity'];
        $qry = mysqli_query($this->con,"SELECT * FROM products WHERE products_id=$item_id");
        $row = mysqli_fetch_assoc($qry);
        $items[] = [
            'name' => $row['products_name'],
            'images' => [BASE.'images/products/viewed/'.$row['products_image']],
            'amount' => $row['products_price'],
            'currency' => 'gbp',
            'quantity' => $quantity,
        ];  
    }

// Check data struct:
var_dump($_SESSION['cart_array']);
var_dump($line_items);
exit;

$items_list = implode(',', $items);  // Hi, you less ";" tag.