Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Create / Apply Discount through Storefront API

Create / Apply Discount through Storefront API

mandertx
Shopify Partner
5 0 2

Hello. I am reading the documentation on Storefront API and I'm incredibly confused. I want to apply a discount with a dynamic amount based on the items in my cart. How do I do this? Can I do this without creating a fixed code in shopify admin? Because then the customers can reuse the code.

Please give me an in-depth explanation. I have created a custom app to do this for me and it works, but I found out later that I cannot use shopify extensions on custom distribution, and can only use public distribution for it to work.

Replies 5 (5)

Liam
Community Manager
3108 344 892

Hi Mandertx,

 

Creating a dynamic discount based on the items in a cart on Shopify requires using various APIs and scripting methods. These can include:

  • The Product Discount Function API allows developers to create a new type of discount that can be applied to specific products or product variants in the cart. This can include offering discounts on certain items based on the quantity purchased or applying discounts to particular products or product variants.
  • The Storefront API allows developers to interact with the cart during a customer's session. This includes the ability to update cart line items and retrieve a checkout URL. Additionally there's a method `applyCartDiscount` in the Cart API which can be used to apply a cart level discount.
  • Custom Apps with the Admin API - For more complex and dynamic discounting schemes, creating a custom app and leveraging the Admin API might be a viable option. Through this method, you can monitor the cart's contents and calculate discounts accordingly. However, the discounts would have to be predefined in the Shopify Admin as discount codes or automatic discounts, but the custom app can determine when and how these discounts are applied based on the cart's contents.

Hope this helps!

Liam | Developer Advocate @ 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

marko82
Shopify Partner
11 0 3

Hi Liam,

 

method applyCartDiscount` in the Cart API is exactly what I need but not sure how to use it since the link you gave leads to POS documentation and I need to use it inside online? Is applyCartDiscount part of Storefront API? Is there any other resource that you can point me to? 

 

Thank you

EugeneBoruhov
Shopify Partner
3 0 0

Hi @Liam! I have the same question about discount application. Do we have an easy way to apply a custom discount (not a Discount Code) to the Cart or Line Item? What I'm looking for is an API that allows me to apply a discount in our frontend app after the customer has added certain products to the cart. It's quite custom logic, so we can't use a Discount Code. As @marko82 mentioned, applyCartDiscount` in the Cart API is what we ideally need, but it's not an API we can use in our app.

Ollie_Holt
Shopify Partner
1 0 0

I think this is what you are looking for in the Storefront API:

 

https://shopify.dev/docs/api/storefront/2024-10/mutations/cartDiscountCodesUpdate

 

EugeneBoruhov
Shopify Partner
3 0 0

This is part of the Discount Code API. I was looking for an alternative way because to apply a custom discount, we have to do it on the fly, which means creating a Discount Code at some point during the purchase. That’s very difficult and has a lot of disadvantages, like the need for a cleanup script. After some research, I realized that the only way to do this is to write a Shopify extension based on the Cart API, which, unfortunately, is also not easy but more clear and straightforward.