Enforce stricter thresholds with discount codes

Why are customers getting free shipping even though they aren't spending enough to meet the free shipping threshold?

This is most likely because of discount codes. By way of example: You set free shipping for orders $50 and above. A customer completes checkout on a $44 order and gets free shipping. The threshold is calculated before discounts are applied so if the customer had a $55 order they qualified for free shipping even if though they applied a 20% discount code at checkout bringing their total down to $44.

This is not always ideal even though it averages out and still creates accurate test results. The reason for this behavior is that Shopify does not pass shipping apps like ShipScout any details on the discounts applied.

Here's how to fix this by using Shopify Scripts

In this example we will show how to test the following variants while taking into account the subtotal after a discount code has been applied.

  • Variant A: Free shipping on orders $30 and above

  • Variant B: Free shipping on order $50 and above

Step 1: Create a shipping rate in Shopify on the desired zone. Set the Price of the shipping rate to be whatever amount you'd like the customer to pay when they are above the free shipping threshold. We will call the rate Standard Shipping:

Step 2: Launch a new threshold test in ShipScout with your 2 variants:

Step 3: Create a new blank template Shipping rates Shopify Script:

Paste the following code into the code area. Before publishing the script you will need to make some modifications:

  • Change the threshold cents in lines 27 and 33 to match the actual thresholds of your variants.

  • If you have more than an A and B variant add those after line 36.

  • Change 'Standard Shipping' on line 24 to match exactly the rate name you set in Step 1 and the rate name in Shopify.

That's it, launch your script!

# Set defaults
variant = "A"
subtotal_cents = Input.cart.subtotal_price.cents

# Calculate subtotal after discount code is applied
if Input.cart.discount_code
  if Input.cart.discount_code.class == CartDiscount::Percentage
    subtotal_cents = subtotal_cents - (subtotal_cents * Input.cart.discount_code.percentage / 100)
  if Input.cart.discount_code.class == CartDiscount::FixedAmount
    subtotal_cents = subtotal_cents - Input.cart.discount_code.amount.cents

# Determine which variant a visitor is in
Input.shipping_rates.each do |shipping_rate|
  if shipping_rate.source === 'ShipScout'
    variant = shipping_rate.code.include?("fallback") ? "A" : shipping_rate.code.split("_").last #this will set "variant" value to A, B, C etc.

# Discount the ShipScout rate
Input.shipping_rates.each do |shipping_rate|
  if shipping_rate.name === 'Standard Shipping'
    if variant === 'A'
      # variant A
      if subtotal_cents >= 3000
        # if $30 or above
        shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')
    elsif variant === 'B'
      # variant B
      if subtotal_cents >= 5000
        # if $50 or above
        shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')

# Delete the ShipScout rate here since we are only using it as an indicator to show/hide other rates
Input.shipping_rates.delete_if do |shipping_rate|
  shipping_rate.source == 'ShipScout'

# Output the rates with these changes
Output.shipping_rates = Input.shipping_rates

Last updated