Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Fixed maximum discount (using percantage discount code)

Fixed maximum discount (using percantage discount code)

melloman
Visitor
2 0 2

I'm using the Shopify Script editor to change the checkout when a customer enters a specific percentage discount code that there is a max limit on the total discount.

The below script taken from https://gist.github.com/fnstirling/d721dc05bf0aa2fdbba4e7ff97b35dfb does exactly what I need but for some reason when the code kicks in and limits the discount, it displays correctly on the line items but the bottom order totals are all wrong. I.e. they show the total of using the entire discount percentage (not the code limited discount).

Is there anyway to correct this?

 

# In this campaign we wanted to have 50% of every product unless the cart value was 200 or more
# If the 200 or more, the maximum discount that could apply was 100
# Wanted to spread that the discount amount across all line items proporinate to its value in the cart

# Do/change following (if required):
  # 1. Create a new coupon code in Shopify
  # 2. Replace the coupon code evaluation value ('DISCOUNT_CODE') with the new code on line 13
  # 3. Change the cart subtotal evaluation amount (Money.new(cents:20000)) on lines 16, 39
  # 4. Change the max_discount value (100) on line 18
  # 5. Change the discount amount (0.5) on line 41

# Check the cart has a discount code and if so it equals the campaign
if Input.cart.discount_code and Input.cart.discount_code.code == 'DISCOUNT_CODE'

  # Conditions for when the maximum discount value applies, in this case 200 or more
  if Input.cart.subtotal_price >= Money.new(cents:20000)
    # Value of the maxium 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, achieved by:
        # 1. Divide the line price by the cart to get the percentage of the cart it represents
        # 2. Multiple the percentage by the max discount amount to get the value the line item needs to be reduced by
        # 3. Find the percentage the line item needs to be reduced by  
        # 4. Minus the percentage the line item needs to reduce by from 1 to get end percentage value the line item needs to be
      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: "")
    end
    
  # If they are under the threshold reduce every line item by 0.5 (50% off)
  elsif Input.cart.subtotal_price < Money.new(cents:20000)
    Input.cart.line_items.each do |line_item|
      line_item.change_line_price(line_item.line_price * 0.5, message: "")
    end
  end
  
end

Output.cart = Input.cart

 

Reply 1 (1)

playwright-mike
Shopify Partner
72 18 33

Hi melloman, 

Not sure if you still need a script here, but I was able to generate one using my Playwright app. You can customize some of the options at the bottom. 

Let me know if this was helpful!

Matthew

 

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.