Categories
Development

Send multiple SKU’s and Quantities to stripe from php

I have built a shopping cart and want to send multiple items to stripe. The page is populated using php and mysql.

I want to take each of the listed SKU’s and associated quantities from the table see below;

     <?php
$sql = "SELECT * FROM cart WHERE sess_id = '$sess_id'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {  
    echo "<table class='table table-striped'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th>Product Name</th><th>Product SKU</th><th>Quantity</th><th>Price</th><th>Remove</th>";
    echo "</tr>";
        echo "<tr>";
        echo "
        <td>".$row["prod_name"]."</td>
        <td>".$row["prod_sku"]."</td>
        <td>".$row["prod_qty"]."</td>
        <td>".$row["prod_price"]."</td>
        <td><a href='delete.php?record=".$row["id"]."' class='btn btn-danger'>Delete</a></td>";
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
} else {
    echo "<h3>Thankyou for visiting. You have no items in your cart.</h3><p>Return to the shop <a href='shop.php'>Click here</a></p>";
}
 ?>

and I want to insert them in the items array below. Everything works fine when I remove the comments and enter manually but of course carts change;

<script>

    var stripe = Stripe('pk_test_XXxxxxxxxxxxxxxxxxxxxxx');

    document.getElementById('checkout').addEventListener('click', e => {
        e.preventDefault();
            var items = <?php echo json_encode($items); ?>; // Pulls items from php varia
    stripe.redirectToCheckout({
        items: [//**I want to insert results here like they do below**
         //  {sku: 'sku_GKmcmMKCbgntH9', quantity: 1},
         //  {sku: 'sku_GKmZw6apqDvusM', quantity: 1},
          // {sku: 'sku_GJW8ks9YMQ51au', quantity: 1},

        ],
    successUrl: 'https:/mddddie.com.au/success',
    cancelUrl: 'https://mddddie.com.au/cancel',
  }).then(function (result) {
    // If `redirectToCheckout` fails due to a browser or network
    // error, display the localized error message to your customer
    // using `result.error.message`.
  });    
});

</script>

Thank you in advance for your help.

Leave a Reply

Your email address will not be published. Required fields are marked *