5 Best Shopify script examples – with script editor instructions

There’s been a lot of talk about Shopify Scripts and the benefits they give your sales, but with all that excitement is a huge knowledge gap that can be difficult to comprehend when writing the perfect solution. Merchants are also often thinking about how they can harness the potential and versatility of scripts to maximize their growth potential. This isn’t an easy job as there aren’t easy solutions to each challenge, especially when the implementation aspect is involved. But in order to bridge the gap, we’re releasing several scripts that have been developed from scratch, carefully chosen to cover a few of the most popular and successful applications.

If presenting discounts to customers, Shopify scripts offer a range of possibilities for customization that could give your business the edge it requires to beat your competition. Do you want access to a wide range of Scripts that will take sales to the next level during the festive season? Keep reading – we’ve got you covered.

Before you begin it is essential to be running the Shopify Script Editor application installed. This is the place where you’ll have to copy and paste scripts beneath.

Notice: Shopify Scripts is limited to Shopify Plus merchants only. Are you not yet a Plus merchant? Explore the advantages of Shopify Plus.

  1. Buy 2 Get 1 Free
  2. Buy 1 Get 1 For $X Off
  3. X% off all products with tag Y
  4. Get $X off 1st purchase
  5. Buy X, Y, and Z, and Get W% off

1. Buy 2 Get 1 Free

DISCOUNTED_ITEM_COUNT = 1
# 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? && product.id == 592406273
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 2 get 1 free”)
end
Output.cart = Input.cart

2. Buy 1 Get 1 For $X

DISCOUNTED_ITEM_COUNT = 1
# 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
@percent = Decimal.new(50) / 100.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? && product.id == 415268529
end
discounted_line_items = partition(Input.cart, eligible_items)
discounted_line_items.each do |line_item|
line_discount = line_item.line_price * @percent
line_item.change_line_price(line_item.line_price – line_discount, message: “Buy one, get one 50% off”)
end
Output.cart = Input.cart

3. X% off all products with a Y tag

Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next unless product.tags.include?(‘myTag’)
line_discount = line_item.line_price * @percent
line_item.change_line_price(line_item.line_price – line_discount, message: “25% Off”)
end
Output.cart = Input.cart

4. Get $X off on your first purchase

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

5. Buy X, Y, and Z, and Get W% off

products_needed = [592406273, 4283854977, 4284984897]
products_seen = []
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
products_seen << product.id if products_needed.include?(product.id)
end
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next unless product.id == discounted_product
line_item.change_line_price(line_item.line_price * 0.90, message: “My Sale”) if products_seen.uniq.sort == products_needed.uniq.sort
end
Output.cart = Input.cart

Increase the conversion rate of your eCommerce by implementing Shopify Scripts

If you’re an eCommerce store administrator If you own an online store, making use of Shopify scripts to improve your checkout process is a good idea. It is possible to offer your customers special discounts, educate them about the choices for payment processing and delivery, and so on. This will increase the trust of your customers in your company, improve their loyalty, and will positively impact the conversion rate.

It’s worth making sure you are acting in accordance with your business and your audience’s requirements. The one-size-fits-all solution isn’t available. Therefore, when choosing the best Shopify Script to run, prioritize your customers’ requirements and expectations.

Leave a Reply

Your email address will not be published. Required fields are marked *