Detect discount code type in shipping scripts

Topic summary

Detecting the discount code type (shipping, percentage, fixed amount) in Shopify Scripts is the core issue. Accessing Input.cart.discount_code.percentage throws an error when the code is a shipping discount, indicating the object’s type varies.

Docs suggest Input.cart.discount_code can be CartDiscount::Shipping or CartDiscount::Percentage, but a direct comparison (e.g., Input.cart.discount_code == CartDiscount::Shipping) isn’t shown to work, and syntax is unclear.

Recent update: a workaround converts the discount_code object to a string and checks substrings, e.g., Input.cart.discount_code.to_s.include?(“FixedAmount”). This implies similar checks might work for “Shipping” or “Percentage,” though not verified.

Key terms: shipping discount reduces shipping cost; percentage discount reduces price by a percent; fixed amount applies a set currency reduction.

Outcome: no confirmed, idiomatic type-check solution. The discussion remains unresolved. Actionable suggestion is to use string-based detection, with caution about reliability. Code snippets are central to understanding.

Summarized with AI on February 11. AI used: gpt-5.

How can I tell if a user has entered a shipping or percentage discount? If I try to access Input.cart.discount_code.percentage and they used a shipping code, I get an error. I noticed in the docs Inputs.cart.discount_code returns "CartDiscount::Shipping, CartDiscount::Percentage, etc. " but I can’t seem to figure out the proper syntax:

if Input.cart.discount_code == CartDiscount::Shipping 
  #do something when user enters shipping discount code
end

Did you ever figure this out? I’m struggling with the same thing.

I tried this and seems working,

my_string = Input.cart.discount_code.to_s
if my_string.include? “FixedAmount”
puts “String includes ‘FixedAmount’”
end