Product discount function's cost.totalAmount input doesn't account for discounts?

Just wanted to confirm if it’s intended and if anyone else is seeing this.

My product discount function requests the total amount of the cart which returns as expected, but when I apply an order discount code, or even apply another automatic product discount function to a line item the resulting function returns the totalAmount of the cart and of each line unaffected by discounts.

And if so, are there any workarounds because any calculation based on the total amount is now incorrect if a discount is applied.

Are you referring to this when you say “totalAmount”?

That suggests that the subtotal amount comes before discounts but that the actual total should be with everything necessary removed from the subtotal amount.

Yes that’s the one, it’s strange because it’s not reflecting the discounts at all for me. Here’s my cart:

And then the resulting function logs:

{
  "cart": {
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "quantity": 1,
        "attribute": null,
        "userAttribute": null,
        "merchandise": {
          "id": "gid://shopify/ProductVariant/47328231325999",
          "weight": 10,
          "weightUnit": "POUNDS",
          "metafield": {
            "value": "{\"applyProductDiscount\":false}"
          }
        },
        "cost": {
          "totalAmount": {
            "amount": "699.95"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/1",
        "quantity": 1,
        "attribute": null,
        "userAttribute": null,
        "merchandise": {
          "id": "gid://shopify/ProductVariant/47328231751983",
          "weight": 0,
          "weightUnit": "KILOGRAMS",
          "metafield": {
            "value": ...
          }
        },
        "cost": {
          "totalAmount": {
            "amount": "10.0"
          }
        }
      }
    ],
    "cost": {
      "subtotalAmount": {
        "amount": "709.95",
        "currencyCode": "CAD"
      },
      "totalAmount": {
        "amount": "709.95",
        "currencyCode": "CAD"
      }
    },
    "buyerIdentity": {
      "isAuthenticated": false,
      "customer": {
        "id": "gid://shopify/Customer/7569428087087",
        "numberOfOrders": 0,
        "hasTags": []
      }
    }
  },
  "discountNode": {
    "metafield": {
      "value": "..."
    }
  },
  ...
}

The logs indicate the totalAmount returned is $709.95, which is the total only when the discount code is not taken into account. Even the line item for the gift card does not take into account the line item discount.

Created Github Issue:

This is intended, you get the totalAmount before any discounts would be applied as input to Discount Function APIs. You can think of your inputs as the state of the cart before we make any discounting decisions, and all discounts operate independently on the same input data.

The job of the phase after you generate discounts is to determine which discounts will apply from many different discount functions (or first party discounts.)

4 Likes