Best answer

Mark Shopify fulfilment status as fulfilled

  • 24 January 2024
  • 6 replies
  • 183 views

I’m trying to update an order in Shopify as fulfilled.

I’m using the Javascript code by Zapier rather than the custom actions. But I’ve tried both.

I tried using both the order and the filfillment IDs.

Each order has multiple line items but I want to update the entire order.

 

This is the code I’m using. 

It runs and doesn’t return an error, but the order isn’t fulfiled.

(Oh, I added the output because Zapier requires it, but I really don’t need it)

Help….

 

const fulfillmentOrderId = 'f_ID';
const newStatus = 'FULFILLED'; // or 'partial'

const updateFulfillmentStatus = async () => {
const response = await fetch(
`https://*****.myshopify.com/admin/api/2024-01/fulfillment_orders/${fulfillmentOrderId}.json`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': '*******'
},
body: JSON.stringify({
"fulfillment_order": {
"id": fulfillmentOrderId,
"status": newStatus
}
})
}
);

const data = await response.json();
console.log(data); // Handle the response as per your requirements
};

updateFulfillmentStatus();


output=updateFulfillmentStatus()

 

icon

Best answer by Irit Levi 28 January 2024, 13:57

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

6 replies

Userlevel 7
Badge +14

Hi @Irit Levi 

Have you tried asking ChatGPT for help with formatting the code and troubleshooting the code?

 

Feedback from ChatGPT:

It looks like you're on the right track, but there might be a few things to consider when updating the fulfillment status of an order in Shopify using JavaScript, particularly when using the Shopify API.

  1. Check the API Endpoint: First, ensure you are using the correct API endpoint. The endpoint you're using seems to be for a single fulfillment order (fulfillment_orders). In Shopify, a fulfillment order is different from an order's fulfillment. If you want to update the fulfillment status of the entire order, you should target the fulfillments of that order. The endpoint typically looks like /admin/api/2024-01/orders/{order_id}/fulfillments.json.

  2. Ensure Proper Permissions: Make sure that the API access token you're using has the required permissions to update order statuses. Lack of proper permissions might not necessarily throw an error but will prevent changes from being made.

  3. Status Update Logic: Shopify's API might not allow direct status changes in the way you're attempting. Typically, to mark an order as fulfilled, you would create a fulfillment for that order. The status change is a result of this process, not a direct property update.

  4. Response Handling: Your updateFulfillmentStatus function is asynchronous, but your output assignment does not wait for it to complete. This might not be an issue if you're only logging the output but could be problematic if you need to use the output for further processing.

Here's a revised version of the function, targeting order fulfillment (note that you'll need the order ID, not the fulfillment order ID):

 
javascript
const orderId = 'your_order_id'; // Replace with the actual order ID const newStatus = 'fulfilled'; // Shopify uses 'fulfilled' status const updateOrderFulfillment = async () => { const response = await fetch( `https://*****.myshopify.com/admin/api/2024-01/orders/${orderId}/fulfillments.json`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': '*******' }, body: JSON.stringify({ "fulfillment": { "location_id": your_location_id, // Replace with your location ID "tracking_number": null, "line_items": [/* array of line item IDs to be fulfilled, if not all */] } }) } ); const data = await response.json(); console.log(data); // Handle the response as per your requirements }; updateOrderFulfillment();

 

Remember to replace your_order_id and your_location_id with the actual order ID and location ID. If you're fulfilling all line items, you don't need to specify them in the body.

Finally, always test with a non-critical order or in a development environment to ensure everything works as expected without affecting your live orders.

Thanks Troy,

 

I actually tried asking ChatGPT as well as Perpelxity. 

 

I tweaked the code a lot and came up with this 👇

However it’s still not working because it says it can’t find the order id, even though I pulled it using an api request, and also tried to hardcode it by pulling the ID from the order url /orders/5349614813373

 

// Replace YOUR_SHOPIFY_STORE, ACCESS_TOKEN, ORDER_ID, and LOCATION_ID with your actual values
const shopifyStore = '******';
const accessToken = 's****_********';
const orderId = '123456789'; // Replace with the order ID
const locationId = '123456789'; // Replace with the actual location ID

// Endpoint
const endpoint = `https://${shopifyStore}.myshopify.com/admin/api/2024-01/orders/${orderId}/fulfillments.json`;

// Replace with the necessary data for fulfillment
const fulfillmentData = {
fulfillment: {
// Add fulfillment details here
location_id: locationId,
// other fulfillment details...
}
};

// Make the API request
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
body: JSON.stringify(fulfillmentData),
})
.then(response => {
console.log('Order ID:', orderId); // Log the order ID
console.log('Endpoint:', endpoint); // Log the endpoint

console.log('Response Status:', response.status);
console.log('Response Headers:', response.headers);

if (response.status === 404) {
console.error('Order not found. Check the order ID.');
return Promise.reject(new Error('Order not found.'));
}

// Check if the response status is okay before attempting to parse JSON
if (response.ok) {
return response.json();
} else {
throw new Error(`HTTP error! Status: ${response.status}`);
}
})
.then(data => {
console.log('Fulfillment created:', data);
// Call the Zapier callback with the result
callback(null, data);
})
.catch(error => {
console.error('Error creating fulfillment:', error);
// Call the Zapier callback with the error
callback(error);
});

 

Userlevel 7
Badge +14

@Irit Levi 

Help articles for using Zap Code steps: https://zapier.com/apps/code/help

You may need to set an Input Variable, then declare the variable in the Code and reference it in the correct spot.

If you look at the code - I actually DID declare the Order ID.

 

const orderId = '123456789'; // Replace with the order ID

And I actually pull it from the other step so I declare it above the code itself.

I just hard coded it here to prove a point.

Neither works - hard coded or declared.

 

This is what is so frustrating

Here is a working solution for this code:

 

 

// Replace with your actual Shopify store details
const shopName = '******';
const apiKey = '********';
const password = 's***_*******';


// Construct the authentication string
const authString = `${apiKey}:${password}`;

// Base64 encode the authentication string
const base64Auth = Buffer.from(authString).toString('base64');


// Replace with the actual fulfillment order ID
const fulfillmentOrderId = 1234567890045;



// Construct the Shopify API endpoint
const endpoint = `https://${shopName}.myshopify.com/admin/api/2023-10/fulfillments.json`;

// Update parameters for the fulfillment
const updateParams = {
fulfillment: {
line_items_by_fulfillment_order: [
{
fulfillment_order_id: fulfillmentOrderId,
},
],
},
};

// In the fetch headers, use the base64-encoded authentication string
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${base64Auth}`,
},
body: JSON.stringify(updateParams),
});


// Parse the response
const responseData = await response.json();

// Check if the update was successful
if (responseData.errors) {
console.error('Failed to update fulfillment:', responseData.errors);
// Use the callback to pass the result to the next step in Zapier
output = [{ error: 'Failed to update fulfillment' }];
} else {
const fulfillmentId = responseData.fulfillment.id;
console.log('Fulfillment updated successfully. ID:', fulfillmentId);
// Use the callback to pass the result to the next step in Zapier
output = [{ fulfillmentUpdate: 'Fulfillment updated successfully', fulfillmentId }];
}

 

Userlevel 7
Badge +14

@Irit Levi 

Looks like you are using a static Order ID.

 

Inputs would need to be declared and referenced in the Code to make the Order ID dynamic.