Discussing Shopify Functions development, deployment, and usage in Shopify apps.
I am currently building an order discount function that should make specific line items free when a certain cart value is reached. Since I want to include line level discounts applied by some product discount functions in the cart value, I am using the order discount instead of a product discount function. In product discount functions, those line level discounts seem to be not reflected in the cart input.
The order discount function run result allows targeting product variants and also specifying a quantity. My function run result includes a product variant target with a quantity of 1, as you can see below:
{ "discountApplicationStrategy": "FIRST", "discounts": [ { "conditions": [ { "orderMinimumSubtotal": { "excludedVariantIds": [ "gid://shopify/ProductVariant/37732318707871" ], "minimumAmount": 100, "targetType": "ORDER_SUBTOTAL" } } ], "message": "Aktion", "targets": [ { "productVariant": { "id": "gid://shopify/ProductVariant/37732318707871", "quantity": 1 } } ], "value": { "percentage": { "value": 100 } } } ] }
But when I add more quantity of this product to my cart, they are discounted too. I only want a quantity of 1 to be discounted. This seems like a bug to me, or what does the quantity in the function run result do?
I am happy for any help on this, thanks in advance!
Hi VitaDev,
Could there be another discount function or rule in your cart that is also applying a discount to the same product variant? You'll want to make sure to check all your discount functions and rules to ensure there are no conflicts.
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
Hey Liam,
Thanks for you quick reply. I had other discount functions active, but none of them applied discounts to the same line item / product variant. Deactivating them changes nothing, the problem remains the same.
Hi VitaDev,
I've reached out to our product team on this to confirm that the use case you're trying to achieve is supported with order discount functions. Will update here when I learn more.
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
I've been having problems with excludedVariantIds too. Actually, it seems like a lot of the functionalities don't work, are poorly exemplified, or I'm just getting them wrong.
conditions:[{
orderMinimumSubtotal: {
excludedVariantIds,
minimumAmount,
targetType: TargetType.OrderSubtotal
}
},
{
productMinimumQuantity: {
ids,
minimumQuantity,
targetType: TargetType.ProductVariant
}
}]
There, for example. I have exlucedVariantIds both in targets and conditions, but it's still counting the excluded variants when comparing to the subtotal. So if the subtotal is over the minimum because of the excluded variants it applies the discount even though it shouldn't. Maybe I'm doing something wrong?
I can artificially raise the minimum value by adding the cost of the unwanted variants to it, but then what's the point of excludedVariantIds? The problem is usually me, but since I can't find more complete examples I don't know what I'm doing wrong.
Hi Ryoshin & VitaDev,
Our devs have identified that there is a bug on our side which is causing this. We're working on a fix which will likely be shipped after BFCM. Thanks for flagging this!
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
Ok, thanks. Another question, how do product discounts show the feedback that "discount code isn’t valid for the items in your cart" if there's no condition field, the target can't be empty, and EMPTY_DISCOUNT shows no feedback?
I agree, I always liked developer documentation of Shopify but regarding functions in many cases more examples and explanation would be beneficial. I also get the impression that there are quite many bugs with discount functions at the moment which makes it hard to migrate complex discount logic from scripts to functions.
Regarding the feedback if a discount is not valid for items in cart, I think there is no feedback wanted for those automatic app discounts, it can just apply discounts or not. Makes sense to me, since automatic discounts are not activated by a customer like discount codes and this kind of feedback would be more confusing than helpful.
I'm creating discount codes. I think it's less confusing if it says "discount code isn’t valid for the items in your cart" instead of simply not applying anything. They might think the discount code isn't working instead of knowing the products don't fit the criteria. And I'm also talking specifically about product discounts since I've got it sort of figured out for order discounts with conditions. I agree they're not as necessary for automatic discounts though.
Also, what is message supposed to do?
The discount message.
I can't see the result of it anywhere
Just ran into another error. I created a product discount, it applies correctly. I had 4 products in my cart, 2 fit the criteria and 2 didn't. It applied correctly. When I removed the 2 products that fit the criteria 1 of the products that didn't fit took the discount for itself. So a product that shouldn't be discounted was!
You should probably create new threads for those kinds of problems you encounter so that they eventually are seen be the right people and can get fixed.
We encounter the similar issue with more complex condition, it looks like the bug was half fixed, but not entirely fixed . The "excludedVariantIds" was successfully applied for "orderMinimumSubtotal" in the condition logic in my case, but only for store default currency (which is USD in our case). In our case, we applied 4 currencies: USD, CAD, AUD, and NZD. The USD works well, but other currencies causing some problems.
Our discount logic:
const thresholds = {
"USD": [ 89, 149, 249 ],
"CAD": [ 119, 199, 339 ],
"AUD": [ 129, 219, 369 ],
"NZD": [ 139, 239, 399 ]
}
const discounts = [
// first tier discount
{
value: {
percentage: {
value: 15,
},
},
conditions: [
{
orderMinimumSubtotal: {
targetType: TargetType.OrderSubtotal,
minimumAmount: thresholds[currencyCode][0],
excludedVariantIds: excludedVariantIds,
},
},
],
targets: [
{
orderSubtotal: {
excludedVariantIds: excludedVariantIds,
},
},
],
},
// seconnd tier discount
{
value: {
percentage: {
value: 25,
},
},
conditions: [
{
orderMinimumSubtotal: {
targetType: TargetType.OrderSubtotal,
minimumAmount: thresholds[currencyCode][1],
excludedVariantIds: excludedVariantIds,
},
},
],
targets: [
{
orderSubtotal: {
excludedVariantIds: excludedVariantIds,
},
},
],
},
// third tier discount
{
value: {
percentage: {
value: 40,
},
},
conditions: [
{
orderMinimumSubtotal: {
targetType: TargetType.OrderSubtotal,
minimumAmount: thresholds[currencyCode][2],
excludedVariantIds: excludedVariantIds,
},
},
],
targets: [
{
orderSubtotal: {
excludedVariantIds: excludedVariantIds,
},
},
],
}
]
// return tier discount;
return {
discounts: discounts,
discountApplicationStrategy: DiscountApplicationStrategy.Maximum
}
We did a huge amount of test, and found that there might be two conditions taking effect at the same time.
First condition (which is exactly what the code above shows) :
This still doesn't work properly....