Discount code issues with new discount function API

I’m in the process of migrating my TS/JS product discount function over to use Rust and the new discount function API (version 2025-10).

In testing the migration by switching out the old function uid to the new rust function, the automatic product discounts work great but when I try to use a discount code it bugs out with InvalidOutputError errors and I’m not sure how to go about fixing it.

When running automatic product discounts it works great and the output looks like this:

output": {
      "operations": [
        {
          "productDiscountdddsAdd": {
            "candidates": [
              {
                "associatedDiscountCode": null,
                "message": "DISCOUNT",
                "targets": [
                  {
                    "cartLine": {
                      "id": "gid://shopify/CartLine/0",
                      "quantity": null
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "22.0"
                  }
                }
              }
            ],
            "selectionStrategy": "ALL"
          }
        }
      ]
    },
    "outputBytes": 199,
    "logs": [],
    "functionId": "8ab17e69-41c7-48e6-a5e5-a92c8eb482f7",
    "fuelConsumed": 99210,
    "target": "cart.lines.discounts.generate.run",

But then I try with a discount code and it errors:

"output": {
      "operations": [
        {
          "productDiscountsAdd": {
            "candidates": [
              {
                "associatedDiscountCode": {
                  "code": "HAY"
                },
                "message": "DISCOUNT",
                "targets": [
                  {
                    "cartLine": {
                      "id": "gid://shopify/CartLine/0",
                      "quantity": null
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "55.0"
                  }
                }
              }
            ],
            "selectionStrategy": "ALL"
          }
        }
      ]
    },
    "outputBytes": 208,
    "logs": [],
    "functionId": "8ab17e69-41c7-48e6-a5e5-a92c8eb482f7",
    "fuelConsumed": 87894,
    "target": "cart.lines.discounts.generate.run",
    "errorMessage": [
      {
        "path": [],
        "explanation": "Associated discount codes must be included in the enteredDiscountCodesAccept operation."
      }
    ],
    "errorType": "InvalidOutputError",

Ok so I just need to include enteredDiscountCodesAccept then:

output": {
      "operations": [
        {
          "productDiscountsAdd": {
            "candidates": [
              {
                "associatedDiscountCode": {
                  "code": "HAY"
                },
                "message": "DISCOUNT",
                "targets": [
                  {
                    "cartLine": {
                      "id": "gid://shopify/CartLine/0",
                      "quantity": null
                    }
                  }
                ],
                "value": {
                  "percentage": {
                    "value": "55.0"
                  }
                }
              }
            ],
            "selectionStrategy": "ALL"
          }
        },
        {
          "enteredDiscountCodesAccept": {
            "codes": [
              {
                "code": "HAY"
              }
            ]
          }
        }
      ]
    },
    "outputBytes": 254,
    "logs": [],
    "functionId": "8ab17e69-41c7-48e6-a5e5-a92c8eb482f7",
    "fuelConsumed": 91108,
    "target": "cart.lines.discounts.generate.run",
    "errorMessage": [
      {
        "path": [],
        "explanation": "The enteredDiscountCodesAccept operation requires the shop to have network access enabled"
      }
    ],

And now it says the shop needs to have network access enabled despite none of the features requiring network access. But I enable it anyway in my toml:

api_version = "2025-10"

[[extensions]]
name = "t:name"
handle = "discount-product-2"
type = "function"
uid = "3859b1e6-e487-5009-641c-f31a59a7f898067f7f11"
description = "t:description"

  [[extensions.targeting]]
  target = "cart.lines.discounts.generate.run"
  input_query = "src/cart_lines_discounts_generate_run.graphql"
  export = "cart_lines_discounts_generate_run"

  [extensions.build]
  command = "cargo build --target=wasm32-wasip1 --release"
  path = "target/wasm32-wasip1/release/discount-product-2.wasm"
  watch = [ "src/**/*.rs" ]

  [extensions.capabilities]
  network_access = true

  [extensions.input.variables]
  namespace = "..."
  key = "input_variables"

But it still gives the same error.

I checked the app settings and it had network access enabled too. I’m not sure what I’m doing wrong or what I’m supposed to do but does anyone know what I’m missing to get discount codes working, or if there’s a way to do this without network access because I’m not looking to making any external calls, or if there’s another way to do this without specifying associated_discount_code?

Network access for discount functions is only available for shops on Shopify for enterprises.

If you create a code app discount (e.g., though the discountCodeAppCreate mutation) that is associated with a code and your function ID, then that code can be used at checkout to trigger your function. Without network access, the code must be created on the app discount and will be implicitly associated with the discount candidates from your Function result. You should not return an “associatedDiscountCode” in your discount candidate, and also do not return a “enteredDiscountCodesAccept” operation. These are reserved for external discount codes through network access.

Thanks for clarifying @Linda-Shopify, that makes more sense but when I try that it errors out for me saying it needs that field:

error[E0063]: missing field `associated_discount_code` in initializer of `ProductDiscountCandidate`
16:33:43 │        discount-product-2 │    --> src/cart_lines_discounts_generate_run.rs:223:30
16:33:43 │        discount-product-2 │     |
16:33:43 │        discount-product-2 │ 223 |             candidates: vec![ProductDiscountCandidate {
16:33:43 │        discount-product-2 │     |                              ^^^^^^^^^^^^^^^^^^^^^^^^ missing `associated_discount_code`
Some(CartOperation::ProductDiscountsAdd(
        ProductDiscountsAddOperation {
            selection_strategy: ProductDiscountSelectionStrategy::All,
            candidates: vec![ProductDiscountCandidate {
                targets,
                message: Some(setting.get_discount_message()),
                value, // associated_discount_code: setting
                       //     .discount_code
                       //     .as_ref()
                       //     .map(|code| AssociatedDiscountCode { code: code.clone() }),
            }],
        },
    ))

I’m able to reproduce the same problem. According to our API, associated_discount_code should be optional, but for some reason our generated type for ProductDiscountCandidate is treating it as a required field. I will report this back to the team, but in the mean time you should be able to add associated_discount_code: None, to get the behaviour you need.

2 Likes