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:
Make sure that the Title is not the same as any Shopify shipping rate name (e.g. Standard Shipping). In this example we name it ShipScout. This will not be displayed to customers.
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!
1
# Set defaults
2
variant = "A"
3
subtotal_cents = Input.cart.subtotal_price.cents
4
5
# Calculate subtotal after discount code is applied
6
if Input.cart.discount_code
7
if Input.cart.discount_code.class == CartDiscount::Percentage
8
subtotal_cents = subtotal_cents - (subtotal_cents * Input.cart.discount_code.percentage / 100)
9
end
10
if Input.cart.discount_code.class == CartDiscount::FixedAmount
11
subtotal_cents = subtotal_cents - Input.cart.discount_code.amount.cents
12
end
13
end
14
15
# Determine which variant a visitor is in
16
Input.shipping_rates.each do |shipping_rate|
17
if shipping_rate.source === 'ShipScout'
18
variant = shipping_rate.code.include?("fallback") ? "A" : shipping_rate.code.split("_").last #this will set "variant" value to A, B, C etc.
19
end
20
end
21
22
# Discount the ShipScout rate
23
Input.shipping_rates.each do |shipping_rate|
24
if shipping_rate.name === 'Standard Shipping'
25
if variant === 'A'
26
# variant A
27
if subtotal_cents >= 3000
28
# if $30 or above
29
shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')
30
end
31
elsif variant === 'B'
32
# variant B
33
if subtotal_cents >= 5000
34
# if $50 or above
35
shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')
36
end
37
end
38
end
39
end
40
41
42
# Delete the ShipScout rate here since we are only using it as an indicator to show/hide other rates
43
Input.shipping_rates.delete_if do |shipping_rate|
44
shipping_rate.source == 'ShipScout'
45
end
46
47
48
# Output the rates with these changes
49
Output.shipping_rates = Input.shipping_rates
Copied!