Attention cet article a été écrit avant 2020, certaines des technologies citées pourraient avoir évolué entre temps.

Exemple de scripts Shopify Plus gratuits

L'une des fonctionnalités les plus puissantes de Shopify Plus est le puissant éditeur de scripts. Les scripts permettent entre autres d'appliquer des remises lors de la commande ou encore de modifier les options de livraison. Ces remises peuvent être appliquées en fonction de critères avancés tels que les éléments déjà présents dans le panier et leur nombre. 

Comment faire ?


Tout d'abord, vous devez installer l'application gratuite Shopify Script Editor via l'App Store. L’utilisation des scripts et de l’application Script Editor n'est disponible que pour les marchands Shopify Plus.


Il existe 3 types de scripts différents:


  • Scripts d'élément de campagne. Ils affectent les éléments de campagne du panier et peuvent modifier les prix et accorder des remises à vos clients.
  • Scripts d'expédition. Ils interagissent avec l'API Shipping, modifient les méthodes d'expédition et accordent des réductions sur les tarifs d'expédition.
  • Scripts de paiement. Ils interagissent avec l'API de paiement et peuvent renommer, masquer et réorganiser la passerelle de paiement.

Voici quelques exemples de scripts :


Achetez-en 1, obtenez-en 1 pour X $ (source Dylan Hunt)

PAID_ITEM_COUNT = 1
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

Achetez-en 1, obtenez-en 1 avec X% de réduction (source Dylan Hunt)

PAID_ITEM_COUNT = 1
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

Obtenez un produit gratuit si le panier est supérieur à X $ (source Ethercycle)

FREEBIE_PRODUCT_ID = 6621649541
CART_TOTAL_FOR_DISCOUNT_APPLIED = Money.new(cents: 100) * 100
DISCOUNT_MESSAGE = "Get a FREE gift for ordering $100 or more"
freebie_in_cart = false
cart_price_exceeds_discounted_freebie_amount = false
cost_of_freebie = Money.zero
# Test if the freebie is in the cart, also get its cost if it is so we can deduct from the cart total
Input.cart.line_items.select do |line_item|
product = line_item.variant.product
if product.id == FREEBIE_PRODUCT_ID
freebie_in_cart = true
cost_of_freebie = line_item.line_price
end
end
# If the freebie exists in the cart, check the subtotal of the other items to see if the freebie should be discounted
if freebie_in_cart
cart_subtotal_minus_freebie_cost = Input.cart.subtotal_price - cost_of_freebie
if cart_subtotal_minus_freebie_cost >= CART_TOTAL_FOR_DISCOUNT_APPLIED
cart_price_exceeds_discounted_freebie_amount = true
end
end
# Only true if the freebie is in the cart
was_discount_applied = false
if cart_price_exceeds_discounted_freebie_amount
Input.cart.line_items.each do |item|
if item.variant.product.id == FREEBIE_PRODUCT_ID && was_discount_applied == false
if item.quantity > 1
new_line_item = item.split(take: 1)
new_line_item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
Input.cart.line_items << new_line_item
next
else
item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
end
end
end
end
Output.cart = Input.cart

2 achetés 1 offert (source Dylan Hunt)

PAID_ITEM_COUNT = 2
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

X% de réduction lorsque le client a passé plus de Y commandes (source Dylan Hunt)

customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count > 2 #number of orders needed to get discount
discount = 0.2 #percent discount in decimal form
message = "VIP Customer"
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 * (1-discount), message: message) unless discount == 0
end
Output.cart = Input.cart

Dépenser au moins 500 $, obtenez 10 $ de rabais (source Dylan Hunt)

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
view raw example.rb hosted with ❤ by GitHub

X $ de réduction pour les nouveaux clients (source Dylan Hunt)

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

X % de réduction pour les nouveaux clients (source Dylan Hunt)

customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count == 0
discount = 0.1 #change the discount given here
message = "Thanks for placing your first order" #change the message shown here
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 * (1-discount), message: message) unless discount == 0
end
Output.cart = Input.cart

Expédition à prix réduit si le panier est supérieur à X $ (source Dylan Hunt)

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

Livraison gratuite si le panier est supérieur à X $ (source Dylan Hunt)

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

Livraison gratuite si le panier est supérieur à X (sauf certains états) (source Dylan Hunt)

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

Livraison gratuite sur le tarif en fonction du nom du tarif (source Dylan Hunt)

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

Livraison gratuite pour les clients ayant dépensé plus de X (source Dylan Hunt)

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

Livraison gratuite pour les clients ayant dépensé plus de X (source Onlygrowth)

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

Livraison gratuite pour les clients VIP (source Onlygrowth)

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

Supprimer Paypal si un produit est taggé «no-paypal» (source Golightly+)

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