Dynamic multiple order_line_items

I’m trying to add dynamic multiple order_line_item to order and having hard time making it work.
Here is a sample of 2 products for example:

$order_line_item = new \Square\Models\OrderLineItem('322');
$order_line_item->setName('Product One');

$order_line_item1 = new \Square\Models\OrderLineItem('616');
$order_line_item1->setName('Product two');

$line_items = [$order_line_item, $order_line_item1];
....
$order->setLineItems($line_items);

I’m creating a PHP loop for each product. Any suggestions on how to add multiple dynamically ?
Can I for example do this?

$order_line_item = new \Square\Models\OrderLineItem('322');
$order_line_item->setName('Product One');
$line_items = [$order_line_item];

$order_line_item = new \Square\Models\OrderLineItem('616');
$order_line_item->setName('Product two');
$line_items  =  $line_items   +   [$order_line_item];
...
$order->setLineItems($line_items);

Thanks.

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:

array_push($line_items, $order_line_item);

Note this is PHP functionality, nothing specific to Square.

1 Like

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? :confused:

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 :slight_smile:

1 Like

I was testing with multiple quantities and forgot to take that in to account.
Thanks a bunch!!! =)