Code discount not applying while automatic discount with the same function works fine

I have a Shopify Function (target: cart.lines.discounts.generate.run) that powers both an automatic discount and a code discount. Both discounts use the exact same function and the same metafield configuration.

When I add an item to the cart and go to checkout:

  • The automatic discount applies correctly (25% off).

  • When I enter the discount code, the code discount does NOT apply — even though the function runs successfully and returns the exact same output.


    (Error message: “Discount code CODE1 is not valid for the items in your cart”)

Both function runs complete with “status”: “success” and produce identical orderDiscountsAdd operations with the same candidates, same values, and same selection strategy. The only difference in the input is:

  • Automatic: “triggeringDiscountCode”: null

  • Code: “triggeringDiscountCode”: “code1”

I’ve attached both function run logs below. They fire within 2ms of each other for the same cart. Both combinations are enabled. The discount code is active and valid.

What I’ve tried:

  • Adding associatedDiscountCode to the candidates for the code-triggered run — no change.

  • Verifying combination settings — both discounts allow combinations with order discounts.

  • Confirming the code discount is active in Shopify Admin.

My question: Why would Shopify silently ignore a code discount’s output when the automatic discount with the exact same function and output works? Is there a known issue with code discounts on the cart.lines.discounts.generate.run target, or is there an additional output field required for code discounts to be applied?

Environment:

  • Function target: cart.lines.discounts.generate.run

  • Selection strategy: FIRST

  • Two candidates: one with fixedAmount + orderMinimumSubtotal condition, one with percentage (no condition)

  • Cart subtotal: 1600, limit amount: 2000

    First one ise the code version, as you can see it has triggeredDiscountCode: “code1”

{
  "shopId": x,
  "apiClientId": 305876008961,
  "payload": {
    "export": "cart-lines-discounts-generate-run",
    "input": {
      "triggeringDiscountCode": "code1",
      "cart": {
        "attribute": null,
        "buyerIdentity": {
          "isAuthenticated": true
        },
        "lines": [
          {
            "id": "gid://shopify/CartLine/0",
            "quantity": 1,
            "cost": {
              "subtotalAmount": {
                "amount": "1600.0"
              }
            },
            "merchandise": {
              "__typename": "ProductVariant",
              "id": "gid://shopify/ProductVariant/48091615363389",
              "product": {
                "inAnyCollection": true
              }
            }
          }
        ]
      },
      "discount": {
        "discountClasses": [
          "PRODUCT",
          "ORDER"
        ],
        "metafield": {
          "value": "{\"discountPercentage\":25,\"limitAmount\":2000,\"collectionIds\":[\"gid://shopify/Collection/510446305597\"],\"requiresLogin\":true,\"platform\":\"all\"}"
        }
      }
    },
    "inputBytes": 474,
    "output": {
      "operations": [
        {
          "orderDiscountsAdd": {
            "candidates": [
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "conditions": [
                  {
                    "orderMinimumSubtotal": {
                      "minimumAmount": "2000",
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "fixedAmount": {
                    "amount": "500.00"
                  }
                }
              },
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "25"
                  }
                }
              }
            ],
            "selectionStrategy": "FIRST"
          }
        }
      ]
    },
    "outputBytes": 300,
    "logs": [],
    "functionId": "019c57ab-0413-761c-87a8-11999cf33085",
    "fuelConsumed": 510386,
    "target": "cart.lines.discounts.generate.run",
    "inputQueryVariablesMetafieldValue": {
      "discountPercentage": 25,
      "limitAmount": 2000,
      "collectionIds": [
        "gid://shopify/Collection/510446305597"
      ],
      "requiresLogin": true,
      "platform": "all"
    },
    "inputQueryVariablesMetafieldNamespace": "upper-limit-discount",
    "inputQueryVariablesMetafieldKey": "function-configuration"
  },
  "logType": "function_run",
  "status": "success",
  "source": "upper-limit-discount",
  "sourceNamespace": "extensions",
  "logTimestamp": "2026-02-19T14:52:28.706869Z",
  "localTime": "2026-02-19 17:52:28",
  "storeName": "x.myshopify.com"
}

This one is the automatic one:

