LogoLogo
  • Help Center
  • Welcome To Inveterate
  • Guides
    • Getting Started
      • Install The Inveterate App
        • App Installation
        • Creating Your Inveterate Account
      • Set Up Company Profile
      • Enable Inveterate On Your Store
      • Adjust Customer Account Settings In Shopify
      • Add Membership Link Snippet to Customer Account Template
    • Program Setup
      • Tier Setup
        • Editing Tiers
        • Deleting Tiers
        • Free Trials For Paid Tiers
        • Universal Rebill Date
      • Benefits
        • Credit Based Benefits
          • Credit Expiration
          • Enabling Credit Based Benefits
          • Recurring Store Credits
          • Credits For Orders
          • Anniversary Credits
          • Store Credits For Purchase Referral
          • Sign Up Store Credits
        • Savings Based Benefits
          • Free Shipping
          • Member Only Discounts
          • Sign Up Discounts
          • Member Only Pricing
        • Access Based Benefits
          • Early Access
          • Exclusive Access
        • Gift Based Benefits
          • Free Gift At Signup
      • Landing Page
        • About The Landing Page
        • Configuring the Landing Page
          • Hero
          • Testimonial
          • Benefits
          • CTA
          • FAQ
        • Adding the Landing Page to Your Store
        • Landing Page Versioning
        • Adding Custom CSS Styling
      • Messaging
        • Content
        • Messaging Settings
    • Members
      • Member Portal
      • Manage Members
        • Canceling Members
        • Tier Upgrades and Downgrades
        • Members Page
        • Creating Segments
        • Exporting Member List
      • Member Profile
        • Fundamentals
        • Adjusting Credits
        • Anonymize Members
      • Shopify Account Pages
    • Settings
      • Users & Access
      • Profile Settings
      • Password Reset
      • Account Settings
    • Integrations
      • Shopify POS
      • Tapcart
      • Aftersell
      • Gorgias
      • Postscript
      • Klaviyo
      • Rebuy
      • Attentive
        • How to Enable Integration
        • Segmenting Customers by Inveterate's Membership Custom Attributes in Attentive
        • How to Create Journeys in Attentive with Inveterate's Custom Events
        • Attentive Custom Events List
  • Strategy & Best Practices
    • Memberships 101
  • Discount Information
    • Apply Discounts To Subscription Products
    • Editing Discount Codes
    • Codes For Free Memberships
    • Combining Discounts
  • Tag Information
    • Customer Tags
    • Order Tags
  • Advanced Guides
    • Editing Free Tier Signup Modals
    • Membership Currency Localization
    • Migrating From Scripts To Functions
    • Migrating To Tiers
    • Spend Based Tiers
      • About Spend Based Tiers
      • Spend Based Tiers Setup
      • About The Landing Page
    • Search Engine Crawling
    • Advanced Settings
      • App Embeds Settings
      • Early Access Settings
      • Exclusive Access Settings
    • Shopify Scripts
      • Free Shipping Script
      • Member Only Pricing
      • Free Gift At Sign Up
    • Theme Editor Sections
      • PDP Upsell Widget
    • Replace Inveterate Emails In Klaviyo
      • How to Send Member Notification Emails with Klaviyo
      • Klaviyo Member Notification Metrics and Their Template Variables
    • Sunsetting Your Membership Program
    • Shopify Flow
      • Flow Library
Powered by GitBook
On this page
  • Editable Values
  • Script

Was this helpful?

  1. Advanced Guides
  2. Shopify Scripts

Free Shipping Script

PreviousShopify ScriptsNextMember Only Pricing

Last updated 1 year ago

Was this helpful?

Shopify Plus Only

This is an advanced guide.

Only one shipping script can be active at a time. If you already have a shipping script running and you want to run this script in tandem, this will not work. You'll need to customize the scripts to work together.

This guide requires the app.

