How to prevent discount stacking in tiered discount code script?

Topic summary

A Shopify store owner seeks to prevent discount code stacking with their tiered discount script. The original script automatically applies percentage discounts based on cart spend thresholds ($30/10% off, $50/15% off, $100/20% off) but allows customers to enter additional discount codes at checkout.

Solution Provided:
A contributor shares an updated script requiring a specific discount code to activate tiered pricing. The modified version supports multiple discount tiers (e.g., STANDARD_CODE and AMBASSADOR_CODE with different discount percentages). Key implementation details:

  • Create 0% discount codes in Shopify Admin
  • The script handles actual discount calculations
  • Each code can trigger different tier structures

Common Issues Raised:

  • Script applies discounts per line item rather than cart subtotal (causing $10 off each item instead of $10 off total order)
  • Questions about restricting eligible items/collections
  • Script not functioning or showing $0 discount in cart
  • Installation confusion (requires Shopify Plus and Script Editor app)

Current Status:
The thread remains open with unresolved questions about cart-level discounting and item restrictions. Note: Shopify Scripts are being deprecated in 2025 in favor of Checkout Functions and authorized apps.

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

Im not sure why this is happening? Did I miss something?

# ================================ Customizable Settings ================================
# ================================================================
# Tiered Discounts by Spend Threshold
#
# If the cart total is greater than (or equal to) an entered
# threshold, the associated discount is applied to each item.
#
#   - 'threshold' is the spend amount needed to qualify
#   - 'discount_type' is the type of discount to provide. Can be
#     either:
#       - ':percent'
#       - ':dollar'
#   - 'discount_amount' is the percentage/dollar discount to
#     apply (per item)
#   - 'discount_message' is the message to show when a discount
#     is applied
# ================================================================
SPENDING_THRESHOLDS_STANDARD = [
  {
    threshold: 100,
    discount_type: :dollar,
    discount_amount: 15,
    discount_message: 'Spend $100 and get $15 off!',
  },
  {
    threshold: 200,
    discount_type: :dollar,
    discount_amount: 40,
    discount_message: 'Spend $200 and get $40 off!',
  },
  {
    threshold: 400,
    discount_type: :dollar,
    discount_amount: 100,
    discount_message: 'Spend $400 and get $100 off!',
  },
]

SPENDING_THRESHOLDS_AMBASSADOR = [
  {
    threshold: 30,
    discount_type: :percent,
    discount_amount: 12.5,
    discount_message: 'Spend $30 and get 12.5% off!',
  },
  {
    threshold: 50,
    discount_type: :percent,
    discount_amount: 17.5,
    discount_message: 'Spend $50 and get 17.5% off!',
  },
  {
    threshold: 100,
    discount_type: :percent,
    discount_amount: 22.5,
    discount_message: 'Spend $100 and get 22.5% off!',
  },
]

DISCOUNT_CODES = {
  'CYBER' => SPENDING_THRESHOLDS_STANDARD,
  'AMBASSADOR_CODE' => SPENDING_THRESHOLDS_AMBASSADOR
}

# ================================ Script Code (do not edit) ================================
# ================================================================
# DiscountApplicator
#
# Applies the entered discount to the supplied line item.
# ================================================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      1 - (discount_amount * 0.01)
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end

    line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

# ================================================================
# TieredDiscountBySpendCampaign
#
# If the cart total is greater than (or equal to) an entered
# threshold, the associated discount is applied to each item.
# ================================================================
class TieredDiscountBySpendCampaign
  def initialize(tiers)
    @tiers = tiers.sort_by { |tier| tier[:threshold] }.reverse
  end

  def run(cart)
    applicable_tier = @tiers.find { |tier| cart.subtotal_price >= (Money.new(cents: 100) * tier[:threshold]) }
    return if applicable_tier.nil?

    discount_applicator = DiscountApplicator.new(
      applicable_tier[:discount_type],
      applicable_tier[:discount_amount],
      applicable_tier[:discount_message]
    )

    cart.line_items.each do |line_item|
      next if line_item.variant.product.gift_card?
      discount_applicator.apply(line_item)
    end
  end
end

def discount_code_present(cart, code)
  !cart.discount_code.nil? and (cart.discount_code.code.upcase == code.upcase)
end

CAMPAIGNS = []

DISCOUNT_CODES.each do |discount_code|
  if discount_code_present(Input.cart, discount_code[0])
    CAMPAIGNS = [
      TieredDiscountBySpendCampaign.new(discount_code[1]),
    ]
  end
end

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart