Product Discount Function Bug - Line Item Quantities

Topic summary

A product discount function is causing inconsistent cart subtotal calculations when line item quantities are updated.

Core Issue:

  • Updating item quantities in the cart produces unpredictable subtotals—sometimes correct, sometimes incorrect
  • The problem appears to stem from how line items are grouped in the function input

Specific Behavior Observed:

  • When quantities were updated (second item to 3, first item to 6), the cart displayed incorrect pricing
  • The cart input showed the second line item incorrectly split into two separate CartLine entries (quantities of 1 and 2) instead of a single line with quantity 3
  • This splitting caused the subtotal calculation to fail

Workaround Found:

  • Adjusting the second item’s quantity to 4, then back to 3, resolved the issue temporarily
  • The cart input then properly grouped the line items, and the function output calculated discounts correctly

Current Status:
The issue remains unresolved and appears intermittent. The root cause seems related to how the cart groups line items before passing them to the discount function, but the exact trigger for the splitting behavior is unclear.

Summarized with AI on November 13. AI used: claude-sonnet-4-5-20250929.

When using a product discount function we are seeing unpredictable behaviour in relation to subtotal pricing of cart items when updating quantities within the cart.

Steps to reproduce:

  1. Add relevant items to cart.
  2. Update quantities of each item.
  3. Check the subtotal of each item - sometimes it is correct and other times not.

Here is an example:

This screenshot shows an incorrect subtotal for the second line. This happened after I had both items in cart with just 1 quantity, then I updated the second item quantity to 3, then the first item to 6.

Here is the cart input to the product discount function when this occurred. Interestingly the second line item (the problematic one) seems to be split into 2 separate line items (CartLine 1 and CartLine 2), even though they appear as one line in the cart, so the quantities show as 1 and 2, when they should be represented as a single line with a quantity of 3.

{
    "buyerIdentity": null,
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "cost": {
          "amountPerQuantity": {
            "amount": "39.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "234.0"
          },
          "totalAmount": {
            "amount": "234.0"
          }
        },
        "quantity": 6,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/32821778645124",
          "product": {
            "id": "gid://shopify/Product/4699130232964",
            "vendor": "Some vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/1",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "65.0"
          },
          "totalAmount": {
            "amount": "65.0"
          }
        },
        "quantity": 1,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/2",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "130.0"
          },
          "totalAmount": {
            "amount": "130.0"
          }
        },
        "quantity": 2,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      }
    ]
  }

The output is fine and works as it should:

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/32821778645124",
            "quantity": 6
          }
        },
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/31929170034820",
            "quantity": 3
          }
        }
      ],
      "value": {
        "percentage": {
          "value": 15.000000000000005
        }
      },
      "message": "2kg Stack!"
    }
  ],
  "discountApplicationStrategy": "ALL"
}

I then updated the quantity of the second line item to 4, then back to 3 and the problem disappeared:

And sure enough the cart input to the function shows the line items grouped more predictably:

{
    "buyerIdentity": null,
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "cost": {
          "amountPerQuantity": {
            "amount": "39.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "234.0"
          },
          "totalAmount": {
            "amount": "234.0"
          }
        },
        "quantity": 6,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/32821778645124",
          "product": {
            "id": "gid://shopify/Product/4699130232964",
            "vendor": "Some vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/1",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "195.0"
          },
          "totalAmount": {
            "amount": "195.0"
          }
        },
        "quantity": 3,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      }
    ]
  }

The function output is identical both times.

As far as I can see this bug only happens when the cart line items are being split for some reason on the Shopify server side.