Add a File attachment to the properties of a draft order using REST DraftOrder API

Add a File attachment to the properties of a draft order using REST DraftOrder API

MuskokaGeek
Shopify Partner
9 0 4

I can successfully create a draft order using the DraftOrder API. However, when the order has an uploaded file attached, it fails to upload and attach the file to the property field of the draft order. Here is the PHP code that runs after the initial draftorder is created. It is attempting to update the draft order with the uploaded file. I have so far (error checking removed for simplicity):

 

 

$file_data = file_get_contents($_FILES['properties']['tmp_name']['customphoto']);
$result = shopify_call($access_token, "https://{$shopifyShop}/admin/api/{$shopifyApiVersion}/assets.json", 
		json_encode(['asset' => ['attachment' => base64_encode($file_data)]]), 'POST');

$file_info = json_decode($result);
$file_url = $file_info['asset']['public_url'];

// Step 3: Associate the file URL with the DraftOrder
$draft_order_update_data = [
    'draft_order' => [
        'note_attributes' => [
            [
                'name' => 'file_url',
                'value' => $file_url,
            ],
        ],
    ],
];
$result = shopify_call($access_token, "https://{$shopifyShop}/admin/api/{$shopifyApiVersion}/draft_orders/{$draftOrderID}.json", 
		json_encode($draft_order_update_data), 'PUT');
$updateResponse = json_decode($result);

 

 

It currently returns a 404 Not Found error, when attempting to upload the file. What am I doing wrong?

 

Replies 0 (0)