Shopify scripts are one of the lesser-known features of the Shopify Plus platform. However, they are probably the most powerful. They allow merchants to create Ruby scripts that can automatically discount items, offer free shipping and modify payment gateways based on who the customer is and/or what they have in their cart.
Shopify’s Shipping Scripts is an amazing way to reward customers and offer more customized shipping options.
Before we begin, you must have the Shopify Script Editor app installed. It is where you will be copying and pasting the scripts below.
Note: Shopify Scripts is limited to Shopify Plus merchants only. Not yet a Plus merchant? Check out the benefits of Shopify Plus.
To date, there are 3 different script types:
- Line item scripts. They affect line items in the cart and can change prices and grant discounts to your customers.
- Shipping scripts. They interact with the Shipping API and change shipping methods and grant discounts on shipping rates.
- Payments scripts. They interact with the Payment API and can rename, hide and reorder payment gateway.
A curated list of Shopify line item script
X% Off Shipping If Cart Value > $Y
MINIMUM_ORDER_AMOUNT = 50 #dollars required in cart to get discount
DISCOUNT = 0.1 #percentage discount (in decimal format)
MESSAGE = "10% off shipping if order is over $50" #promotional message
if Input.cart.subtotal_price_was > (Money.new(cents:100) * MINIMUM_ORDER_AMOUNT)
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == "shopify"
shipping_rate.apply_discount(shipping_rate.price * DISCOUNT, message: MESSAGE)
end
end
Output.shipping_rates = Input.shipping_rates
Storewide Discount
Whether it’s a special holiday incentive or a flash sale to attract customers, nothing gets visitors more excited than a storewide discount. A sale like this becomes an event and (when used strategically) can attract visitors and turn them into buyers, and eventually convert them to loyal, repeat customers.
# Set to the percentage discount you'd like to apply store-wide.
DISCOUNT_AMOUNT = 10
# Apply the discount to every item in the cart.
Input.cart.line_items.each do |line_item|
line_item.change_line_price(line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)), message: "#{DISCOUNT_AMOUNT}% off store-wide!")
end
Output.cart = Input.cart
Buy One, Get One Free
# Buy One, Get One Free Script
# The basis of this script provided by the Script Editor itself as a "default" script option.
# Adjusting these values lets you tweak the scope of the discount, eg:
# PAID_ITEM_COUNT = 1, DISCOUNTED_ITEM_COUNT = 1 -> Buy One, Get One
# PAID_ITEM_COUNT = 3, DISCOUNTED_ITEM_COUNT = 2 -> Buy Three, Get Two
PAID_ITEM_COUNT = 1
DISCOUNTED_ITEM_COUNT = 1
# Specify the IDs of the products you'd like to be eligible for this promotion.
ELIGIBLE_PRODUCT_IDS = [9307791812, 9307791940]
# Returns the integer amount of items that must be discounted next
# given the amount of items seen
#
def discounted_items_to_find(total_items_seen, discounted_items_seen)
Integer(total_items_seen / (PAID_ITEM_COUNT + DISCOUNTED_ITEM_COUNT) * DISCOUNTED_ITEM_COUNT) - discounted_items_seen
end
# Partitions the items and returns the items that are to be discounted.
#
# Arguments
# ---------
#
# * cart
# The cart to which split items will be added (typically Input.cart).
#
# * line_items
# The selected items that are applicable for the campaign.
#
def partition(cart, line_items)
# Sort the items by price from high to low
sorted_items = line_items.sort_by{|line_item| line_item.variant.price}.reverse
# Create an array of items to return
discounted_items = []
# Keep counters of items seen and discounted, to avoid having to recalculate on each iteration
total_items_seen = 0
discounted_items_seen = 0
# Loop over all the items and find those to be discounted
sorted_items.each do |line_item|
total_items_seen += line_item.quantity
# After incrementing total_items_seen, see if any items must be discounted
count = discounted_items_to_find(total_items_seen, discounted_items_seen)
# If there are none, skip to the next item
next if count <= 0
if count >= line_item.quantity
# If the full item quantity must be discounted, add it to the items to return
# and increment the count of discounted items
discounted_items.push(line_item)
discounted_items_seen += line_item.quantity
else
# If only part of the item must be discounted, split the item
discounted_item = line_item.split(take: count)
# Insert the newly-created item in the cart, right after the original item
position = cart.line_items.find_index(line_item)
cart.line_items.insert(position + 1, discounted_item)
# Add it to the list of items to return
discounted_items.push(discounted_item)
discounted_items_seen += discounted_item.quantity
end
end
# Return the items to be discounted
discounted_items
end
eligible_items = Input.cart.line_items.select do |line_item|
product = line_item.variant.product
!product.gift_card? && ELIGIBLE_PRODUCT_IDS.include?(product.id)
end
discounted_line_items = partition(Input.cart, eligible_items)
discounted_line_items.each do |line_item|
line_item.change_line_price(Money.zero, message: "Buy one, get one free!")
end
Output.cart = Input.cart
Reference from ShopifySpend at least $500, get $10 off
discount = 0 # initial discount
min_discount_order_amount = Money.new(cents:100) * 500 # $500 to cents, to fire the discount flag
total = Input.cart.subtotal_price_was
discount = Money.new(cents: 100) * 10 if total > min_discount_order_amount #discount amount you are offering in cents
message = "Here's $10 off" #discount message shown to customer
Input.cart.line_items.each do |line_item|
line_item.change_price(line_item.line_price - discount, message: message)
end
Output.cart = Input.cart
$X off for new customers
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count < 1
discount = 1000 #discount amount in cents
message = "New Customer - $10 off"
end
end
puts discount
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
line_item.change_line_price(line_item.line_price - Money.new(cents: discount), message: message) unless discount == 0
end
Output.cart = Input.cart
Curated list of Shopify shipping scripts
Discounted shipping if the cart is over $X
min_discount_order_amount = Money.new(cents:100) * 50
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
0.1
else
0
end
message = "10% off shipping if order is over $50"
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == "shopify"
shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end
Output.shipping_rates = Input.shipping_rates
Free shipping if cart is over $X
min_discount_order_amount = Money.new(cents:100) * 50
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
1
else
0
end
message = "Free shipping if order is over $50"
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == "shopify"
shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end
Output.shipping_rates = Input.shipping_rates
Free Shipping if cart is over X (excluding certain states)
MINIMUM_ORDER_AMOUNT = 50 #dollars required in cart to get discount
MESSAGE = "Minimum Order $50 Promotion" #promotional message
if !Input.cart.shipping_address.province.include?("Hawaii") && !Input.cart.shipping_address.province.include?("Alaska")
if Input.cart.subtotal_price_was > (Money.new(cents:100) * MINIMUM_ORDER_AMOUNT)
Input.shipping_rates.each do |shipping_rate|
if shipping_rate.name.include?("2 Day Ground")
shipping_rate.change_name("Free 2 Day Ground", { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
end
end
end
Output.shipping_rates = Input.shipping_rates
Free shipping on rate based on the name of the rate
discount = 1
message = "Free Standard Shipping"
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == "shopify"
next unless shipping_rate.name == "Standard Shipping"
shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
end
Output.shipping_rates = Input.shipping_rates
Free shipping for customers that spent over X
MINIMUM_SPENT = 50 #dollars purchased in history as a customer
MESSAGE = "Loyal Customer Promotion" #additional message
customer = Input.cart.customer
if customer
if customer.total_spent > (Money.new(cents:100) * MINIMUM_SPENT)
Input.shipping_rates.each do |shipping_rate|
if shipping_rate.name.include?("Insured Shipping and Handling (USPS Priority Express)")
shipping_rate.change_name("FREE VIP GROUND SHIPPING (USPS Priority Express)", { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
end
end
end
Output.shipping_rates = Input.shipping_rates
Free shipping for customers with VIP tag
TAG = "vip" #customer tag
MESSAGE = "VIP Customer Promotion" #additional message
customer = Input.cart.customer
if customer
if customer.tags.include?(TAG)
Input.shipping_rates.each do |shipping_rate|
if shipping_rate.name.include?("Insured Shipping and Handling (USPS Priority Express)")
shipping_rate.change_name("FREE VIP GROUND SHIPPING (USPS Priority Express)", { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
end
end
end
Output.shipping_rates = Input.shipping_rates
Free Shipping
MESSAGE = "Free Standard Shipping" #promotional message
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == "shopify"
next unless shipping_rate.name == "Standard Shipping"
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
Output.shipping_rates = Input.shipping_rates
Free Shipping For Customers That Spent >$X On All Orders
MINIMUM_SPENT = 50 #dollars purchased in history as a customer
MESSAGE = "Loyal Customer Promotion" #additional message
customer = Input.cart.customer
if customer
if customer.total_spent > (Money.new(cents:100) * MINIMUM_SPENT)
Input.shipping_rates.each do |shipping_rate|
if shipping_rate.name.include?("Insured Shipping and Handling (USPS Priority Express)")
shipping_rate.change_name("FREE VIP GROUND SHIPPING (USPS Priority Express)", { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
end
end
end
Output.shipping_rates = Input.shipping_rates
Display Carrier Name Below Shipping Method
Input.shipping_rates.each do |shipping_rate|
shipping_rate.apply_discount(shipping_rate.price, { message: shipping_rate.source })
end
Output.shipping_rates = Input.shipping_rates
A curated list of Shopify payments scripts
Remove Paypal if a product is tagged with “no-PayPal”
has_no_paypal_tag = Input.cart.line_items.any? { |line_item| line_item.variant.product.tags.include?('no-paypal') }
if has_no_paypal_tag
Output.payment_gateways = Input.payment_gateways.delete_if { |payment_gateway| payment_gateway.name.include?("PayPal") }
else
Output.payment_gateways = Input.payment_gateways
end
more example on Shopify website
To create Shopify script Click here