Shopify Carrier Service reorder shipping options

If you use the Shopify API Carrier Service to populate your shipping by realtime to add pickup-points and other custom shipping options, you might want to re-order your shipping options in a certain way.

I wanted to have ‘normal’ shipping options up top, and pickup points below, ordered by distance, but at this point shopify does not have a sort_order property that you can send with your response to give a certain order to your shipping options.

Luckily with shopify scripts we are able to get the right sorting, in your API code you will need to prefix a number to all your shipping names, the shopify script will sort by the first character in the name, and remove the first character before the option gets displayed.
I have a free shipping option within shopify too, which I added a 0 for in it’s name(e.g. 0Free Shipping) so it wil be always displayed on top.

 

The shopify script (you need to add the shopify script editor from the app store):

#I have a lot of free shipping options above x order inside shopify for 
#every zone, I need to have that on top if it is shown
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source != "My Custom API Rates"
  shipping_rate.change_name("0" + shipping_rate.name, message: "")
end

#re-order shipping options
Input.shipping_rates.sort_by!(&:name)


#remove first character from shipping name
Input.shipping_rates.each do |shipping_rate|
  shipping_rate.change_name(shipping_rate.name[1..-1], message: "")
end

Output.shipping_rates = Input.shipping_rates

 

 Now I can display the rates in the order I like.

Leave a Reply

Your email address will not be published.