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.

Re: Rejecting a discount code with discount function

Solved

Rejecting a discount code with discount function

LouiseEH
Shopify Partner
38 3 7

Hi,

I am looking into converting checkout scripts to functions as scripts will be deprecated August 2025.

 

When using scripts it was possible to prevent regular discount codes from being applied via the "reject" feature/function. C.f. https://shopify.dev/docs/api/functions/reference/product-discounts the reject feature is not available for functions. The documentation says that the workaround for "reject" not being available in functions is to: "Use a Product discount or Order discount function that contains the necessary logic. For example, you can use conditions to exclude a discount from applying."

As far as I can see, the discount functions do not have any information about applied discount codes in the input c.f. https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/input, which means I can only prevent the custom discounts in the functions from being applied, but I cannot prevent discount codes from being applied.
If that is correct, it is a problem for our scripts-to-functions-conversion, as I would like to reject all discount codes, if the custom discounts defined in the function applies, to avoid both the custom discounts and the discount codes to apply at the same time.


Anybody knows how to reject discount codes in functions?

Kind regards,
-Louise

Accepted Solution (1)
tobebuilds
Shopify Partner
476 34 128

This is an accepted solution.

Discount functions can only calculate discounts. They can't instruct Shopify whether or not to apply a specific discount.

 

However, Discount Allocator Functions are in dev preview, and they look like they'll be able to achieve this:

 

https://shopify.dev/docs/api/functions/reference/discounts-allocator

Founder, Regios Discounts app (4.9 stars, 68 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer

View solution in original post

Replies 4 (4)

LouiseEH
Shopify Partner
38 3 7

Ok I just found this post that suggests using a checkout UI extension to remove the discount via useApplyDiscountCodeChange hook c.f. https://community.shopify.com/c/extensions/is-it-it-possible-to-remove-discount-codes-from-a-cart-us....
That seems doable, but if it is possible via the functions or elsewhere, I would prefer that over the checkout UI extension - anybody knows another way to do it?

LouiseEH
Shopify Partner
38 3 7

Furthermore the documentation of the hook currently states "This method will return an error if the buyer is using an accelerated checkout method, such as Apple Pay, Google Pay, or Meta Pay." - which seems to be a big issue.

Haroon_Ahmed
Shopify Partner
3 0 0

Hi, LouiseEh! did you found a way to achieve this? I also want to reject some specific codes when the user enter any of these codes in the discount code field and show them a message "This coupon is valid for mobile application only.".
Any Idea how to achieve this ?

tobebuilds
Shopify Partner
476 34 128

This is an accepted solution.

Discount functions can only calculate discounts. They can't instruct Shopify whether or not to apply a specific discount.

 

However, Discount Allocator Functions are in dev preview, and they look like they'll be able to achieve this:

 

https://shopify.dev/docs/api/functions/reference/discounts-allocator

Founder, Regios Discounts app (4.9 stars, 68 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer