Create discount applied to compare_at_price

Topic summary

A developer is migrating Shopify Scripts to Shopify Functions and needs to apply an automatic discount to compare_at_price instead of the final price for users with a specific tag.

Current approach:

  • Filtering cart lines for tagged customers (costumerIsPro) and product variants
  • Mapping filtered lines to discount targets using variant IDs

Migration challenge:
The original script logic checks if compare_at_price exists on the variant and calculates a new line price based on it. The developer is attempting to replicate this behavior in Shopify Functions with a 50% discount for pro users.

Key question:
Can Shopify Functions apply discounts to compare_at_price rather than the standard price? The post includes code snippets showing the target selection logic and discount properties structure, but the migration implementation details appear incomplete or corrupted in the provided text.

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

We are working to migrate our Shopify Scripts to Shopify Functions.

I am following the examples to create an automatic discount for specific user tag . I would like to know if I can apply the discount to the compare_at_price instead of the “final” price.

This is how we select the target to apply the discount:

const targets = input.cart.lines
      .filter(line =>  costumerIsPro &&
        line.merchandise.__typename == "ProductVariant")
      .map(line => {

        const variant = /** @type {ProductVariant} */ (line.merchandise);
        return /** @type {Target} */ ({
          productVariant: {
            id: variant.id
          }
        });
      });

And this the discount properties :

return {
      discounts: [
        
        {        
          "message": "50% pro users",
          targets,
          value: {
            percentage: {
              value: "50.0"
            }
          }
        }
      ],
      discountApplicationStrategy: DiscountApplicationStrategy.First
    };

The script that I am trying to migrate do the following:

 if item.variant.compare_at_price
            compare_price = item.variant.compare_at_price
          else
            compare_price = item.variant.price
          end
          new_line_price = compare_price * (100 - proDiscountNumber) * 0.01 *  item.quantity