Reorder Shipping Options by Name not working

Reorder Shipping Options by Name not working

NicoSpoke
Shopify Partner
23 0 15

Hi,

 

This is what I want to achieve:

  1. If a discount name exists at checkout I want to reorder my shipping methods by name
  2. Automatically select the first shipping method available

The problem I'm finding:

  1. Reordering the shipping options is working when a user enters a specific discount name
  2. However the selected options is not the first shipping method available, it keeps the previously selected shipping method

 

This is my code:

discount_applies = Input.cart.discount_code && Input.cart.discount_code.code == "FREEDOM"
# Create array of shipping methods
desired_order = ["Next Working Day"]

if discount_applies
  # Reorder the payment gateway methods to the order of desired_order.
  Output.shipping_rates = Input.shipping_rates.sort_by do |shipping_rate|
  desired_order.index(shipping_rate.name) || desired_order.length
  end
else
  Output.shipping_rates = Input.shipping_rates
end

 

See attached image to see the results when adding the discount code

Screenshot 2021-05-20 at 20.27.13.jpg

Thanks,

N

 

Building digital interfaces for Shopify Stores
Replies 0 (0)