Source
Source

Reputation: 1021

Creating an array to send within an array for Stripe Session

I am trying to create the stripe session, but want to pass through multiple products to the line_items. I have an example of how it should look below, however I cant work out how to send multiple products.

    $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,
    ]);

So I wanted to create a variable that will do this

'line_items' => [$line_items],

And $line_items would equal

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

But I dont know how to create that variable, I got started on something like this, but it's obviously not going to work.

    $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)

What do I need to change in the foreach loop?

Upvotes: 1

Views: 1245

Answers (1)

UioSun
UioSun

Reputation: 59

I can't comment, the reputation limit is 50.

Why you can't use 'line_items' => $line_items?

If $_SESSION['cart_array'] has your shop item, foreach has return 2 dimensional array for you.

Maybe you can check data struct, add var_dump($_SESSION['cart_array']);var_dump($line_items);exit;.

All code:

    $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.

Upvotes: 1

Related Questions