Shopify’s Draft Orders feature is an essential tool for merchants, allowing them to create orders on behalf of customers. This is particularly useful for custom orders, phone orders, or situations where a customer needs assistance in placing their order. You can add products, apply discounts, and send invoices to customers, which they can pay online. Leveraging Shopify’s GraphQL API, you can create draft orders programmatically, and using Shopify Remix, a modern React-based framework, you can seamlessly integrate this functionality into your storefront.
In this tutorial, we’ll walk through how to create a draft order in Shopify using GraphQL in a Shopify Remix app.
Contents
Draft orders simplify the sales process by allowing merchants to:
This flexibility makes draft orders particularly useful for businesses that deal with custom orders or need to provide personalized service.
For demonstration purposes, we will use static data to simulate the customer order. Here’s an example of the static draft order data:
// Static draft order data (customer, line items, and attributes)
const draftOrderData = { customerId: "gid://shopify/Customer/123456789", email: "customer@example.com", note: "Custom order for VIP client", lineItems: [ { variant_id: "gid://shopify/ProductVariant/987654321", quantity: 2 } ], customAttributes: { name: "John Doe", email: "customer@example.com", message: "This is a special order" }, Tags: ["VIP", "CustomOrder"], };
Explanation:
To create a draft order, we need to define a GraphQL mutation. Here’s the mutation we’ll use:
graphql mutation draftOrderCreate($input: DraftOrderInput!) { draftOrderCreate(input: $input) { draftOrder { id invoiceUrl } userErrors { field message } } }
Explanation:
Now, let’s implement the action in our Remix app that will handle the draft order creation using the static data defined earlier.
Create a new route in your Remix app for creating draft orders:
// app/routes/create-draft-order.jsx
import { json } from '@remix-run/node'; import { authenticate } from '../shopify.server'; export async function action() { const { admin } = await authenticate.admin(); // Static draft order data const draftOrderData = { customerId: "gid://shopify/Customer/123456789", email: "customer@example.com", note: "Custom order for VIP client", lineItems: [ { variant_id: "gid://shopify/ProductVariant/987654321", quantity: 2 } ], customAttributes: { name: "John Doe", email: "customer@example.com", message: "This is a special order" }, Tags: ["VIP", "CustomOrder"], }; const draftOrderInput = { customerId: draftOrderData.customerId, note: draftOrderData.note, email: draftOrderData.email, lineItems: draftOrderData.lineItems.map(item => ({ variantId: item.variant_id, quantity: parseInt(item.quantity, 10), })), customAttributes: [ { key: "Name", value: draftOrderData.customAttributes.name }, { key: "Email", value: draftOrderData.customAttributes.email }, { key: "Message", value: draftOrderData.customAttributes.message } ], tags: draftOrderData.Tags, taxExempt: true, }; try { const response = await admin.graphql( `#graphql mutation draftOrderCreate($input: DraftOrderInput!) { draftOrderCreate(input: $input) { draftOrder { id invoiceUrl } userErrors { field message } } }`, { input: draftOrderInput } ); const result = response.data; if (result.draftOrderCreate.userErrors.length > 0) { console.error('Error creating draft order:', result.draftOrderCreate.userErrors); return json({ success: false, errors: result.draftOrderCreate.userErrors }, { status: 400 }); } console.log('Draft Order Created:', result.draftOrderCreate.draftOrder); return json({ success: true, draftOrder: result.draftOrderCreate.draftOrder }); } catch (error) { console.error('Failed to create draft order:', error); return json({ success: false, error: error.message }, { status: 500 }); } }
Explanation:
Now that the code is set up, you can test the draft order creation functionality by visiting the route you defined in your Remix app. Make sure to have a running development server and authenticate against your Shopify store.
Testing Steps:
Upon successful execution, the console log will display the following:
Draft Order Created: { id: “gid://shopify/DraftOrder/123456789”, invoiceUrl: “https://yourstore.myshopify.com/admin/orders/123456789/invoice” }
Creating draft orders using Shopify’s GraphQL API in a Remix application opens up various possibilities for enhancing customer service and streamlining order management. By following the steps outlined in this tutorial, you can build a robust feature that leverages the flexibility of draft orders.
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…