Shopify scripts

If your store is on Shopify Plus you can use the Script Editor app from Shopify to create custom Shipping Scripts.

These custom scripts will open up the possibilities of what you can test with ShipScout.

The basic concept is that you can create all the shipping rates in Shopify for all the different variants and then use a Script to detect which variant a visitor is in and hide all the irrelevant rates.

Here is a walkthrough of some of the script logic to get started. To detect which variant a visitor is in:

# Set defaults
variant = "A"

# Determine which variant a visitor is in
Input.shipping_rates.each do |shipping_rate|
  if shipping_rate.source === 'ShipScout'
    shipping_rate_code = shipping_rate.code
    shipping_rate_code.slice! "-bypass" #clean up forced variant postfix
    shipping_rate_code.slice! "_fallback" #clean up fallback postfix
    variant = shipping_rate_code.split("_").last #sets "variant" value to A, B, C etc.
  end
end

Then you can add your logic after this. For example, if you set up "Standard Shipping - A" ($5) and "Standard Shipping - B" ($8) rates in Shopify and want to A/B test $5 shipping against $8 shipping you could use this code:

Input.shipping_rates.delete_if do |shipping_rate|
  if variant === 'A'
    shipping_rate.name === 'Standard Shipping - B' #if in variant A then delete B's rate
  elsif variant === 'B'
    shipping_rate.name === 'Standard Shipping - A' #if in variant B then delete A's rate
  end
end

You can see how the simple example above could be expanded to test more complex sets of dynamic or weight-based tiered rates that have been set up in Shopify.

Then lastly, since we just used the ShipScout rate as an indicator of which variant the visitor is in and we don't actually want to show that to the customer we can remove the ShipScout rate like this:

# 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'
end

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

Here's the entire script put together:

# Set defaults
variant = "A"
# Determine which variant a visitor is in
Input.shipping_rates.each do |shipping_rate|
  if shipping_rate.source === 'ShipScout'
    shipping_rate_code = shipping_rate.code
    shipping_rate_code.slice! "-bypass" #clean up forced variant postfix
    shipping_rate_code.slice! "_fallback" #clean up fallback postfix
    variant = shipping_rate_code.split("_").last #sets "variant" value to A, B, C etc.
  end
end

# Remove rates not in variant
Input.shipping_rates.delete_if do |shipping_rate|
  if variant === 'A'
    shipping_rate.name === 'Standard Shipping - B' #if in variant A then delete B's rate
  elsif variant === 'B'
    shipping_rate.name === 'Standard Shipping - A' #if in variant B then delete A's rate
  end
end

# Now rename the rates
Input.shipping_rates.each do |shipping_rate|
  if shipping_rate.name.include? 'Standard Shipping -'
    shipping_rate.change_name("Standard Shipping")
  end
end

# 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'
end
# Output the rates with these changes
Output.shipping_rates = Input.shipping_rates

Note that this example expects 2 shipping rates to be set up in Shopify with the names of "Standard Shipping - B" and "Standard Shipping - A". This is because any shipping rates in Shopify that have identical names will be combined together by Shopify which is not ideal. That is why there is logic in this script to rename the rates (line 19).

Last updated