Specific discount code for free specific shipping method

Highlighted
New Member
28 0 0

I searched high and low for documentation on this and couldn't find anything. I'm posting what I came up with on here for anyone that may find this useful. 

Define the specific code in the 1st line, then define the name of the shipping method in the 6th line.

The code needs to be setup in Shopify as a percentage discount code, set to 0% off the entire order. 

discountCode = 'FreeSecondDayAir'

Input.cart.line_items.each do |line_item|
  next unless Input.cart.discount_code
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.name.downcase.include? "second"
    shipping_rate.apply_discount(shipping_rate.price * 1.00, message: "Free Second Day Air!") if Input.cart.discount_code.code == discountCode
end
end

Output.shipping_rates = Input.shipping_rates

 

0 Likes