By default we utilize discount codes to provide free shipping to subscribers. Due to Shopify's limitation, only allowing 1 discount code to be used at checkout, this is not always the most desirable solution. A more robust solution is to utilize the Shopify Script Editor app to create a free shipping script. This will allow your customers to utilize any current discount codes your providing as well as take advantage of their free shipping benefit at the same time.

Editable Values

  • MESSAGE - {string} The message that will be displayed next to discounted shipping items.

  • MINIMUM_PURCHASE_AMOUNT - {number | nil} The amount a customer must purchase before this discount is applied. Measured in dollars. Set to nil to remove this check.

  • MINIMUM_QUANTITY_ITEMS - {number | nil} The amount of items that must be in cart before this discount is applied. Set to nil to remove this check.

  • MAXIMUM_SHIPPING_PRICE - {number | nil} The maximum amount a shipping item can cost to allow this discount to apply. Set to nil to remove this check.

  • PRODUCT_ID_LIST - {array[number]} An array of product IDs that must be in cart to get the discount. All items in cart must be represented in the array for the discount to apply. If left empty, discount will apply to all products.

  • COUNTRY_CODE_LIST - {array[string]} An array of that the shipping address must match to receive the discount. If left empty, discount will apply to all countries.

Script

# Editable Values
MESSAGE = "Free shipping for members!"
MINIMUM_PURCHASE_AMOUNT = nil
MINIMUM_QUANTITY_ITEMS = nil
MAXIMUM_SHIPPING_PRICE = nil
PRODUCT_ID_LIST = []
COUNTRY_CODE_LIST = []

########
# DO NOT EDIT PAST THIS POINT
########

class InveterateFreeShipping 
  def initialize()
    @percent_off = 100 * 0.01
    @message = MESSAGE
    @minimum_purchase_amount = MINIMUM_PURCHASE_AMOUNT
    @minimum_quantity_items = MINIMUM_QUANTITY_ITEMS
    @maximum_shipping_price = MAXIMUM_SHIPPING_PRICE
    @product_id_list = PRODUCT_ID_LIST
    @country_code_list = COUNTRY_CODE_LIST
  end

  def run(shipping_rates, cart)
    @shipping_rates = shipping_rates
    @cart = cart
    start
  end

  private

  def check_purchase_amount
    return true unless @minimum_purchase_amount
    min_price = Money.derived_from_presentment(customer_cents:@minimum_purchase_amount)
    return @cart.subtotal_price > min_price
  end

  def check_item_quantity
    return true unless @minimum_quantity_items
    return @cart.line_items.size >= @minimum_quantity_items
  end

  def check_max_shipping_price(shipping_rate)
    return true unless @maximum_shipping_price
    max_shipping_price = Money.derived_from_presentment(customer_cents: @maximum_shipping_price)
    return shipping_rate.price <= max_shipping_price
  end

  def check_product_ids
    return true unless @product_id_list.size > 0
    line_item_ids = @cart.line_items.map do |line_item|
      line_item.variant.product.id
    end
    reduced_array = line_item_ids - @product_id_list
    return true unless reduced_array.size > 0
  end

  def check_countries
    return true unless @country_code_list.size > 0
    return @country_code_list.include? @cart.shipping_address.country_code
  end
  
  def start
    return unless @cart.customer
    return unless @cart.customer.tags.include? "inveterate-subscriber"
    return unless check_purchase_amount
    return unless check_item_quantity
    return unless check_product_ids
    return unless check_countries
    
    @shipping_rates.each do |shipping_rate|
      return unless check_max_shipping_price(shipping_rate)

      shipping_rate.apply_discount(
        shipping_rate.price * @percent_off,
        message: @message
      )
    end
  end
end

CAMPAIGNS = [
  InveterateFreeShipping.new()
]

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

Output.shipping_rates = Input.shipping_rates
Shopify Script Editor
ISO 3166-1 alpha-2 country code designators