How to Dynamically Fetch Line Item Properties in Shopify Functions?

Topic summary

A developer is building a Shopify Function to apply discounts based on custom line item properties, but faces a technical limitation with dynamic attribute retrieval.

The Core Problem:

  • The GraphQL RunInput query requires a hardcoded key when fetching cart line attributes using attribute(key: "some_key")
  • Users need to define custom property names when configuring discounts, making the key unknown at development time
  • There’s no apparent way to fetch all attributes from a cart line or dynamically specify which attribute to retrieve at runtime

Specific Requirements:

  • App users should enter any line item property name during discount configuration
  • Customers provide the property value when adding products to cart
  • The function must retrieve the user-defined attribute dynamically when executing

Current Status:
The developer is seeking either a method to fetch all cart line attributes in Shopify Functions or a workaround to dynamically retrieve user-defined attributes without hardcoding keys. The discussion remains open with no solution provided yet.

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

I’m building a Shopify Function that applies discounts based on a custom line item property entered by the user. The issue is that in the function’s input (RunInput query), we must provide a specific key to fetch an attribute. However, I don’t know the key in advance because users define it when creating the discount.

What I Need:

  1. Users should be able to enter any line item property name when configuring the discount.

  2. When the function runs, it should dynamically fetch that specific attribute from cart lines.

Right now, cart.line.attribute(key: “some_key”) requires a hardcoded key, which makes it impossible to dynamically retrieve all attributes.

GraphQL Input Query (Current Approach)

query RunInput {
  cart {
    lines {
      id
      quantity
      attribute(key: "user-defined-key") { # The issue: Key must be hardcoded
        key
        value
      }
    }
  }
}

Is there a way to fetch all attributes from a cart line in Shopify Functions? Or any workaround to dynamically retrieve the user-defined attribute at runtime?

Any help would be greatly appreciated!

Some points to consider.

  1. the line item property is entered by the app user and the value is entered by the customer when they add the product to the cart, so we cant store the user defined property and value as value to the hard coded key.