Skip to main content
All CollectionsIntegrationsShopify
[Shopify Plus] Shopify Script Editor - Instruction Manual
[Shopify Plus] Shopify Script Editor - Instruction Manual
Mags Maras avatar
Written by Mags Maras
Updated over 6 months ago

What are Shopify Scripts and Shopify Script Editor?

Shopify Scripts are small pieces of code that let you create personalized experiences for your customers in their cart and at checkout.

You can use scripts to create discounts that are applied to a cart based on the items in that cart as well as other cart properties. You can also use scripts to customize the shipping and payment options that are available to your customers.


Benefits of using Shopify Script Editor

  • Save hours of time

  • Personalization - use Scripts based on the customer information to deliver a personalized experience

  • Create automatic discounts online items for your customers without the need to use vouchers - as Shopify only allows one discount voucher code per checkout, you can use Scripts to create automatically applied discounts online items your customers have in their cart (for Black Friday or Seasonal Sales), without the need for your customers to copy and paste a seasonal voucher

  • Set automatic discounts online items in the cart for your customers in specific tiers

  • Experiences instead of points - using LoyaltyLion and Scripts can help you create a "point-free" loyalty program experience for your customers

Checklist

In order to use LoyaltyLion with Shopify Scripts, make sure you tick all the boxes below:

✅ You're using Shopify Plus

✅ Your loyalty tiers are live

✅ You have the Loyalty Tier (customer tag) enabled (read more about our metafields)


How to make sure your Loyalty Tier (customer tag) is enabled?

Our Loyalty Tier (customer tag) falls under our metafields integration. To enable this, please contact support@loyaltylion.com to get it turned on for your account and tell us which metafields you'd like us to sync. Once this is done, we'll start synchronizing this information with your Shopify.


If you want to see if the Loyalty Tier (customer tag) is enabled the easiest way is to follow the steps below:

  1. Log into your Shopify admin

  2. Click on the Customers tab

3. Select an enrolled customer from the list

4. In the profile view, scroll down to Tags box

5. Each of your enrolled customers will have a tag "tier: XXX" based on the LoyaltyLion tier they're currently in.


How to use Shopify Script Editor with LoyaltyLion?

Please note that this implementation requires technical knowledge and might involve amending the below code to your set-up. Please include your developers in the process.

If you want to see more examples of Scripts please see below:

Free shipping for customers in a specific tier

TAG = "tier: Red" #customer tag
MESSAGE = "Red tier 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?("Standard Shipping")
shipping_rate.change_name("FREE Red tier GROUND SHIPPING", { message: "" })
shipping_rate.apply_discount(shipping_rate.price, message: MESSAGE)
end
end
end
end

Output.shipping_rates = Input.shipping_rates

Percentage discount for tier members

e.g. 20% discount automatically applied to checkout for Gold Tier members

DISCOUNTS_FOR_CUSTOMER_TAG = [
{
customer_tag_match_type: :include,
customer_tags: ["tier: Gold"],
discount_type: :percent,
discount_amount: 20,
discount_message: "Discount for Gold customers!",
},
]
class CustomerTagSelector
def initialize(match_type, tags)
@comparator = match_type == :include ? 'any?' : 'none?'
@tags = tags.map { |tag| tag.downcase.strip }
end
def match?(customer)
customer_tags = customer.tags.map { |tag| tag.downcase.strip }
(@tags & customer_tags).send(@comparator)
end
end
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(pennies: 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
class DiscountForCustomerTagCampaign
def initialize(discounts)
@discounts = discounts
end
def run(cart)
return unless cart.customer&.tags
@discounts.each do |discount|
customer_tag_selector = CustomerTagSelector.new(discount[:customer_tag_match_type], discount[:customer_tags])
next unless customer_tag_selector.match?(cart.customer)
discount_applicator = DiscountApplicator.new(
discount[:discount_type],
discount[:discount_amount],
discount[:discount_message]
)
cart.line_items.each do |line_item|
discount_applicator.apply(line_item)
end
end
end
end
CAMPAIGNS = [
DiscountForCustomerTagCampaign.new(DISCOUNTS_FOR_CUSTOMER_TAG),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart

Discount by quantity for tier members

DISCOUNTS_BY_QUANTITY = {
50 => 20,
15 => 15,
10 => 10,
5 => 5,
}
TAG = "tier: Red" #customer tag
MESSAGE = "Red tier Bonus" #additional message
customer = Input.cart.customer

if customer
if customer.tags.include?(TAG)
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
line_item.quantity >= quantity
end
next unless discount
message = "Red tier bonus! #{discount}% off when buying at least #{quantity}."
line_item.change_line_price(
line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
message: message
)
end
end
end

Output.cart = Input.cart

£5 off for products for customers in a specific tier

TAG = "tier: Diamond" #customer tag
MESSAGE = "VIP Customer Promotion - £5 off Ocean Blue Shirt" #additional message
customer = Input.cart.customer

if customer
if customer.tags.include?(TAG)
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next unless product.id == 1756778135622
line_item.change_line_price(line_item.line_price - Money.new(cents: 500), message: MESSAGE)
end
end
end

Output.cart = Input.cart

If you want to learn more about Shopify Script Editor, please see Shopify Documentation Here.

Did this answer your question?