What's your biggest current challenge? Have your say in Community Polls along the right column.
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: Order discount - product variant target quantity does nothing

Order discount - product variant target quantity does nothing

VitaDev
Shopify Partner
12 0 4

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!

Replies 13 (13)

Liam
Community Manager
3108 344 895

 

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

VitaDev
Shopify Partner
12 0 4

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.

Liam
Community Manager
3108 344 895

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

Ryoshin
Shopify Partner
40 3 7

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.

Liam
Community Manager
3108 344 895

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

Ryoshin
Shopify Partner
40 3 7

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?

VitaDev
Shopify Partner
12 0 4

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.

Ryoshin
Shopify Partner
40 3 7

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. 

Ryoshin
Shopify Partner
40 3 7

Also, what is message supposed to do? 

  • message (String)

    The discount message.

     

    I can't see the result of it anywhere

     
Ryoshin
Shopify Partner
40 3 7

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!

VitaDev
Shopify Partner
12 0 4

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.

Ang123
Shopify Partner
2 0 0

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)  : 

Order subtotal (excluding the variantIds we expected) meets the minimum amount we set, for whatever currency provided by the cart object. (Which is perfectly correct. This is what we want! )

Second condition ( not implemented by our code at all, but taking effect at the same time, we suspect it is the old logic in Shopify ) :
Order subtotal (doesn't excluding the variantIds we expected) meets the minimum amount we set, ignores the currency (it supposes the currency is "USD", not taking cart currency info into consideration), and only works for the first tier (looks like the "discountApplicationStrategy: DiscountApplicationStrategy.Maximum" is not taking effect for this logic as well).
 
The second condition causing issues when our store switched to other currency, since the condition was still calculated in "USD", and it doesn't exclude the variantIds we specified, and it somehow only apply first tier. (This is very likely matching the logic in the old code.)

So we highly suspected that the bug was fixed by adding new code, but didn't remove the old code. For the store with only one currency, the problem looks fixed. But for multiple currencies situation, the problem stay there.

Please check with the technical person to confirm whether this bug exists or not, as this affect our business strategies. (And we are in Shopify Plus plan)

sanjar
Shopify Partner
21 1 2

This still doesn't work properly....