How to add variant item for existing order id using REST API?
For Example:
'https://mystoreid.myshopify.com/admin/api/2024-04/orders/54829400064690.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'{"order":{"id":xxx,"line_items":[{"name": "6in Silver Bowl - CASE OF 12", "price": "240.00","product_id": xxx, "quantity": 1, "sku": "xxx-case", "title": "6in Silver Bowl", "variant_id": xxx, "variant_title": "CASE OF 12","variant_inventory_management": "shopify","requires_shipping": true, "product_exists": true,"fulfillable_quantity": 1,"grams": 3810}]}}',
CURLOPT_HTTPHEADER => array(
'X-Shopify-Access-Token: xxx',
'Content-Type: application/json',
'Cookie: cart_currency=USD; localization=US; secure_customer_sig='
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The above code is not working for adding item in existing order using rest API
To add a variant item to an existing order using the Shopify REST API, you need to update the order with new line items. Your PHP code example is mostly correct, but ensure you use the correct order ID and variant ID,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://mystoreid.myshopify.com/admin/api/2024-04/orders/54829400064690.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode(array(
"order" => array(
"id" => 54829400064690, // Ensure this is the correct order ID
"line_items" => array(
array(
"variant_id" => 123456789, // Ensure this is the correct variant ID
"quantity" => 1
)
)
)
)),
CURLOPT_HTTPHEADER => array(
'X-Shopify-Access-Token: your_access_token_here',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
} else {
echo $response;
}
curl_close($curl);
The line_items should include the variant_id and quantity of the item you want to add.