Help structuring return to apply custom discount to each cart line - DiscountApplicationStrategy.All

Topic summary

A developer is struggling to apply individual discounts to each cart line item using Shopify’s product discount function with DiscountApplicationStrategy.All (API version 2024-01).

The Problem:

  • Input data contains different discount values per variant ID (e.g., 20% for one variant, 10% for another)
  • Current output only applies a single discount (10%) to all targeted variants instead of unique discounts per variant
  • The developer wants each variant to receive its associated discount value from the cart line attributes

Technical Details:

  • Using app/extension API version 2024-01, CLI version 3.53.0
  • Discount values are stored in line item attributes (key: “item-discount”)
  • Code attempts to map discounts to individual variants but the output structure groups all targets under one discount object

Additional Context:

  • Another user asks a related question about applying discounts to specific line items (with property “is_free”) rather than all items sharing the same variant ID
  • The original poster indicates this feature may not have been available at the time and suggests contacting Shopify support

Status: Unresolved; limited examples exist for this newer DiscountApplicationStrategy approach

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

I am having trouble structuring the standard output required to apply a different product discount to each cart item variant ID. I am using app and extension api-version 2024-01, app and cli versions 3.53.0. My product-discount function standard input looks like the below (and I would like the standard output to similarly reflect each variant ID’s separate discount value). STDIN:

{
  "cart": {
    "lines": [
      {
        "quantity": 1,
        "attribute": {
          "key": "item-discount",
          "value": "20.00"
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/45970165236027"
        }
      },
      {
        "quantity": 1,
        "attribute": {
          "key": "item-discount",
          "value": "10.00"
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/45970164515131"
        }
      }
    ]
  }
}

But my standard output only applies the first selected product discount (10%) and looks like this - STDOUT:

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970165236027"
          }
        },
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970164515131"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10.00"
        }
      }
    }
  ],
  "discountApplicationStrategy": "ALL"
}

and I would like the output to look something like this instead:

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970164515131"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10.00"
        }
      }
    }
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970165236027"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "20.00"
        }
      }
    }
  ],
  "discountApplicationStrategy": "ALL"
}

My return code in the js is as follows, can anyone help direct me on how to modify the code to output the associated discount for each variant id (as shown in the standard input at the top)? Since DiscountApplicationStrategy.All is fairly new, there’s not a lot of examples I have found on how I could accomplish associating a custom discount for each variant id.

return {
    discounts: [
      {
        targets, 	// Apply the discount to the collected targets
        value: {
          percentage: { 	// Define a percentage-based discount (default=0.00)
            value: discount	// Use variable value of discount per variant ID
          }
        }
      }
    ],
    discountApplicationStrategy: DiscountApplicationStrategy.All
  };

I should probably add the code showing the discount mapped to each line:

.filter(line => line.quantity >= configuration.quantity &&
        line.merchandise.__typename == "ProductVariant" &&
        line.attribute?.value != "0.00")
      .map(line => {
        discount = line.attribute.value.toString(); //override the default discount for each line.attribute.value
        const variant = /** @type {ProductVariant} */ (line.merchandise);
        console.error("DEBUG: Variant ID: "+variant.id+", Variant Discount: "+line.attribute?.value);
        return /** @type {Target} */ ({
          productVariant: { 	// Use the product variant ID to create a discount target
            id: variant.id
          }
        });
      });

hi @DeeDee-TDY ! do you know how to apply discounts only for target line_item, not for all line_items with the same id’s.
I have this issue, I need to apply discount for line_item with property “is_free”, but for ‘targets’ we use product variant ID to create a discount target..

query RunInput {
  cart {
    lines {
      quantity
      custom_data: attribute(key: "is_free") {
         value
      }
      merchandise {
        __typename
        ...on ProductVariant {
            id
        }
      }
    }
  }
}

const targets = input.cart.lines
    .filter(line => line.merchandise.__typename == "ProductVariant" && line.custom_data && line.custom_data.value == "true")
    .map(line => {
      const variant = /** @type {ProductVariant} */ (line.merchandise);
      return /** @type {Target} */ ({
        productVariant: {
          id: variant.id,
        }
      });
    });

Sorry I do not know how to apply to the line regardless of variantID. I believe at the time of the original post, it was not possible, but perhaps you can ask a Shopify team member if this feature is now available.