Tourist
9 0 1

## 20% Discount with a \$100 limit

Hi guys!

I'm having a hard time using the script editor to figure out how I can implement this:

I'll give an influencer a promo code, say, INFLUENCER20, where it'll give all who use it 20% discount on their total order—but only up to \$100. If their carts, therefore, exceed \$500, instead of getting 20% they max out their discounts to just \$100.

Thanks for any insight!

Shopify Staff
3 0 0

## Re: 20% Discount with a \$100 limit

Hi Kevinrein,

Here’s an example script/apprach you can explore: Shopify Script - Max discount based on cart value

Steps:

1. Create a Discount code “INFLUENCER20” with 0% discount
2. Insert line item script in the Script editor app

example script:

 # Check the cart has a discount code and if so it equals the campaignif Input.cart.discount_code and Input.cart.discount_code.code == 'INFLUENCER20'  # Conditions for when the maximum discount value applies, in this case 500 or more  if Input.cart.subtotal_price >= Money.new(cents:50000)    # Value of the maximum discount that the cart will have    max_discount = 100        # Get Float value of the cart's cents variable in dollars    cart_subtotal_float = Float((Input.cart.subtotal_price.cents / 100).to_s)      # For each line item    Input.cart.line_items.each do |line_item|      # Get Float value of line item's cents variable in dollars      line_price_float = Float((line_item.line_price.cents / 100).to_s)            # Calculate percentage the line items price should be reduce to    percentage = 1 - (((line_price_float / cart_subtotal_float) * max_discount) / line_price_float)      # Multiple the line item by the percentage value it needs to be      line_item.change_line_price(line_item.line_price * percentage, message: "\$100 off")    end      # If they are under the threshold reduce every line item by 0.2 (20% off)  elsif Input.cart.subtotal_price < Money.new(cents:50000)    Input.cart.line_items.each do |line_item|      line_item.change_line_price(line_item.line_price * 0.8, message: "20% off")    end  end  endOutput.cart = Input.cart

3. Test, Save and Publish

Top Contributors
Community Browser