{
  "shopId": xxx,
  "apiClientId": 305876008961,
  "payload": {
    "export": "cart-lines-discounts-generate-run",
    "input": {
      "triggeringDiscountCode": null,
      "cart": {
        "attribute": null,
        "buyerIdentity": {
          "isAuthenticated": true
        },
        "lines": [
          {
            "id": "gid://shopify/CartLine/0",
            "quantity": 1,
            "cost": {
              "subtotalAmount": {
                "amount": "1600.0"
              }
            },
            "merchandise": {
              "__typename": "ProductVariant",
              "id": "gid://shopify/ProductVariant/48091615363389",
              "product": {
                "inAnyCollection": true
              }
            }
          }
        ]
      },
      "discount": {
        "discountClasses": [
          "PRODUCT",
          "ORDER"
        ],
        "metafield": {
          "value": "{\"discountPercentage\":25,\"limitAmount\":2000,\"collectionIds\":[\"gid://shopify/Collection/510446305597\"],\"requiresLogin\":true,\"platform\":\"all\"}"
        }
      }
    },
    "inputBytes": 469,
    "output": {
      "operations": [
        {
          "orderDiscountsAdd": {
            "candidates": [
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "conditions": [
                  {
                    "orderMinimumSubtotal": {
                      "minimumAmount": "2000",
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "fixedAmount": {
                    "amount": "500.00"
                  }
                }
              },
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "25"
                  }
                }
              }
            ],
            "selectionStrategy": "FIRST"
          }
        }
      ]
    },
    "outputBytes": 300,
    "logs": [],
    "functionId": "019c57ab-0413-761c-87a8-11999cf33085",
    "fuelConsumed": 509722,
    "target": "cart.lines.discounts.generate.run",
    "inputQueryVariablesMetafieldValue": {
      "discountPercentage": 25,
      "limitAmount": 2000,
      "collectionIds": [
        "gid://shopify/Collection/510446305597"
      ],
      "requiresLogin": true,
      "platform": "all"
    },
    "inputQueryVariablesMetafieldNamespace": "upper-limit-discount",
    "inputQueryVariablesMetafieldKey": "function-configuration"
  },
  "logType": "function_run",
  "status": "success",
  "source": "upper-limit-discount",
  "sourceNamespace": "extensions",
  "logTimestamp": "2026-02-19T14:52:28.708198Z",
  "localTime": "2026-02-19 17:52:28",
  "storeName": "x.myshopify.com"
}

Hi, can you share the x-request-id header for the checkout request, so I can look into the logs?

Cheers,

Jonathan

7364b8fd-a393-44af-8e47-a05d3ec01c18-1771517052

Hi Jonathan,

I just wanted to follow up regarding the x-request-id I shared earlier:
7364b8fd-a393-44af-8e47-a05d3ec01c18-1771517052

This is quite urgent on our side, so we would really appreciate any update you can provide.

Please let us know as soon as possible.

Thanks,
Emre

Looking at the logs for that specific request, only code1 ran because at that specific time auto1 had been disabled. It was only enabled a few minutes after that request. When I just now tried using your shop to see what it would do, I see both discounts ran and I had both effects applied on my checkout.

I think that means you may have already solved your problem, right?

Cheers,

Jonathan

Actually, no — the problem isn’t solved yet. As you mentioned, auto1 was disabled during the request I shared, but you can see that code1 still wasn’t applied in that case.

Here’s what’s going on in more detail:

The bug: The code discount only works when the applied product quantity is 2 (or more than 2). When quantity is 1, it doesn’t apply, even though the discount logic has no quantity-related logic and both code and auto discounts produce the same output in all cases.

Here are the request IDs and what happens in each case:

  1. 843947a4-e129-44eb-ae42-2eb0c10fe917-1771541702

Both auto1 and code1 discounts are active. Auto1 is applied, but code1 fails with the same behavior. Line quantity is 1.

  1. 52924219-6be4-4977-b4af-98ea77ccd49f-1771541788

Both auto and code are active. Both discounts are applied. Line quantity is 2.

  1. c2fc3980-8c91-45b5-8a83-c5baf18e39dc-1771541857

Only code1 is active (auto1 disabled). Code1 does not apply when quantity is 1.

  1. 72167558-d643-4ff1-9672-2ced938602cc-1771542046

Same setup as above — only code1 active. Code1 does apply when quantity is 2 (or more).

By contrast, when I deactivate code1 and keep only auto1 active, it works correctly regardless of quantity (can’t add the image with 2 quantity, has 5 max image limit per comment).

So the behavior difference is clearly tied to code discounts vs automatic discounts, not to the discount logic itself. Both produce the same output JSON (as in the first message I shared in the topic,i shared it below as well.), but the code discount only applies when quantity is 2.

    "output": {
      "operations": [
        {
          "orderDiscountsAdd": {
            "candidates": [
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "conditions": [
                  {
                    "orderMinimumSubtotal": {
                      "minimumAmount": "2000",
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "fixedAmount": {
                    "amount": "500.00"
                  }
                }
              },
              {
                "targets": [
                  {
                    "orderSubtotal": {
                      "excludedCartLineIds": []
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "25"
                  }
                }
              }
            ],
            "selectionStrategy": "FIRST"
          }
        }
      ]
    },

@Jonathan-Shopify Apologies for following up again, but I was wondering if there have been any updates. In my previous message, I provided a detailed explanation of the issue I’m facing.