Not sure if I’m understanding correctly, but it sounds like you just want to add an additional element to an existing array? If so, that can be done via a PHP function called array_push which the first parameter is the array to push items to, and every other parameter is the items to push. In your above example, you would do:
When creating an order, customers have multiple items in their cart.
I’m trying to add each product in PHP file to the $line_items
(the product names, prices, quantities etc).
But I have to do this dynamically. I tried this code below with your help:
$line_items = array();
-- START LOOP for all products in cart {
// Create an amount for this specific product
$base_price_money = new \Square\Models\Money();
$base_price_money->setAmount($price * 100);
$base_price_money->setCurrency('USD');
// Add information about it
$order_line_item = new \Square\Models\OrderLineItem(1);
$order_line_item->setName($title);
$order_line_item->setBasePriceMoney($base_price_money);
// Add this product to the order_line_items
array_push($line_items, $order_line_item);
// Continue loop and add more products as order_line_item
} // END of LOOP for all products in cart
...
$order->setLineItems($line_items);
But this gave error messages:
[category:Square\Models\Error:private] => INVALID_REQUEST_ERROR
[code:Square\Models\Error:private] => VALUE_EMPTY
[detail:Square\Models\Error:private] => Field must not be blank
[field:Square\Models\Error:private] => source_id
Any idea what I’ve done wrong? :S
I’m not 100% sure if this error message is because how I did the order_line_items above incorrectly.
Do you think the code above will work? any any feedback regarding the error message?
That error is not about the order…that sounds like you’re calling CreatePayment without a source_id set (the Order API doesn’t have a source_id). I won’t say 100% for certain the code will work, but it makes sense to me that it should