Skip to content

Instantly share code, notes, and snippets.

@gavinballard
Created May 12, 2016 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gavinballard/dadd4508682b3d72bc58ad47347d21f7 to your computer and use it in GitHub Desktop.
Save gavinballard/dadd4508682b3d72bc58ad47347d21f7 to your computer and use it in GitHub Desktop.
Shopify Scripts Example
# Use an array to keep track of the discount campaigns desired.
CAMPAIGNS = [
# $5 off all items with the "sale" tag
ItemCampaign.new(
AndSelector.new(
TagSelector.new("sale"),
ExcludeGiftCardSelector.new,
),
MoneyDiscount.new(5_00, "5$ off all items on sale",),
),
# 10% off all items with a price lower than $100
ItemCampaign.new(
AndSelector.new(
ExcludeGiftCardSelector.new,
PriceSelector.new(:lower_than, Money.new(cents: 100_00)),
),
PercentageDiscount.new(10, "10% off all items under 100$"),
),
# Give every 3rd item with the tag "letter" for free
BogoCampaign.new(
TagSelector.new("letter"),
PercentageDiscount.new(100, "Third item is free"),
LowToHighPartitioner.new(2,1),
)
]
# Iterate through each of the discount campaigns.
CAMPAIGNS.each do |campaign|
# Apply the campaign onto the cart.
campaign.run(Input.cart)
end
# In order to have the changes to the line items be reflected, the output of
# the script needs to be specified.
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment