With Tapcart and Shopify Scripts, you are able to optimize and automate your customer experience in multiple ways. Shopify Scripts are small pieces of code that allow you to create personalized experiences for your customers in their cart and at checkout. You can use Scripts to create discounts that are applied to a checkout based on the items in that cart, customize the shipping and payment options that are available, and more!
Important note: All Shopify scripts intended to be used in the app must be published to both the Online Store and Storefront API Sales Channels.
Script Examples
Listed below, are examples of different discounts and customizations you can create with Shopify Scripts:
discount products with specific tags to offer a percentage (%) or fixed ($) discounts, or a combination of both
run promotions with simple or complex logic (buy one, get one free (BOGO); buy two get 10% off, buy four get 20% off)
offer dynamic pricing with volume-based price breaks
modify, hide, or re-order shipping options and prices
modify, hide, or re-order payment gateway methods
Here is an example script you can use to create a discount for specific products:
# ================================ Customizable Settings ================================
# ================================================================
# Discount by Product
#
# Any matching item will be discounted by the entered amount.
#
# - 'product_selector_match_type' determines whether we look for
# products that do or don't match the entered selectors. Can
# be:
# - ':include' to check if the product does match
# - ':exclude' to make sure the product doesn't match
# - 'product_selector_type' determines how eligible products
# will be identified. Can be either:
# - ':tag' to find products by tag
# - ':type' to find products by type
# - ':vendor' to find products by vendor
# - ':product_id' to find products by ID
# - ':variant_id' to find products by variant ID
# - ':subscription' to find subscription products
# - ':all' for all products
# - 'product_selectors' is a list of identifiers (from above)
# for qualifying products. Product/Variant ID lists should
# only contain numbers (ie. no quotes). If ':all' is used,
# this can also be 'nil'.
# - 'discount_type' is the type of discount to provide. Can be
# either:
# - ':percent'
# - ':dollar'
# - 'discount_amount' is the percentage/dollar discount to
# apply (per item)
# - 'discount_message' is the message to show when a discount
# is applied
# ================================================================
PRODUCT_DISCOUNTS = [
{
product_selector_match_type: :include,
product_selector_type: :tag,
product_selectors: ["your_tag"],
discount_type: :percent,
discount_amount: 10,
discount_message: '10% off tagged products!'
}
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# ProductSelector
#
# Finds matching products by the entered criteria.
# ================================================================
class ProductSelector
def initialize(match_type, selector_type, selectors)
@match_type = match_type
@comparator = match_type == :include ? 'any?' : 'none?'
@selector_type = selector_type
@selectors = selectors
end
def match?(line_item)
if self.respond_to?(@selector_type)
self.send(@selector_type, line_item)
else
raise RuntimeError.new('Invalid product selector type')
end
end
def tag(line_item)
product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors & product_tags).send(@comparator)
end
def type(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@match_type == :include) == @selectors.include?(line_item.variant.product.product_type.downcase.strip)
end
def vendor(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@match_type == :include) == @selectors.include?(line_item.variant.product.vendor.downcase.strip)
end
def product_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.product.id)
end
def variant_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.id)
end
def subscription(line_item)
!line_item.selling_plan_id.nil?
end
def all(line_item)
true
end
end
# ================================================================
# DiscountApplicator
#
# Applies the entered discount to the supplied line item.
# ================================================================
class DiscountApplicator
def initialize(discount_type, discount_amount, discount_message)
@discount_type = discount_type
@discount_message = discount_message
@discount_amount = if discount_type == :percent
1 - (discount_amount * 0.01)
else
Money.new(cents: 100) * discount_amount
end
end
def apply(line_item)
new_line_price = if @discount_type == :percent
line_item.line_price * @discount_amount
else
[line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
end
line_item.change_line_price(new_line_price, message: @discount_message)
end
end
# ================================================================
# ProductDiscountCampaign
#
# Any matching item will be discounted by the entered amount.
# ================================================================
class ProductDiscountCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
@campaigns.each do |campaign|
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
discount_applicator = DiscountApplicator.new(
campaign[:discount_type],
campaign[:discount_amount],
campaign[:discount_message]
)
cart.line_items.each do |line_item|
next unless product_selector.match?(line_item)
discount_applicator.apply(line_item)
end
end
end
end
CAMPAIGNS = [
ProductDiscountCampaign.new(PRODUCT_DISCOUNTS),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart
You can create the following types of scripts:
Line item scripts
Line item scripts affect line items in a cart, they can change prices and grant discounts. This script will run every time that an item is added, removed, or modified in your cart
Shipping scripts
This type of script will interact with all shipping modifications, it can change shipping methods and grant discounts on shipping rates. These scripts will run every time your customer accesses the shipping options page at checkout.
Payment scripts
Payment scripts interact with all payment modifications, you can rename, hide and re-order payment gateways. These scripts will run every time your customer accesses the payment methods page at checkout. Please note, payment scripts do not interact with payment gateways that are shown to your customer before checkout, such as PayPal Express or Apple Pay.
The Script Editor includes templates of common scripts. When you create a script, you can choose a template and modify it for the needs of your store.
The following list contains examples of the templates that you can customize:
Percentage (%) off a product
Amount ($) off a product
Percentage (%) and amount ($) off a product
Bulk discounts
Buy one get one free (BOGO)
Modify the shipping rate price
Modify the shipping rate name
Hide shipping rates
Re-order shipping rates
Modify the payment gateway name
Hide the payment gateway
Re-order payment gateways
Important Note: Using line item, shipping, and payment scripts you can implement custom logic and tailor the user experience during a customer’s checkout journey.
Considerations
Shopify Scripts are only available to Shopify Plus merchants, to learn more about Shopify Plus please access this link.
More Questions
To learn more about Tapcart, visit Tapcart Academy to become a mobile app expert!
Have more questions on Shopify Scripts, we’re happy to help! Please reach out to us through LiveChat on your Tapcart Dashboard or email us at [email protected]!