All things Shopify and commerce
Hi everyone,
I'm building a Shopify Remix app for a volume discount feature and using GraphQL automatic discounts along with the Shopify Product Discount Function. I'm facing an issue where:
It seems like the discount function is not applying multiple discounts properly when different products with their own discounts are in the cart. I have set discountApplicationStrategy: DiscountApplicationStrategy.All, but the issue persists.
Has anyone faced a similar issue? How can I ensure that each eligible product gets its respective discount applied correctly?
Here’s my run.js file for reference:
Hi,
Hope this will help
Seems like your function is only applying a discount to one product at a time instead of all eligible products.
Try to fix using steps
- Understand the Loop Problem
- Ensure Every Product Gets Its Own Discount
- Modify the code
Example of code
for (const target of targets) {
const line = input.cart.lines.find(line => line.merchandise?.id === target?.productVariant?.id);
if (!line) continue;
const eligibleDiscount = sortedDiscountValues.find(discountValue => line.quantity >= discountValue.quantity);
if (eligibleDiscount) {
// If line quantity is enough for this discount, create a discount for this line item
const discount = {
targets: [target],
message: eligibleDiscount.discount_message,
value: eligibleDiscount.discount_type === "Percentage"
? { percentage: { value: parseFloat(eligibleDiscount.discount_percent) } }
: { fixedAmount: { amount: parseFloat(eligibleDiscount.discount_fixed) } }
};
discounts.push(discount);
}
}
Hi ,
First of all, thanks a lot for your help and for sharing the example code! I really appreciate it.
I tried implementing your example code in my Shopify discount function, but I’m still facing the same issue where the discount is only applied to one product in the cart instead of both eligible products.
I checked my function logs, and here’s what I found:
Here’s my function log output for reference:
✔ 2025-03-05T07:34:31.308603Z (success) - d70e8d
Input
{
"cart": {
"lines": [
{
"merchandise": {
"id": "gid://shopify/ProductVariant/41778763169843",
"typename": "ProductVariant"
},
"quantity": 2
},
{
"merchandise": {
"id": "gid://shopify/ProductVariant/41778762842163",
"typename": "ProductVariant"
},
"quantity": 2
}
]
},
"discountNode": {
"metafield": {
"value": "{\"title\":\"Volume Discount product\",\"discountValues\":[{\"discount_type\":\"Percentage\",\"quantity\":\"1\",\"discount_percent\":\"\",\"discount_fixed\":\"\"},{\"discount_type\":\"Percentage\",\"quantity\":\"2\",\"discount_percent\":\"10\",\"discount_fixed\":\"\"}],\"variants\":[\"gid://shopify/ProductVariant/41778763169843\"]}"
}
}
}
Logs
Parsed Configuration: [object Object]
Discounts from function: [
{
"targets": [
{
"productVariant": {
"id": "gid://shopify/ProductVariant/41778763169843"
}
}
],
"message": "XP-VD: Buy 2+, Save 10%\"",
"value": {
"percentage": {
"value": 10
}
}
},
{
"targets": [
{
"productVariant": {
"id": "gid://shopify/ProductVariant/41778763169843"
}
}
],
"message": "XP-VD: Buy 2+, Save 10%\"",
"value": {
"percentage": {
"value": 10
}
}
}
]
Output
{
"discountApplicationStrategy": "ALL",
"discounts": [
{
"message": "XP-VD: Buy 2+, Save 10%\"",
"targets": [
{
"productVariant": {
"id": "gid://shopify/ProductVariant/41778763169843"
}
}
],
"value": {
"percentage": {
"value": 10
}
}
},
{
"message": "XP-VD: Buy 2+, Save 10%\"",
"targets": [
{
"productVariant": {
"id": "gid://shopify/ProductVariant/41778763169843"
}
}
],
"value": {
"percentage": {
"value": 10
}
}
It looks like the issue might be related to how the discountNode metafield is handling multiple variants. Do you think I should be structuring the metafield differently, or is there something missing in how I’m applying discounts to multiple products?
Would love to hear your thoughts on this! Thanks again for your help. 😊
Hi ,
First of all, thanks a lot for your help and for sharing the example code! I really appreciate it.
I tried implementing your example code in my Shopify discount function, but I’m still facing the same issue where the discount is only applied to one product in the cart instead of both eligible products.
I checked my function logs, and here’s what I found:
Here’s my function log output for reference:
It looks like the issue might be related to how the discountNode metafield is handling multiple variants. Do you think I should be structuring the metafield differently, or is there something missing in how I’m applying discounts to multiple products?
Would love to hear your thoughts on this! Thanks again for your help. 😊
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025