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
Are you ready to take your business to the next level? Look no further than the latest ...
By SarahF_Shopify Apr 15, 2024We’re keeping the ball rolling to make sure you’re always ahead of the game. So buckle ...
By JasonH Apr 8, 2024Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024