I am developing Shopify Remix app using typescript for quote. I want to generate coupon code for draft order and want to apply it in checkout url generated for the draft order, the coupon code when applied it must check the product and quantity in quote order matches. How can we implement it? It would be great if anyone could help?
This is a common requirement for “Quote to Order” apps. The most reliable method is not to rely on the checkout URL to validate the code. Draft Order invoices act as static snapshots; they do not dynamically validate PriceRules like the online cart does.
You should validate the “Product + Quantity” logic inside your Remix app first, and then bake the result into the Draft Order using applied_discount.
The Workflow:
Validate in Remix: Your Typescript logic checks if the line_items meet your quantity/product rules.
Create Draft: If valid, create the Draft Order with the applied_discount field set explicitly.
Send URL: Return the invoice_url to the user.
Implementation:
export async function createQuoteDraft(admin: any, items: any[]) {
// 1. Run your validation logic here (e.g. Check if quantity > 50)
const isValidQuote = items.some(item => item.quantity >= 50);
const draftOrder = new admin.rest.resources.DraftOrder({session: admin.session});
draftOrder.line_items = items;
draftOrder.use_customer_default_address = true;
// 2. If valid, apply the discount directly to the object
if (isValidQuote) {
draftOrder.applied_discount = {
description: "Quote Pricing (Qty 50+)",
value_type: "percentage",
value: "10.0", // 10% off
title: "QUOTE-SPECIAL" // The code the customer sees
};
}
await draftOrder.save({ update: true });
// 3. Get the checkout link
return draftOrder.invoice_url;
}
If you want to give your quote orders a discount in Shopify and make sure it only works for the right products and quantities, here’s a simple way to do it:
Create a discount code using Shopify’s Price Rules API, setting it for the specific products and quantities in your quote.
Create a draft order with the same products and quantities.
Generate the checkout URL for that draft order and add the discount code like this:
checkout_url + “?discount=YOURCODE”
Shopify will automatically check that the order matches the conditions before applying the discount.