ana maria
ana maria

Reputation: 1

Automatically set Fulfillment status as prepared when a new order ID is added to Google Sheets using Zapier

I am trying to use Zapier so when I add a new order to Google sheets, the fulfillment status in shopify changes to Prepared.

Im using the Order ID to match the order in shopify, but the Order Name would also work.

I think that the problem is with the new version of the API in shopify, I took the code from the zapier community but it was using the 2023-10 version. I tried fixing it with chat GPT but could not figure it out.

Here is the code:

// Function to update fulfillment status in Shopify

const updateFulfillmentStatus = async (orderId) => { try { const shopifyStoreUrl = 'https://lamia-colombia.myshopify.com'; // Replace with your Shopify store URL const apiKey = 'a0e138b5f77ca5c4a621dc6a392f9286'; // Replace with your Shopify API key const password = 'd20729fa3e33a614a3ff3fc1f601f262'; // Replace with your Shopify password const newStatus = 'fulfilled';

const auth = Buffer.from(${apiKey}:${password}).toString('base64');

const response = await fetch(${shopifyStoreUrl}/admin/api/2024-04/orders/${orderId}/fulfillments.json, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': Basic ${auth} }, body: JSON.stringify({ fulfillment: { status: newStatus } }) });

const data = await response.json(); console.log('Response from Shopify:', data); // Log response from Shopify return data; catch (error) { console.error('Error updating fulfillment status:', error.message); if (error.response) { console.error('Response from Shopify:', error.response.data); // Log detailed error response if available else if (error.statusCode) { console.error('Status code:', error.statusCode); // Log status code if available } throw new Error('Failed to update fulfillment status in Shopify'); } };

Function to extract order ID from Google Sheet const extractOrderIdFromSheet = (rowData) => { Assuming the order ID is in the first column (index 0) of the Google Sheet return rowData[0]; };

Main function to handle Zapier trigger const updateShopifyFulfillmentStatus = async (sheetData) => { try { Extract order ID from the Google Sheet data const orderId = extractOrderIdFromSheet(sheetData);

Update fulfillment status in Shopify const updatedOrder = await updateFulfillmentStatus(orderId); console.log('Fulfillment status updated for order:', updatedOrder.fulfillment.id);

Define output object const output = { fulfillmentId: updatedOrder.fulfillment.id, status: updatedOrder.fulfillment.status };

return output; catch (error) { console.error('Error updating fulfillment status:', error.message); throw new Error('Failed to update fulfillment status in Shopify'); } };

Execute the function and store the result in output const output = await updateShopifyFulfillmentStatus(inputData); output;

// const response = await fetch(${shopifyStoreUrl}/admin/api/2024-04/orders/${orderId}/fulfillments.json, {

In this part of the code it was originally 2023-04, but I tried changing it to 2024-04 since thats what shopify is showing

Upvotes: 0

Views: 33

Answers (0)

Related Questions