download-icon
INPUT VARIABLES
{
shopDomain
}
The Shopify store domain (e.g., `your-development-store.myshopify.com`).
{
accessToken
}
The Shopify access token.
{
customerId
}
The identifier which could be either an email or a customer ID.
{
variantId
}
The ID of the product variant being ordered.
{
quantity
}
The quantity of the item being ordered (default will be 1).
{
}
share-icon
OUTPUT VARIABLES
{
Orderresponse
}
Full JSON response, make sure to stringify when accessing
{
orderId
}
{
orderEmail
}
{
orderConfirmationNumber
}
{
orderCreatedAt
}
{
orderSubtotalPrice
}
paths-icon
PATHS
{
success
}
{
error
}
{
}
{
}
{
}
{
}

Function Walkthrough

Function Code Snippet

 
export default async function main(args) {
  const logs = [];
  
  // Extract input variables from args
  const { accessToken, shopDomain, customerId, variantId, quantity } = args.inputVars;
  
  logs.push(`Extracted inputs - customerId: ${customerId}, variantId: ${variantId}, quantity: ${quantity || 1}, accessToken: ${accessToken ? 'PROVIDED' : 'MISSING'}, shopDomain: ${shopDomain}`);
  
  // Validate that the required input variables are provided
  if (!accessToken || !shopDomain || !customerId || !variantId) {
      logs.push("Error: Missing required input variables: accessToken, shopDomain, customerId, or variantId");
      return {
          next: { path: 'error' },
      };
  }
  
  // Define the endpoint URL
  const url = `https://${shopDomain}/admin/api/2024-01/orders.json`;
  logs.push(`URL: ${url}`);
  
  // Request payload for creating an order, ensuring that IDs and quantities are numbers
  
  const payload = {
      order: {
          line_items: [{ variant_id: Number(variantId), quantity: Number(quantity || 1) }],
          customer: { id: Number(customerId) }
      }
  };
  
  // Configure the fetch request
  const config = {
      method: 'POST',
      headers: {
          'Content-Type': 'application/json',
          'X-Shopify-Access-Token': accessToken,
      },
      body: JSON.stringify(payload)
  };
  
  try {
      // Make the API call
      logs.push(`Making API call to URL: ${url} with payload: ${JSON.stringify(payload)}`);
      const response = await fetch(url, config);
  
      // Check if the response status is OK
      if (!response.ok) {
          const errorMsg = await response.text;
          logs.push(`HTTP error! status: ${response.status}, message: ${errorMsg}`);
          throw new Error(`HTTP error! status: ${response.status}, message: ${errorMsg}`);
      }
  
      // Extract the JSON body from the response
      logs.push("Extracting JSON from the response");
      const responseBody = await response.json;
  
      // Validate the responseBody structure as expected
      if (!responseBody || !responseBody.order || typeof responseBody.order !== 'object') {
          throw new Error("Invalid or missing response body from the API");
      }
  
      // Extract required order details
      const order = responseBody.order;
      const orderId = order.id;
      const orderConfirmationNumber = order.confirmation_number || order.name; // Shopify uses 'name' as the order number
      const orderEmail = order.email;
      const orderCreatedAt = order.created_at;
      const orderCurrency = order.currency;
      const orderSubtotalPrice = order.subtotal_price;
      const orderStatusUrl = order.order_status_url;
  
      logs.push(`Order created: ID - ${orderId}, Confirmation Number - ${orderConfirmationNumber}`);
  
      // Create the success return object with extracted data
      return {
          outputVars: {
              Orderresponse: JSON.stringify(responseBody),
              orderId,
              orderConfirmationNumber,
              orderEmail,
              orderCreatedAt,
              orderCurrency,
              orderSubtotalPrice,
              orderStatusUrl
          },
          next: { path: 'success' },
      };
  } catch (error) {
      logs.push(`Error: ${error.message}`);
      return {
          next: { path: 'error' },
      };
  }
}
copy-icon

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.