javascriptshopifyshopify-api

Shopify fulfillment of orders without tracking using JSON (in Javascript)


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

I’m using Javascript code.

I tried using both the order and the filfillment IDs.

Each order has multiple line items but I want to update the entire order. And the API documentation says if you update the fulfillment ID and not specify line items it will fulfill the entire order.

This is the code I’m using.

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

(Oh, I added the output because the tools I am using 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': 's****_******'
      },
      body: JSON.stringify({
        "fulfillment_order": {
          "location_id": "123456789",
          "id": fulfillmentOrderId,
          "status": newStatus
        }
      })
    }
  );

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

updateFulfillmentStatus();


output=updateFulfillmentStatus()

Solution

  • Here's the code I finally managed to get to work

    // 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 }];
    }