Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.

Applying a DiscountCode to an Order or Draft Order

Bill6
Shopify Partner
47 0 17

Is there a way to apply a pre-existing DiscountCode when creating an Order/DraftOrder using the API? 

 

For example, I would expect to pass an id of an existing DiscountCode into an Order/DraftOrder and have it apply to the order based on the associated PriceRule.  Just like we do with variant_id for lineItems.  We are not forced to construct all the variant details when adding as a LineItem.  Correct?  What gives with DiscountCodes?

 

It appears we have to construct the all discount and price rule details as a custom object in the order every time.  Tell me it ain't so!  What good are DiscountCodes if they can't be pre-defined AND applied to orders with a simple reference to the DiscountCode.  Am I missing something here?

Replies 10 (10)

KB77
Shopify Staff
59 6 11

Hi @Bill6 ,

You are correct, it is necessary to include the structured details when creating new draft/orders with specific codes as per our guides on using the REST Admin API, or GraphQL Admin API.

That being said, what you are describing sounds like a use case for automatic discounts. You can check our examples here, if you're seeking a more automated approach this may be a better solution for your needs.

Hope this helps.

KB77 | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Bill6
Shopify Partner
47 0 17

We do not want automatic discounts.  That simply paints us into another corner.  But thanks for confirming. 

 

Just FYI, we want to simply create orders with the API that use pre-built discounts and have the discount usage tracked.  So, you can see that the bigger problem is tracking the discount usage.  You cant use "single-use" discounts or get any accurate metrics on discount usage.  There is no way around this.  Hopefully the Shopify API developers will recognize this as a design flaw and improve.

 

I expect that I am not the only API consumer that would like to create orders with the admin API and have the discount codes applied and tracked.  This seems like a must have for anyone.

damithMA
Visitor
1 0 0

Hey @Bill6 
Did you find a solution to this one? 

Bill6
Shopify Partner
47 0 17
No. I did not.
RafayBetalogics
Shopify Partner
4 0 0

hey @Bill6 
did you find any alternative to this problem, as I also want to apply discount codes when creating the order through api.

 

Skylitech
Shopify Partner
9 0 5

Hi @KB77 ,

Actually, @Bill6 is right. 

If we can't use the pre-existing Discount code via API, how can we track the usage of the Discount Code? It is a mandatory option during order creation.

It is a Shopify API designing glitch and a must-required feature.

ShopifyDevSup
Shopify Staff
1244 201 440

Hey @Bill6 and @Skylitech , thanks so much for these observations and feedback. 

In the case of draft orders, the functionality will emulate what's available in the admin when creating draft orders where you can only apply a discount to the order, not a discount code. For tracking purposes, you may want to use an order attribute or tag to specify an equivalent discount code. 

If you're importing orders through the REST api, you can reference the discount code that was used for the order (for tracking purposes) but you will still need to specify the amount and type in the transaction fields. The reason for that is you can specify any discount code that was used, it doesn't need to be an active code (important when importing orders from different sales channels or when migration from a different platform). The only time the discount code is calculated using the shops current active discount codes and price rules is when the order passes through checkout. 

You can use the GraphQL cart or checkout endpoints through the Storefront API to apply a discount to an order so that they will be applied and calculated properly when the customer checks out. 

Hope that helps clarify the limitations with discounts. 

 

- Kyle G. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ffgamba7
Tourist
4 0 2

Hi Kyle,

 

I'm a customer service manager and our company recently transitioned to Shopify. I'd like my agents to be able to use Draft Orders to take client orders over the phone as this would be much quicker than placing them on the front end of the site and we can easily associate them with client accounts this way. Clients could just provide the code they have to our agents over the phone and then the agent could enter this code into the draft order.

 

I'm trying to parse the above but what I think I've read is that there is not a way to apply an existing discount code to a draft order? We also need this for tracking purposes as well as removing manual error potential by entering an incorrect discount amount. Is there a plug in or app that you know of that would allow us to do this? 

Thank you!
Forrest

ffgamba7
Tourist
4 0 2

@ShopifyDevSup Sorry, looks like I didn't tag here.

ShopifyDevSup
Shopify Staff
1244 201 440

Hey @ffgamba7 thanks for sharing that additional context. The best solution here if you're on a Plus plan would be to look in to our B2B features as you can set up specific catalog and pricing for individual clients, eliminating the need for discount codes at all for them. https://help.shopify.com/en/manual/b2b 

As for apps, searching for wholesale or b2b will return apps that are tailored to custom solutions for specific clients and pricing. 

Hope that helps. 

- Kyle G. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog