Free shipping for specific item tags

Solved
Thomas_Kessel
Tourist
6 0 0

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!

Accepted Solution (1)

Accepted Solutions
playwright-matt
Shopify Partner
72 18 27

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
Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.

View solution in original post

Replies 6 (6)
playwright-matt
Shopify Partner
72 18 27

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 

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.

Thomas_Kessel
Tourist
6 0 0

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

 

playwright-matt
Shopify Partner
72 18 27

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
Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.

Thomas_Kessel
Tourist
6 0 0

Matthew, that worked perfectly! Thank you so much!

phil_brad
New Member
1 0 0

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

Thomas_Kessel
Tourist
6 0 0

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