REST API Order Creation

Highlighted
Excursionist
12 1 0

When creating an order using the REST API (POSTing to api/2020-04/orders.json) I'm given the following error: 

{
    errors: {
        line_items: "expected Hash to be a Array"
    }
}

I thought I was passing the correct schema using the JavaScript payload which looks like this:

Annotation 2020-06-12 143320.png

Since that didn't work I tried rebuilding the line_items in the payload that is sent to Shopify:

$payload = $request->all();
$line_items = [];

foreach ($payload['order']['line_items'] as $line_item) {
    $line_items[] = $line_item;
}

$payload['order']['line_items'] = $line_items;

Same error there as well. Where am I going wrong here?

0 Likes
Highlighted
Excursionist
17 0 5

@codemonkey wrote:

When creating an order using the REST API (POSTing to api/2020-04/orders.json) I'm given the following error: 

{
    errors: {
        line_items: "expected Hash to be a Array"
    }
}

I thought I was passing the correct schema using the JavaScript payload which looks like this:

Annotation 2020-06-12 143320.png

Since that didn't work I tried rebuilding the line_items in the payload that is sent to Shopify:

$payload = $request->all();
$line_items = [];

foreach ($payload['order']['line_items'] as $line_item) {
    $line_items[] = $line_item;
}

$payload['order']['line_items'] = $line_items;

Same error there as well. Where am I going wrong here?


Can you get the $line_items output and paste here?

I had the same problem when I first tested it and this is how I structured it - $lineItem is the data object that I am retrieving from the source database, not related with the Shopify API.
Basically, each line item is supposed to be an array as well.  (Using PHP below)

$order['line_items'][] = [
    'title' => $lineItem['title'],
    'product_id' => $lineItem['product_id'],
    'variant_id' => $lineItem['variant_id'],
    'price' => $lineItem['price'],
    'quantity' => $lineItem['quantity']
];

Array output:

array (
  'line_items' => 
  array (
    0 => 
    array (
      'title' => 'title 1',
      'product_id' => 123456789,
      'variant_id' => 2345678900,
      'price' => 1.23,
      'quantity' => 1,
    ),
    1 => 
    array (
      'title' => 'title 2',
      'product_id' => 123456789,
      'variant_id' => 2345678900,
      'price' => 2.34,
      'quantity' => 1,
    ),
  ),
)

JSON to send to API:

{
	"line_items": [{
		"title": "title 1",
		"product_id": 123456789,
		"variant_id": 2345678900,
		"price": 1.23,
		"quantity": 1
	}, {
		"title": "title 2",
		"product_id": 123456789,
		"variant_id": 2345678900,
		"price": 2.34,
		"quantity": 1
	}]
}

 

1 Like
Highlighted
Excursionist
12 1 0

Thanks for helping me out! I just tried with the following payload and received the same response.

{
  "order": {
    "email": "---@gmail.com",
    "send_receipt": true,
    "line_items": [
      [
        {
          "variant_id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzYxNjg1MjEwNzMxOQ==",
          "quantity": 1
        }
      ]
    ],
    "transaction": {
      "amount": "49",
      "authorization": "gmvt8y0x"
    }
  }
}
0 Likes
Highlighted
Excursionist
17 0 5

See my line_items JSON sample and compare with yours - your line_items has array in array, it should look like this:

    "line_items": [
        {
          "variant_id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzYxNjg1MjEwNzMxOQ==",
          "quantity": 1
        }
    ],

 

0 Likes
Highlighted
Excursionist
12 1 0

Isn't that the same structure in my OP? I just tried this one, same error:

{
  "order": {
    "email": "----@gmail.com",
    "send_receipt": true,
    "line_items": [
      {
        "variant_id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzYxNjg1MzQ1MDgwNw==",
        "quantity": 1
      }
    ],
    "transaction": {
      "amount": "45",
      "authorization": "0dwqpm5z"
    }
  }
}
0 Likes
Highlighted
Excursionist
17 0 5

1- Is it exactly the same error?

2- Isn't variant_id supposed to be a numeric?

3- Does Order entity has a child object called "transaction"?

4- Can we add an order transaction record using the API other than marking the order with a payment status? Or do I get the following wrong in the API help page for Order?

Orders can be created through the API, but no payment information will be collected, and no transaction performed. You can mark the order with any payment status.

0 Likes
Highlighted
Excursionist
12 1 0
  1. Exactly the same error
  2. I don't know if variant_id being a number is required. 
  3. It does. You can see it in the example as well. Removing it doesn't change anything.

My guess is #2 may be the issue. I'm pulling the order information from the GraphQL API so I'm not given a numerical ID.

 

0 Likes
Highlighted
Excursionist
17 0 5
@codemonkey wrote:
  1. Exactly the same error
  2. I don't know if variant_id being a number is required. 
  3. It does. You can see it in the example as well. Removing it doesn't change anything.

My guess is #2 may be the issue. I'm pulling the order information from the GraphQL API so I'm not given a numerical ID.


I am talking in REST API context, not sure how things are different in GraphQL API. REST API page says variant_id is numeric.

I can't see "transfer" entity in the link you give. "transfers" is a linked entity on the other hand but I still don't think that you can create transfers by using API considering the note at the top of the Order help page, and also considering that Shopify shouldn't be letting us manage payment process or set up those information manually (due to being PCI compliance). I believe that's why they explicitly say "... no transaction performed. You can mark the order with any payment status".

0 Likes