Shopify Functions (Product Discount API, Order Discount API)

Topic summary

A developer needs to implement a discount scenario where:

Core Requirements:

  • Apply a discount to only ONE instance of product “A” when added to cart (even if multiple quantities exist)
  • Block users from entering discount codes at checkout when product “A” is present
  • Prevent automatic discounts from applying

Migration Challenges from Shopify Scripts:

The developer identifies two documentation issues:

  1. Line-item targeting limitation: Product Discount API only supports discounts by “Product Variant ID” and “Quantity”, not by specific line items with properties like “discount_target”. This makes it impossible to discount just one instance when multiple identical variants exist in cart.

  2. No exclusion method: Neither Product Discount API nor Order Discount API provides a direct method to exclude/cancel other discounts. Documentation mentions “use conditions to exclude a discount” but no actual exclusion mechanism exists in the FunctionResult.

Proposed Workarounds:

A responder suggests:

  • Using Storefront API to add checkout validation rules (requires custom storefront, won’t work with default Shopify checkout)
  • Creating conditional logic in the function to prevent discount creation under certain conditions

The developer asks for clarification on preventing automatic discounts and whether Checkout UI Extensions with useBuyerJourneyIntercept could block checkout progression.

Summarized with AI on November 16. AI used: claude-sonnet-4-5-20250929.

Hello.
We need to realize the following scenario.
We have a specific product “A”, when adding it to the cart we need to apply a discount using Shopify Function.
The discount should only apply to one specific line item “A”, meaning that if there are 2 line items “A” in the cart, the discount will only apply to one line item “A”.
If product ‘A’ is added to cart, we should prevent the user from applying discount codes on Checkout by entering them in the discount field displaying an error message. We should also disallow the application of automatic discounts.
Is it possible to implement something similar with the current Shopify Functions functionality ?

Shopify Scripts has methods that allow you to apply a discount to a specific line item, as well as cancel a discount code entered by the user. We have referred to the Product Discount API documentation.
In the section “Migrating from scripts to functions” ( https://shopify.dev/docs/api/functions/reference/product-discounts#migrating-from-scripts-to-functions ) there is the following table ( https://take.ms/sL38Cl ).
But it seems to us that there are inaccuracies in the documentation regarding migration (described below), we contacted Shopify support but they said they don’t provide support in this area.

№1

The table has the following description, “Use a product variant to limit the number of times that you want to apply the discount” ( https://take.ms/B75p6 ). In "Product Discount API " in “FunctionResult” we can specify only “Product Variant Id” and “Quantity” ( https://take.ms/U9XBWG ) in the “FunctionResult”, based on the documentation.

This means that it is possible to limit the discount application by “Quantity” and “Variant Id”, but not by a specific line item.

For example, we have several line items in the cart with the same variant and one of them has property “discount_target”. For the line item with property “discount_target” we need to apply a discount.

Based on the documentation that is described in “FunctionResult”, there is no method to apply a discount to a particular line item. Does this mean that currently applying a discount to a specific line item is not possible using Shopify Functions ?

№2

The table has the following description: “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.” ( https://take.ms/y9E2u )

The description mentions “use conditions to exclude a discount” ( https://take.ms/DMHvC ).

In “Product Discount API” in “FunctionResult” or in “Order Discount API” in “FunctionResult” there is a method that applies a discount ( https://take.ms/wruZZ ), but there is no method to exclude a discount, based on the documentation.

How should the discount exclusion work using Shopify Functions then, based on the description in the documentation “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.” ?
Also, there is no mention in the documentation about excluding automatic discounts.

1 Like

Hi Aleksandr,

You have some very specific requirements and while Shopify Functions is designed to be flexible and powerful, it may not cover all your specific needs as of yet, especially since the Product Discount API is relatively new.

1:

Shopify Functions can indeed apply discounts to a specific product variant, but currently, there’s no way to apply a discount to a specific line item based on a property, like “discount_target”. The line item discounts are applied based on the product variant, not on individual line items.

2:

The Product Discount API and Order Discount API allow you to create specific discount rules, but they don’t currently have a direct method to cancel or exclude other discounts. To exclude a discount from applying, you could potentially create a condition in your function that checks if a certain condition is met (like if product ‘A’ is in the cart), and if so, doesn’t create a discount.

However, this wouldn’t cancel out any other discounts that are already applied.

Currently, Shopify Functions does not have the ability to exclude automatic discounts. This might be a feature that Shopify adds in the future, but it is not available at this time.

A potential workaround might be to use the storefront API to add a validation rule to the checkout process that prevents a user from proceeding to payment if a discount code is applied when product ‘A’ is in the cart. However, this would require a custom storefront and would not be possible with the default Shopify checkout process.

Hope this helps,

Hello.

Thank you for your reply.

№2

A potential workaround might be to use the storefront API to add a validation rule to the checkout process that prevents a user from proceeding to payment if a discount code is applied when product ‘A’ is in the cart. However, this would require a custom storefront and would not be possible with the default Shopify checkout process.

  1. You mean with Checkout UI extensions using useBuyerJourneyIntercept to disallow going to the next checkout page ?

  2. As far as we understand, this approach can work for discount codes entered by the user in the checkout field. Is there any way to prevent the automatic discount from being applied or to cancel the automatic discount on checkout using Shopify Functions/ Shopify Scripts ?