I’m trying to create a checkout using Checkout API, my app is a sales channel and has the access of “writing_checkouts“.
The product is listed on the sales channel but when I try to create a checkout, the request-response in 200, return a complete checkout object, but there is no field added like email, line_items or shipping address.
Here is the example code:
$field = array(
"checkout"=>array(
"email"=> "[email protected]",
"line_items"=> array(array(
"variant_id"=> 31520093667373,
"quantity"=> 1
)),
"shipping_address"=> array(
"first_name"=> "John",
"last_name"=> "Smith",
"address1"=> "126 York St.",
"city"=> "Ottawa",
"province_code"=> "ON",
"country_code"=> "CA",
"phone"=> "(123)456-7890",
"zip"=> "K1N 5T5"
)
)
);
$data = json_encode($field);
$url=$shopUrl.'/checkouts.json';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic Og==",
"accept: application/json",
"content-type: application/graphql",
"X-Shopify-Access-Token: ********************************",
),
CURLOPT_POSTFIELDS=> $postFields
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo $err;
} else {
echo $response;
}
and this is the response I’m gettin:
{"checkout":
{"completed_at":null,
"created_at":"2019-12-24T00:02:15-05:00",
"currency":"PKR",
"presentment_currency":"PKR",
"customer_id":null,
"email":null,
"name":"#11978400727085","note":"",
"order_id":null,
"order_status_url":null,
"subtotal_price":"0.00",
"token":"*******************************",
"total_price":"0.00",
"total_tax":"0.00",
"total_tip_received":"0.00",
"total_line_items_price":"0.00",
"updated_at":"2019-12-24T00:02:15-05:00",
"user_id":null,
"line_items":[],
"gift_cards":[],
"tax_lines":[],
"shipping_line":null,
"shipping_rate":null,
"shipping_address":null,
"credit_card":null,
"billing_address":null,
"applied_discount":null}
}
2
Answers
Line items are not an array of an array. They are an array of objects, or in other words, an array of hashed items. Try sending an array of objects instead, and it might work better. In JSON terms… you see [{},{},…] whereas it seems you are doing [[],[],…] which is close but not quite the same.
Perhaps I am missing something and the terminology of PHP is the problem, but I think array of array is wrong.
If anyone is looking for the answer here, I had the same issue and made the following changes: