Free shipping with large item exclusions

Josef_Lagunas
New Member
2 0 0

Hello all, I've been stumped on this for a few days now and in need of some assistance. I'm trying to update our current shipping script to:

  1. remove all shipping rates for international orders that contain a "large item/yoga mat" and show error message at checkout
  2. if the order is to Canada, over $150, and does not contain a "large item/yoga mat"- it ships free
  3. and any order to USA over $120 can ship for free (regardless of cart)

This is our current script:

# RateCampaign
# ============
class RateCampaign
  
  # Initializes the campaign.
  def initialize(rateToHide, freeShippingCriteria)
    @rateToHide = rateToHide.downcase
    @freeShippingCriteria = freeShippingCriteria
  end
  
  # Check the cart is over $100 in US or $150 in Canada
  # and also does not contain any large items
  def run(cart, rates)
    
    country = cart.shipping_address.country_code;
    cart_total = cart.subtotal_price;
    has_large_item = false
    trigger_free_shipping = false
    
    # check each item in cart if large item
    cart.line_items.each do |line_item|
      product = line_item.variant.product
      next if product.gift_card?
      next unless product.tags.include?('FITNESS/YOGA MATS')
      has_large_item = true
    end
    
    # make sure country is one that applies
    if @freeShippingCriteria[country]

      # if no heavy items and price is right, trigger free shipping
      if cart_total.cents >= @freeShippingCriteria[country][0]
        unless has_large_item && country == 'CA'
          trigger_free_shipping = true
        end
      end
      
      # unless cart criteria is met, hide shipping rate
      unless trigger_free_shipping
        rates.delete_if { |rate| 
          rate.name.downcase.include?(@rateToHide) 
        }
      end
    else
      if has_large_item
        rates.delete_if { |rate| 
          rate.name.downcase.include?("international") || rate.name.downcase.include?("priority insured required")
        }
      end
    end
  end
  
end

CAMPAIGNS = [
  RateCampaign.new(
    'Free!', # name of free shipping rate to show
    {
      'US' => [10000], # country code, amount for free shipping (cents)
      'CA' => [15000]  # country code, amount for free shipping (cents)
    }
  )  
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.shipping_rates)
end

Output.shipping_rates = Input.shipping_rates

 

0 Likes
Josef_Lagunas
New Member
2 0 0

This is what we I ended up with incase anyone can use this or modify it to fit their needs.

# RateCampaign
# ============
class RateCampaign
  
  # Initializes the campaign.
  def initialize(rateToHide, freeShippingCriteria)
    @rateToHide = rateToHide.downcase
    @freeShippingCriteria = freeShippingCriteria
  end
  
  # Check the cart is over $100 in US or $150 in Canada
  # and also does not contain any large items
  def run(cart, rates)
    
    country = cart.shipping_address.country_code;
    cart_total = cart.subtotal_price;
    has_large_item = false
    trigger_free_shipping = false
    
    # check each item in cart if large item
    cart.line_items.each do |line_item|
      product = line_item.variant.product
      next if product.gift_card?
      next unless product.tags.include?('FITNESS/YOGA MATS')
      has_large_item = true
    end
    
    # if country is not US and has large item
    if country != 'US' && has_large_item
      
      rates.delete_if { |rate| rate.name.downcase.include?("international") || rate.name.downcase.include?("priority insured required") || rate.name.downcase.include?("standard") }
      rates.delete_if { |rate| rate.name.downcase.include?(@rateToHide) }
      
    end
    
    
  end
  
end

CAMPAIGNS = [
  RateCampaign.new(
    'Free!', # name of free shipping rate to show
    {
      'US' => [10000], # country code, amount for free shipping (cents)
      'CA' => [15000]  # country code, amount for free shipping (cents)
    }
  )
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.shipping_rates)
end

Output.shipping_rates = Input.shipping_rates

 

0 Likes