Skip to content

Instantly share code, notes, and snippets.

@matthewbeta
Last active April 4, 2023 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewbeta/592ce749bb90f0b3e6a1fd76393ee375 to your computer and use it in GitHub Desktop.
Save matthewbeta/592ce749bb90f0b3e6a1fd76393ee375 to your computer and use it in GitHub Desktop.
Shopify Script - Free Shipping with purchase of a specific product
# This is the product you need to buy to get free shipping
PRODUCT_ID = 000000000000
# This is the name of the shipping rate you want to discount (you can probably ad more options here and loop over them or soemthing
SHIPPING_NAME = "Standard Shipping"
# This is updated shipping name
UPDATED_SHIPPING_NAME = "FREE Standard Shipping"
# This is the message that will appear next to the doiscounted shipping rate option
MSG = "Buy X get Free Delivery offer"
is_in_cart = false
# Test if the item is in the cart
Input.cart.line_items.select do |line_item|
product = line_item.variant.product
if product.id == PRODUCT_ID
is_in_cart = true
end
end
# It is so make shipping free
if is_in_cart
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.name == SHIPPING_NAME
shipping_rate.change_name(UPDATED_SHIPPING_NAME, { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MSG)
end
end
@ejscheepers
Copy link

Hi, hope it’s going well. Do you know if this code is still working? Where would I install this script?

@nsbabrah
Copy link

nsbabrah commented Apr 4, 2023

[Error] undefined method 'map' for nil
shopify/std_lib/cart_shipping_rates/output.rb:7:in #Class:0x7faad27081c0.to_hash
shopify/std_lib/core/script_kernel.rb:12:in Object.prepare_output

Gives error to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment