Last active
April 4, 2023 15:29
Shopify Script - Free Shipping with purchase of a specific product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
[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
Hi, hope it’s going well. Do you know if this code is still working? Where would I install this script?