Back to Blog

2021-12-09

shopifyguides

Shopify Shipping Scripts to get you started

Shopify Shipping Scripts to get you started

I wanted to write a few scripts to help people get started with Shipping scripts. Let me know if you have any comments or want to see anything else for how these work.


1. Discounted Shipping if cart is over $X

min_discount_order_amount = Money.new(cents:100) * 50
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
              0.1
            else
              0
            end
message = "10% off shipping if order is over $50"

Input.shipping_rates.each do |shipping_rate|
  next unless shipping_rate.source == "shopify"
  shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end

Output.shipping_rates = Input.shipping_rates

2. Free shipping if the cart is over $X

min_discount_order_amount = Money.new(cents:100) * 50
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
              1
            else
              0
            end
message = "Free shipping if order is over $50"

Input.shipping_rates.each do |shipping_rate|
  next unless shipping_rate.source == "shopify"
  shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end

Output.shipping_rates = Input.shipping_rates

3. Free shipping on rate based on the name of the rate

discount = 1
message = "Free Standard Shipping"

Input.shipping_rates.each do |shipping_rate|
  next unless shipping_rate.source == "shopify"
  next unless shipping_rate.name == "Standard Shipping"
  shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end

Output.shipping_rates = Input.shipping_rates