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 29 and 35 to match the actual thresholds of your variants.
  • If you have more than an A and B variant add those after line 39.
  • Change 'Standard Shipping' on line 26 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
6
# Calculate subtotal after discount code is applied
7
if Input.cart.discount_code
8
if Input.cart.discount_code.class == CartDiscount::Percentage
9
subtotal_cents = subtotal_cents - (subtotal_cents * Input.cart.discount_code.percentage / 100)
10
end
11
if Input.cart.discount_code.class == CartDiscount::FixedAmount
12
subtotal_cents = subtotal_cents - Input.cart.discount_code.amount.cents
13
end
14
end
15
16
17
# Determine which variant a visitor is in
18
Input.shipping_rates.each do |shipping_rate|
19
if shipping_rate.source === 'ShipScout'
20
variant = shipping_rate.code.include?("fallback") ? "A" : shipping_rate.code.split("_").last #this will set "variant" value to A, B, C etc.
21
end
22
end
23
24
25
# Discount the ShipScout rate
26
Input.shipping_rates.each do |shipping_rate|
27
if shipping_rate.name === 'Standard Shipping'
28
if variant === 'A'
29
# variant A
30
if subtotal_cents >= 3000
31
# if $30 or above
32
shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')
33
end
34
elsif variant === 'B'
35
# variant B
36
if subtotal_cents >= 5000
37
# if $50 or above
38
shipping_rate.apply_discount(shipping_rate.price, message: 'Free shipping')
39
end
40
end
41
end
42
end
43
44
45
# Delete the ShipScout rate here since we are only using it as an indicator to show/hide other rates
46
Input.shipping_rates.delete_if do |shipping_rate|
47
shipping_rate.source == 'ShipScout'
48
end
49
50
51
# Output the rates with these changes
52
Output.shipping_rates = Input.shipping_rates
Copied!
Last modified 2mo ago