For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
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-functio... ) 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.
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,
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
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 ?