Personalized checkout and custom promotions with Shopify Scripts
I have a script that we use to give the customer free shipping based on if their cart contains all items with a specific tag, but I am wonder how to make it so we can use multiple tags. For example, if a customer has two items in their cart where product1 has a tag of "item1" and product2 has a tag of "item2", the cart would show free shipping.
Here is my working script for a single tag:
FREE_TAG = 'Club:Murfreesboro SC'
if Input.cart.shipping_address.country_code == 'US' && Input.cart.line_items.all?{|item| item.variant.product.tags.include? FREE_TAG}
Input.shipping_rates.each do |shipping_rate|
next if shipping_rate.price == Money.zero
shipping_rate.apply_discount(shipping_rate.price, message: "Free Shipping Item(s)!")
break
end
end
Output.shipping_rates = Input.shipping_rates
But I want something like this:
FREE_TAG = ['Club:Murfreesboro SC','Club:Northland United']
if Input.cart.shipping_address.country_code == 'US' && Input.cart.line_items.all?{|item| item.variant.product.tags.include? FREE_TAG}
Input.shipping_rates.each do |shipping_rate|
next if shipping_rate.price == Money.zero
shipping_rate.apply_discount(shipping_rate.price, message: "Free Shipping Item(s)!")
break
end
end
Output.shipping_rates = Input.shipping_rates
Is there any way to have multiple tags in the FREE_TAG?
Thanks!
Solved! Go to the solution
This is an accepted solution.
Sorry, small typo:
FREE_TAG = ['Club:Murfreesboro SC','Club:Northland United']
if Input.cart.shipping_address.country_code == 'US' && Input.cart.line_items.all?{ |item| item.variant.product.tags.any? { |x| FREE_TAG.include?(x)} }
Input.shipping_rates.each do |shipping_rate|
next if shipping_rate.price == Money.zero
shipping_rate.apply_discount(shipping_rate.price, message: "Free Shipping Item(s)!")
break
end
end
Output.shipping_rates = Input.shipping_rates
Hi Thomas, haven't had a chance to test it, switching your script to this might work.
FREE_TAG = ['Club:Murfreesboro SC','Club:Northland United']
if Input.cart.shipping_address.country_code == 'US' && Input.cart.line_items.all?{ item.variant.product.tags.any? { |x| FREE_TAG.include?(x)} }
Input.shipping_rates.each do |shipping_rate|
next if shipping_rate.price == Money.zero
shipping_rate.apply_discount(shipping_rate.price, message: "Free Shipping Item(s)!")
break
end
end
Output.shipping_rates = Input.shipping_rates
Let me know if that is helpful,
Matthew
Hi Matthew,
Thank you for the help. It doesn't seem to work and shows the following run error:
[Error] undefined method 'item' for main
Free Shipping for Club Tag:3:in Object.call
shopify/std_lib/core/list.rb:41:in List.each
Free Shipping for Club Tag:3
This is an accepted solution.
Sorry, small typo:
FREE_TAG = ['Club:Murfreesboro SC','Club:Northland United']
if Input.cart.shipping_address.country_code == 'US' && Input.cart.line_items.all?{ |item| item.variant.product.tags.any? { |x| FREE_TAG.include?(x)} }
Input.shipping_rates.each do |shipping_rate|
next if shipping_rate.price == Money.zero
shipping_rate.apply_discount(shipping_rate.price, message: "Free Shipping Item(s)!")
break
end
end
Output.shipping_rates = Input.shipping_rates
Matthew, that worked perfectly! Thank you so much!
Hi,
This script is exactly what I've been looking for. As fairly new to Shopify where would I place this script/ which file?
Thank you
You have to be on a Shopify Plus plan and install the Script Editor. Here is some more info: https://help.shopify.com/en/manual/checkout-settings/script-editor
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024