Shopify+ Scripts: Re-ordering shipping options (multiple shipping zones)

MikeCKN
Visitor
2 0 0

Hello friends,

Just started playing with Shopify+ Scripts and loving it so far!

Re-ordered our shipping options so they are logically grouped together, instead of ordered via price.

However, we work with multiple shipping zones, and from my understanding, the provided script only allows to re-order one of them at a time.

Is there a certain code we can add to the beginning of our Script or something so that we can order multiple shipping zones independently?

  • We tried making 2 scripts, but only 1 can be active at a given time
  • We also tried putting ALL 8 of the shipping options in the script, but the result gives a 'shipping option not found' error of sorts when running it on the website

    As for the code, we are using one of the cookie-cutter scripts provided:

 

 

 

# ================================ Customizable Settings ================================
# ================================================================
# The order in which you would like your rates to display
# ================================================================
DESIRED_RATE_ORDER = [
  "Shipping option 1", "Shipping option 2", "Shipping option 3", "Shipping option 4",
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ReorderRatesCampaign
#
# Reorders rates into the entered order
# ================================================================
class ReorderRatesCampaign
  def initialize(desired_order)
    @desired_order = desired_order.map { |item| item.downcase.strip }
  end

  def run(cart, shipping_rates)
    shipping_rates.sort_by! { |rate| @desired_order.index(rate.name.downcase.strip) || Float::INFINITY }
  end
end

CAMPAIGNS = [
  ReorderRatesCampaign.new(DESIRED_RATE_ORDER),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.shipping_rates)
end

Output.shipping_rates = Input.shipping_rates

 

 

Suggestions welcome!

Reply 1 (1)

MikeCKN
Visitor
2 0 0

Anyone?